Ember.js Ember.RSVP总是落在失败的路由上

Ember.js Ember.RSVP总是落在失败的路由上,ember.js,ember-model,Ember.js,Ember Model,我正在使用下面显示的代码在Ember模型中保存一条新记录 var saveResult = this.get('model').save(); saveResult.then (function(value) {$('#statusArea').html('Save succedded.\n'), function(value) {$('#statusArea').html('Save failed.\n')}); 在服务器端,一切都按预期进行。我可以看到

我正在使用下面显示的代码在Ember模型中保存一条新记录

var saveResult = this.get('model').save();  
saveResult.then (function(value) {$('#statusArea').html('Save succedded.\n'),
                 function(value) {$('#statusArea').html('Save failed.\n')});
在服务器端,一切都按预期进行。我可以看到正确的“post”消息,并且能够将数据保存到数据库中。但无论我从服务器返回什么状态(我尝试了200、201和204),承诺总是落在失败的例程上。我有两个问题:

1) 我是否在上面显示的代码中正确使用了从Ember模型返回的Ember.RSVP.promise

2) 如果是,我必须从服务器返回什么来反映成功?我完全控制服务器端,基本上可以返回任何必要的内容。

是的,您可以

创建自己的rest适配器并重写save方法

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

  },

顺便说一句,当你的函数到达失败的路径时,你有没有考虑过
value
?你有没有考虑过使用jqueryajax钩子?它们似乎更适合你的情况,也更容易控制。尤里,不知道你的意思。由于Ember模型在保存过程中控制ajax调用,您是建议我放弃Ember模型并滚动我自己的数据层,还是破解Ember模型代码来完成我需要的工作?那么只需返回status=204,以及服务器接收到的原始JSON?这是标准实践还是余烬和/或余烬模型的要求?为什么会有不必要的网络流量呢?如果你返回的是一个204,那么包含数据就没有意义了,因为包含数据的是一个200。一个常见的用例是我创建了一个记录,但我还没有与之关联的ID(通常由服务器定义),我尝试返回204,没有响应文本,以及200&201,响应文本=“ok”。所有这些都会变成承诺错误程序。”我最近一次运行时的value包含以下内容:Object{readyState=4,responseText=“ok”,status=201,statusText=“Created”then=null,等等}。在我的应用程序中,我让用户定义id,我只是验证它的唯一性。所以服务器实际上不需要返回模型;但是如果必要的话,我会让RSVP保证正常工作。由于某些原因,在Ember API文档中没有提到它。那么,除了promise之外,用法语法是否与$.getJSON相同?