backbone 0.5.3 源代码中的属性与 this.attributes
attributes vs this.attributes in the backbone 0.5.3 source codes
我正在学习tutorial on backbone source codes。我对以下代码中 attributes
和 this.attributes
之间的区别感到困惑。有人可以解释一下吗?谢谢
我知道当前版本是 1.3.3,但我只是想知道旧源代码中的语法及其作用。
Backbone.Model = function(attributes, options) {
var defaults;
attributes || (attributes = {});
if (defaults = this.defaults) {
if (_.isFunction(defaults)) defaults = defaults.call(this);
attributes = _.extend({}, defaults, attributes);
}
this.attributes = {};
this._escapedAttributes = {};
this.cid = _.uniqueId('c');
this.set(attributes, {silent : true});
this._changed = false;
this._previousAttributes = _.clone(this.attributes);
if (options && options.collection) this.collection = options.collection;
this.initialize(attributes, options);
};
this.attributes
是 Backbone 模型的 属性。它是保存模型数据的散列。在构造函数中,它被初始化为一个空对象。您可以从 class modelInstance.attributes
.
外部访问它
attributes
只是 参数 的名称,其中初始数据被传递给该模型。它可以是任何名称,因为它是构造函数中的局部变量。它不能从构造函数外部访问。
当创建一个新的模型实例时,您可以传递立即设置的属性。
var data = {
initial: "data",
id: "1234"
// etc.
};
// here, data is passed into the `attributes` parameter.
var modelInstance = new Backbone.Model(data);
我将本地 attributes
变量的名称更改为 initialData
来说明这个概念。我还稍微简化了构造函数以更好地展示发生了什么。
Backbone.Model = function(initialData, options) {
// Initializes properties
this.attributes = {};
this._escapedAttributes = {};
this.cid = _.uniqueId('c');
// Makes sure that the initialData variable is an object.
if (!initialData) { // if it's a falsy value (undefined, 0, null, "")
initialData = {}; // assign it a new object
}
// if the `defaults` property exists on this model class
if (this.defaults) {
var defaults;
// and if it's a function
if (_.isFunction(defaults)) {
// call it and save the return value
defaults = this.defaults();
}
// merge the defaults with the initial data, where any attribute present
// in the initialData object overwrites the default value.
initialData = _.extend({}, defaults, initialData);
}
// Apply the initialData to the `this.attributes` with all of other things
// `set` does. No events are triggered since `silent` is set to true.
this.set(initialData, { silent: true });
// cancel the change flag since it's the initialization of a new instance.
// Even if it changed from an empty object to the initialData value, it
// doesn't make sense to flag it as changed.
this._changed = false;
// a shallow copy of the attributes to compare to when changes are made.
this._previousAttributes = _.clone(this.attributes);
// A model keeps a reference to the collection instance in which it was
// created (when passing raw data to a collection) to use its `url` property
// when none are set in the model class.
if (options && options.collection) this.collection = options.collection;
// calls the initialize function, which is empty by default and is just
// a convinience for the developer to override.
this.initialize(initialData, options);
};
我正在学习tutorial on backbone source codes。我对以下代码中 attributes
和 this.attributes
之间的区别感到困惑。有人可以解释一下吗?谢谢
我知道当前版本是 1.3.3,但我只是想知道旧源代码中的语法及其作用。
Backbone.Model = function(attributes, options) {
var defaults;
attributes || (attributes = {});
if (defaults = this.defaults) {
if (_.isFunction(defaults)) defaults = defaults.call(this);
attributes = _.extend({}, defaults, attributes);
}
this.attributes = {};
this._escapedAttributes = {};
this.cid = _.uniqueId('c');
this.set(attributes, {silent : true});
this._changed = false;
this._previousAttributes = _.clone(this.attributes);
if (options && options.collection) this.collection = options.collection;
this.initialize(attributes, options);
};
this.attributes
是 Backbone 模型的 属性。它是保存模型数据的散列。在构造函数中,它被初始化为一个空对象。您可以从 classmodelInstance.attributes
. 外部访问它
attributes
只是 参数 的名称,其中初始数据被传递给该模型。它可以是任何名称,因为它是构造函数中的局部变量。它不能从构造函数外部访问。
当创建一个新的模型实例时,您可以传递立即设置的属性。
var data = {
initial: "data",
id: "1234"
// etc.
};
// here, data is passed into the `attributes` parameter.
var modelInstance = new Backbone.Model(data);
我将本地 attributes
变量的名称更改为 initialData
来说明这个概念。我还稍微简化了构造函数以更好地展示发生了什么。
Backbone.Model = function(initialData, options) {
// Initializes properties
this.attributes = {};
this._escapedAttributes = {};
this.cid = _.uniqueId('c');
// Makes sure that the initialData variable is an object.
if (!initialData) { // if it's a falsy value (undefined, 0, null, "")
initialData = {}; // assign it a new object
}
// if the `defaults` property exists on this model class
if (this.defaults) {
var defaults;
// and if it's a function
if (_.isFunction(defaults)) {
// call it and save the return value
defaults = this.defaults();
}
// merge the defaults with the initial data, where any attribute present
// in the initialData object overwrites the default value.
initialData = _.extend({}, defaults, initialData);
}
// Apply the initialData to the `this.attributes` with all of other things
// `set` does. No events are triggered since `silent` is set to true.
this.set(initialData, { silent: true });
// cancel the change flag since it's the initialization of a new instance.
// Even if it changed from an empty object to the initialData value, it
// doesn't make sense to flag it as changed.
this._changed = false;
// a shallow copy of the attributes to compare to when changes are made.
this._previousAttributes = _.clone(this.attributes);
// A model keeps a reference to the collection instance in which it was
// created (when passing raw data to a collection) to use its `url` property
// when none are set in the model class.
if (options && options.collection) this.collection = options.collection;
// calls the initialize function, which is empty by default and is just
// a convinience for the developer to override.
this.initialize(initialData, options);
};