2013-08-05 74 views
2

我想更新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 
+0

我可能錯過了一些東西,但是這有什麼錯'this.model.set(模型);'? – nikoshr

+0

@nikoshr其實我沒有在文檔中看到這個。我嘗試過,但它不復制其他屬性,然後'id'。例如,更新模型中缺少'name'。 – JJD

+0

那麼,'model'包含了什麼(成功回調中的第一個參數)? – nikoshr

回答

11
  • 你是一個Backbone.Model走動,
  • Model.set接受屬性的哈希值,
  • 你可以將Backbone.Model轉換爲屬性的散列值Model.toJSON

你可以寫你的回調爲

success: function(model, response, options) { 
    self.model.set(model.toJSON()); 
} 
+1

我找到了一個替代方案:'self.model.set(model.attributes)'。這似乎是我想要完成的事情。 – JJD

+0

一定要閱讀http://backbonejs.org/#Model-attributes – nikoshr

+0

你的評論是否意味着你會不鼓勵使用'.attributes'? – JJD

1

您可以使用this.model.set(model)作爲@nikoshr說。迭代屬性並設置每個屬性都會執行與model.set相同的操作。請參閱骨幹model.set功能:

// Set a hash of model attributes on the object, firing `"change"`. This is 
// the core primitive operation of a model, updating the data and notifying 
// anyone who needs to know about the change in state. The heart of the beast. 
set: function(key, val, options) { 
    var attr, attrs, unset, changes, silent, changing, prev, current; 
    if (key == null) return this; 

    // Handle both `"key", value` and `{key: value}` -style arguments. 
    if (typeof key === 'object') { 
    attrs = key; 
    options = val; 
    } else { 
    (attrs = {})[key] = val; 
    } 

    [...] 

    // For each `set` attribute, update or delete the current value. 
    for (attr in attrs) { 
    val = attrs[attr]; 
    if (!_.isEqual(current[attr], val)) changes.push(attr); 
    if (!_.isEqual(prev[attr], val)) { 
     this.changed[attr] = val; 
    } else { 
     delete this.changed[attr]; 
    } 
    unset ? delete current[attr] : current[attr] = val; 
    } 
[...] 
} 

另一種選擇是實例化一個新的模式:

this.model = new MyApp.User.Model(model); 
2

您可以簡單地使用set,給其他型號的attributes屬性值(所有屬性值的對象)作爲參數。

self.model.set(model.attributes);