Ember-data:使用.save()

时间:2015-09-16 14:17:48

标签: ember.js ember-data

我在使用Ember-Data 1.3

尝试使用.save()记录

当我执行.save()时,没有任何问题,但请求有效负载是空的:

Ember-data PUT request

我非常确信这是一个问题,因为从后端那里我得到的唯一数据就是“令牌= blahblahblah" 。我也接受了请求(复制为cURL),我确认它是空的:

enter image description here

这里是.save()代码:

    var self = this;
        this.set('isLoading',true);
        return this.store.find('feed', feed_id).then(function(feed) {
            //Setting the system_status of the feed to either 4 (archived) or 1 (normal)
            feed.set('system_status',param);
            //Persist to change to store (and server)
            console.log(feed);
            feed.save().then(function(){
                //success
                self.set('isLoading',false);
                alert('ok');
            },function(){
                //Error
                self.set('isLoading',false);
                alert('error');
            }) // => PUT to /feeds/id
        });

RESTAdapter:

export default DS.RESTAdapter.extend({
host: 'http://localhost:8000/',
namespace: 'ed',
headers: {
    "Content-type": "x-www-form-urlencoded" // workaround for laravel
}});

在.save()之前建模console.log

enter image description here

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

解决方案确实是Serializer,您可以将其更改为发送POST请求而不是PUT请求。

使用以下代码更新DS.RESTAdapter:

  updateRecord: function(store, type, snapshot) {
    var data = this.serialize(snapshot, { includeId: true });
    var id = snapshot.id;

      console.log(snapshot);
    var url = ENV.apiUrl + "ed/" + snapshot.typeKey + "/" + snapshot.id;

    return new Ember.RSVP.Promise(function(resolve, reject) {
      jQuery.ajax({
        type: 'POST',
        url: url,
        dataType: 'json',
        data: data
      }).then(function(data) {
        Ember.run(null, resolve, data);
      }, function(jqXHR) {
        jqXHR.then = null; // tame jQuery's ill mannered promises
        Ember.run(null, reject, jqXHR);
      });
    });