[英]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.