我正在嘗試在我的應用程序中創建一個User.current()
,該應用程序使用$.getJSON('/users/current', function(data) { ... });
從我的服務器中提取數據。我使用的話語使用辛格爾頓方法,它具有以下功能:如何返回延期承諾並使用Ember.Deferred創建模型?
Dashboard.Singleton = Ember.Mixin.create({
// See https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/mixins/singleton.js
current: function() {
if (!this._current) {
this._current = this.createCurrent();
}
return this._current;
},
createCurrent: function() {
return this.create({});
}
});
在我User
單模式,我已經重寫createCurrent
如下:
Dashboard.User.reopenClass(Dashboard.Singleton, {
createCurrent: function() {
return Ember.Deferred.promise(function(p) {
return p.resolve($.getJSON('/users/current').then(function(data) {
return Dashboard.User.create(data);
}));
});
}
});
用戶是一個正常的餘燼對象型號:
Dashboard.User = Ember.Object.extend({
});
這確實從服務器請求數據,但功能沒有正確設置User.current()
- 當我檢查它時,User.current()
沒有應設置的屬性,例如name
。
如何返回並設置當前用戶使用Ember的延期和承諾?
什麼是用戶模式?普通的灰燼物體? – Kingpin2k
是的。我會用什麼'User'來更新我的問題。 – josh