2017-03-25 4 views
2

Ich lerne die tutorial on backbone source codes. Ich bin verwirrt durch den Unterschied zwischen attributes und this.attributes in den folgenden Codes. Kann jemand eine Erklärung geben? DankeAttribute vs this.attributes im Backbone 0.5.3 Quellcodes

Ich weiß, dass die aktuelle Version 1.3.3 ist, aber ich bin nur neugierig auf die Syntax in den alten Quellcodes und was es tut.

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

Ich bin mir nicht sicher, was Sie wollen, erklärt. Weißt du, was "das" in JavaScript bedeutet? – Touffy

+0

KEINE Idee, was '' 'this.atattributes''' ist für hier (' '' this.attributes = {}; this._previousAttributes = _.clone (this.attributes); '' '). '' 'attributes''' wird verwendet, um einige Felder in der Konstruktorfunktion zu setzen. – BAE

+0

Die Eigenschaft 'attributes' von Backbone Models wird verwendet, um die Felder des Modells intern zu speichern. Diejenigen, die Sie normalerweise mit den Methoden »get«, »set« und »unset« des Modells bearbeiten. – Touffy

Antwort

0
  • this.attributes ist die Eigenschaft eines Backbone Modell. Es ist der Hash, in dem die Daten des Modells gespeichert werden. Im Konstruktor wird es als leeres Objekt initialisiert. Sie können von außerhalb der Klasse modelInstance.attributes darauf zugreifen.

  • attributes ist nur der Name des Parameters in dem die Anfangsdaten auf das Modell übergeben werden. Es könnte ein beliebiger Name sein, da es sich um eine lokale Variable in der Konstruktorfunktion handelt. Auf sie kann nicht von außerhalb der Konstruktorfunktion zugegriffen werden.

Beim Erstellen einer neuen Modellinstanz können Sie Attribute übergeben, die sofort festgelegt werden.

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

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

änderte ich den Namen des lokalen attributes Variable initialData das Konzept zu erläutern. Ich habe auch ein bisschen die Konstruktorfunktion vereinfacht, um besser zu zeigen, was vor sich geht.

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); 
}; 
Verwandte Themen