3

我想在成功User时更新模型。它包括以前模型中不存在的后端分配。signInid

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


当我按照nikoshrjohn-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
4

3 回答 3

13
  • 你在一个Backbone.Model,
  • Model.set接受属性哈希,
  • 您可以将 a 转换Backbone.Model为属性哈希Model.toJSON

你可以把你的回调写成

success: function(model, response, options) {
    self.model.set(model.toJSON());
}
于 2013-08-05T13:38:15.930 回答
2

您可以简单地使用set,将另一个模型的attributes属性值(具有所有属性值的对象)作为参数。

self.model.set(model.attributes);

于 2015-02-01T14:47:22.670 回答
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);
于 2013-08-05T12:40:52.210 回答