Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 如何批量分配主干模型的所有属性?_Backbone.js_Model_Attributes - Fatal编程技术网

Backbone.js 如何批量分配主干模型的所有属性?

Backbone.js 如何批量分配主干模型的所有属性?,backbone.js,model,attributes,Backbone.js,Model,Attributes,每当登录成功时,我想更新用户模型。它包括由后端分配的id,该后端以前在模型中不存在 MyApp.module("User", function(User, App, Backbone, Marionette, $, _) { User.Controller = Backbone.Marionette.Controller.extend({ initialize: function() { this.model = new MyApp.User.Model();

每当
登录
成功时,我想更新
用户
模型。它包括由后端分配的
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.
    }

  });
});
如何一次更新所有属性?我知道我可以手动更改每个属性,但这似乎是错误的,因为属性列表可能会随时间而更改。
通常,我希望在
用户
模型中有这样一个
更新(模型)
方法


当我使用Nikosh和john-4d5建议的主干
model.set()
方法时

。。。
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

您可以使用@nikosh所说的
this.model.set(model)
。迭代属性并设置每个属性将执行与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);
  • 您正在移动一个
    主干。型号
  • 接受属性的散列
  • 您可以将
    主干.Model
    转换为具有
您可以将回调写为

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

您只需使用
set
,将另一个模型的
属性
属性值(具有所有属性值的对象)作为参数


self.model.set(model.attributes)

我可能错过了一些东西,但是
这个.model.set(model)有什么问题?@nikoshr实际上我在文档中没有看到这一点。我尝试了它,但它没有复制其他属性,然后
id
。例如,更新后的模型中缺少
name
。那么
model
包含什么(成功回调中的第一个参数)?我找到了一个可行的替代方法:
self.model.set(model.attributes)
。这似乎是对我想要实现的目标的一种限制。你的评论是否意味着你会不鼓励使用
.attributes
?它是一种内部表示。它可能在您的情况下工作,但不能保证它在将来的主干版本中保持不变。我倾向于谨慎,并倾向于
Model.toJSON
我实际上无法从文档中读取这一点。相反,
.toJSON()
对我来说似乎更不稳定,比如
.toString()
。我认为在处理模型(用户、会话)时存在架构错误。我现在更改了设置。我会接受你的回答,因为这对我来说很有意义。
success: function(model, response, options) {
    self.model.set(model.toJSON());
}