我使用下面显示的代码在Ember-Model中保存一个新记录。
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-Model返回的Ember.RSVP.promise?
2)如果是,我必须从服务器返回什么才能反映成功?我在服务器端有完全的控制权,基本上可以返回任何需要的东西。
发布于 2013-11-20 10:52:17
是的,你是
创建您自己的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;
});
},https://stackoverflow.com/questions/20086208
复制相似问题