2017-03-25 14 views
2

私はtutorial on backbone source codesを学習しています。私はattributesthis.attributesの違いを次のコードで混同しています。誰かが何か説明をすることはできますか?ありがとう属性vs this.attributes in backbone 0.5.3ソースコード

私は、現在のバージョンが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); 
}; 
+0

私はあなたが何を説明したいのか分かりません。 JavaScriptで「これ」が何を意味するのか知っていますか? – Touffy

+0

'' 'this.attributes''はここ(' '' this.attributes = {}; this._previousAttributes = _.clone(this.attributes); '' ')のためのものではありません。 '' 'attributes'''はコンストラクタ関数のいくつかのフィールドを設定するために使われます。 – BAE

+0

Backbone Modelsの 'attributes'プロパティは、モデルのフィールドを内部的に保存するために使用されます。モデルの 'get'、' set'、 'unset'メソッドで通常操作するものです。 – Touffy

答えて

0
  • this.attributesバックボーンモデルのプロパティです。これはモデルのデータが保持されるハッシュです。コンストラクタでは、空のオブジェクトとして初期化されます。クラスmodelInstance.attributesの外からアクセスできます。

  • attributesは、初期データがモデルに渡されたパラメータの名前だけです。コンストラクタ関数内のローカル変数であるため、任意の名前を使用できます。コンストラクタ関数の外部からアクセスすることはできません。

新しいモデルインスタンスを作成するときに、すぐに設定する属性を渡すことができます。

var data = { 
    initial: "data", 
    id: "1234" 
    // etc. 
}; 

// here, data is passed into the `attributes` parameter. 
var modelInstance = new Backbone.Model(data); 

私は概念を説明するためにinitialDataにローカルattributes変数の名前を変更しました。私はまた、何が起こっているかをより良く示すために、コンストラクタ関数を単純化しました。

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); 
}; 
関連する問題