简体   繁体   中英

Ember.RSVP always falling to failed routing

I'm using the code shown below to save a new record in Ember-Model.

var saveResult = this.get('model').save();  
saveResult.then (function(value) {$('#statusArea').html('Save succedded.\n'),
                 function(value) {$('#statusArea').html('Save failed.\n')});

Everything worked as expected on the server side. I can see the correct 'post' message coming across, and able to save the data to the DB. But no matter what status I return from the server (I've tried 200, 201, and 204), the promise always falls to the failed routine. I have two questions:

1) Am I using the Ember.RSVP.promise returned from Ember-Model correctly in the code shown above?

2) If yes, what do I have to return from the server to reflect success? I have full control on the server side, and can basically return anything necessary.

yes you are

Create your own rest adapter and override the save method

App.MyRestAdapter = Ember.RESTAdapter.extend({

  saveRecord: function(record) {
    var primaryKey = get(record.constructor, 'primaryKey'),
      url = this.buildURL(record.constructor, get(record, primaryKey)),
      self = this;

  // this is the default implementation, you're blowing up on self.didSaveRecord,
  // you should laugh a little bit noting the todo to implement an api that doesn't
  // return data :)

  //  return this.ajax(url, record.toJSON(), "PUT").then(function(data) {  // TODO: Some APIs may or may not return data
  //    self.didSaveRecord(record, data);
  //    return record;
  //  });

      // technically you could just do
      // return this.ajax(url, record.toJSON(), "PUT");
      // but if you ever want to massage it based on the response in the future, here's the spot
      return this.ajax(url, record.toJSON(), "PUT").then(function(data) {
        return record;
      });

  },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM