我想更新User
型號,只要signIn
是成功。它包括由後端分配的id
,這在模型中還沒有出現過。如何批量分配骨幹模型的所有屬性?
MyApp.module("User", function(User, App, Backbone, Marionette, $, _) {
User.Controller = Backbone.Marionette.Controller.extend({
initialize: function() {
this.model = new MyApp.User.Model();
},
signIn: function(credentials) {
var signInData = { user: credentials };
var self = this;
App.session.signIn(signInData, {
success: function(model, response, options) {
self.updateUserModel(model);
},
error: function(model, xhr, options) {}
});
},
updateUserModel: function(model) {
// TODO Update all attributes, including new onces e.g. id.
}
});
});
如何一次更新所有屬性?我知道我手動可以set
每一個屬性,但這似乎是錯誤的,因爲屬性列表可能隨時間而改變。
一般來說,我期望在User
模型中有這樣的update(model)
方法。
當我使用骨幹model.set()
方法,通過nikoshr和約翰-4D5 ...
signIn: function(credentials) {
var signInData = { user: credentials };
var self = this;
App.session.signIn(signInData, {
success: function(model, response, options) {
self.model.set(model);
},
error: function(model, xhr, options) {}
});
},
...的id
屬性被複制到this.model
但其他性能如的建議缺少name
。
在success
回調返回的模式是這樣的:
_changing: false
_pending: false
_previousAttributes: Object
attributes: Object
bind: function (name, callback, context) {
close: function(){
constructor: function(){ return parent.apply(this, arguments); }
created_at: "2013-07-22T19:03:24Z"
email: "[email protected]"
id: 3
initialize: function() {
listenTo: function (obj, name, callback) {
listenToOnce: function (obj, name, callback) {
logout: function() {
model: child
name: "Some User"
off: function (name, callback, context) {
on: function (name, callback, context) {
once: function (name, callback, context) {
options: Object
signIn: function (credentials) {
signUp: function (credentials) {
stopListening: function (obj, name, callback) {
trigger: function (name) {
triggerMethod: function (event) {
unbind: function (name, callback, context) {
updated_at: "2013-08-05T13:20:43Z"
user: Object
__proto__: Object
changed: Object
cid: "c3"
id: 3
__proto__: Surrogate
我可能錯過了一些東西,但是這有什麼錯'this.model.set(模型);'? – nikoshr
@nikoshr其實我沒有在文檔中看到這個。我嘗試過,但它不復制其他屬性,然後'id'。例如,更新模型中缺少'name'。 – JJD
那麼,'model'包含了什麼(成功回調中的第一個參數)? – nikoshr