繁体   English   中英

Ember.RSVP总是会陷入路由失败

[英]Ember.RSVP always falling to failed routing

我正在使用下面显示的代码在Ember-Model中保存新记录。

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

一切都在服务器端按预期工作。 我可以看到正确的“发布”消息,并且能够将数据保存到数据库。 但是,无论我从服务器返回什么状态(我尝试过200、201和204),承诺总是落在失败的例程上。 我有两个问题:

1)我在上面显示的代码中正确使用了从Ember-Model返回的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;
      });

  },

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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