如何批量分配Backbone模型的所有属性?

时间:2013-08-05 10:47:57

标签: backbone.js model attributes

我想在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每个属性,但这似乎是错误的,因为属性列表可能会随着时间而改变。
通常,我希望在update(model)模型中使用User方法。


当我使用 nikoshr john-4d5 建议的Backbone的model.set()方法时......

    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: "user@example.com"
    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

3 个答案:

答案 0 :(得分:13)

  • 您正在移动Backbone.Model
  • Model.set接受属性哈希,
  • 您可以使用Model.toJSON
  • Backbone.Model转换为属性哈希值

您可以将回调写为

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

答案 1 :(得分:2)

您可以简单地使用set,将其他模型的attributes属性值(包含所有属性值的对象)作为参数。

self.model.set(model.attributes);

答案 2 :(得分: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);