オブジェクト/クラスのような Backbone.Model を構築したい。
バックボーンのドキュメントを調べたところ、継承可能な属性とメソッドを備えた継承可能なオブジェクト/クラスが2つのステップで作成されていることがわかりました。
I. いくつかの属性を持つ関数を作成する
var Model = Backbone.Model = function(attributes, options) {
var defaults;
attributes || (attributes = {}); //?
if (options && options.parse) attributes = this.parse(attributes); //?
if (defaults = getValue(this, 'defaults')) {
attributes = _.extend({}, defaults, attributes); // ?
}
if (options && options.collection) this.collection = options.collection;
this.attributes = {};
this._escapedAttributes = {};
this.cid = _.uniqueId('c');
this.changed = {};
this._silent = {};
this._pending = {};
this.set(attributes, {silent: true});
this.changed = {};
this._silent = {};
this._pending = {};
this._previousAttributes = _.clone(this.attributes);
this.initialize.apply(this, arguments);
};
Ⅱ.アンダースコアの拡張を使用して、いくつかの機能を提供します
_.extend(Model.prototype, Events, { // Events?
changed: null,
_silent: null,
_pending: null,
idAttribute: 'id',
initialize: function(){},
toJSON: function(options) {
return _.clone(this.attributes);
}
// other Model methods...
};
この動作についていくつか質問があります。
- 最初の部分の 3 ~ 4 行目は何をしますか
- 6 行目で何が起こるか?
- Events オブジェクトに「_.extend」を追加するのはなぜですか? パラメータとして他に何を指定できますか?
他に注意すべきことはありますか?
よろしく