3
votes

Im stuck trying to.save() a record using Ember-Data 1.3 ????

When I perform a .save() nothing goes wrong but the request payload is empty:

Ember-data PUT request

I'm pretty convinced it's an issue with the ember-data request because from the back-end side the only data I got it's "token=blahblahblah". Also I took the request (copy as cURL) and I confirm it's empty:

enter image description here

Here's the .save() code:

    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
}});

Model console.log before .save()

enter image description here

Any ideas?

1
The issue might be in a serializer. Could you please provide its code as well? - Artur Smirnov

1 Answers

0
votes

The solution is indeed the Serializer, you can change it to send a POST request instead of a PUT request.

Update the DS.RESTAdapter with this code:

  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);
      });
    });