我想在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
答案 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);