Javascript 如何将属性传递到我不希望被视为属性的 Backbone.Model 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7084651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I pass properties into a Backbone.Model which I do not wish to be treated as attributes?
提问by aw crud
I have a Backbone.Model named Company
.
My Company
model has an Employees
Backbone.Collection containing Employee
models.
我有一个名为Company
. 我的Company
模型有一个Employees
包含Employee
模型的Backbone.Collection 。
When I instantiate my Employee
models to populate the Employees
collection I wish for them to have a reference to the Company
they belong to. But when I pass Company
in it becomes one of the attributes of Employee
. This is an issue when I go to save the Employee
because the toJSON
method will contain a Company
object, when in the database all I store is the foreign key integer company_id
.
当我实例化我的Employee
模型以填充Employees
集合时,我希望它们能够引用Company
它们所属的模型。但是当我传入时Company
,它成为Employee
. 当我去保存时这是一个问题,Employee
因为该toJSON
方法将包含一个Company
对象,而在数据库中我存储的只是外键 integer company_id
。
I wish there was a second parameter for Backbone.Model that accepted model properties that are not part of the core attributes. How can I get around this? I realize I could instantiate my Employee
models and afterwards attach the Company
, but I really want to do all the assignments in a traditional "constructor" rather than attach properties from outside.
我希望 Backbone.Model 有第二个参数接受不属于核心属性的模型属性。我怎样才能解决这个问题?我意识到我可以实例化我的Employee
模型,然后附加Company
,但我真的想在传统的“构造函数”中完成所有分配,而不是从外部附加属性。
e.g.:
例如:
Employee = Backbone.Model.extend({});
Employees = Backbone.Collection.extend({
model: Employee
});
Company = Backbone.Model.extend({
initialize: function() {
this.employees = new Employees({});
}
});
c1 = new Company({id: 1});
e = new Employee({name: 'Joe', company_id: 1, company: c1});
c1.employees.add(e);
e.get('company'); // => c1
e.save(); // BAD -- attempts to save the 'company' attribute, when in reality I only want to save name and company_id
//I could do
c2 = new Company({id: 2});
e2 = new Employee({name: 'Jane', company_id: 2});
e2.company = c2;
c2.employees.add(e);
e.company; // => c2
//I don't like this second method because the company property is set externally and I'd have to know it was being set everywhere in the code since the Employee model does not have any way to guarantee it exists
回答by shesek
You could always read it manually from the options
object and store it however you like. The options are passed as the second argument to the initialize method:
您始终可以从options
对象中手动读取它并根据需要存储它。这些选项作为第二个参数传递给 initialize 方法:
var Employee = Backbone.Model.extend({
initialize: function(attributes, options) {
this.company = options.company;
}
});
var shesek = new Employee({name: 'Nadav'}, {company: Foobar});
Alternatively, you can use Backbone-relationalwhich makes it much easier to handle models that contains references to other models and collection.
或者,您可以使用Backbone-relational,它可以更轻松地处理包含对其他模型和集合的引用的模型。
You might also be interested in making toJSON() recursive(a patch I submitted to their issue tracker)
您可能也有兴趣使 toJSON() 递归(我提交给他们的问题跟踪器的补丁)
回答by Justin Thomas
You can override the save method on the model to convert the save record to only send name and company id.
您可以覆盖模型上的 save 方法以将保存记录转换为仅发送名称和公司 ID。
Employee = Backbone.Model.extend({
save: function() {
var toSend = {name:this.get('name'),company_id: this.get('company').get('id')}
// method to send toSend to server.
}
});