Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I probably won't be able to answer all the questions, since code in question is probably copied from larger code base. </p> <ol> <li>Why do we use var self = this; and what exactly would this represent when the above code is executed ?</li> </ol> <p><em>var self = this;</em> is used to avoid scoping problems. Sometimes, when you use callbacks, <em>this</em> might change to some other object. Code mentioned in question doesn't benefit from it in any way <em>this</em> could be used directly.</p> <p>Example when it is usefull - lets say, we need to listen to changes in model, and we want to attach handler in initialize method and call some logic from view on changes:</p> <pre><code>// view code initialize: function() { console.log(this); // 'this' points to view this.listenTo(this.model, "change", function() { console.log(this); // 'this' points to model // calling 'this.someLogic();' would throw exception }); }, someLogic: function() { // .. } </code></pre> <p>To avoid problem described in first example, you need to store 'this' from view context in some other variable (don't have to be named self).</p> <p>Rewritten example:</p> <pre><code>// view code initialize: function() { console.log(this); // 'this' points to view var self = this; // store this into variable that will won't be changed in different scope this.listenTo(this.model, "change", function() { console.log(this); // 'this' points to model console.log(self); // 'self' points to view self.someLogic(); // won't throw }); }, someLogic: function() { // .. } </code></pre> <p>I recommend you to check how closures in JavaScript work. It is usefull not only for Backbone, but for JavaScript development in general.</p> <ol> <li>Does "this" change if we are inside initialize Vs when we are in renderCount Vs when we are in "render" in the above code?</li> </ol> <p>No, Backbone will point 'this' to view object, which contains those methodd.</p> <ol> <li>For the code "this.company.get("status")", what exactly does this.company represent? Is that referring to model ?</li> </ol> <p>No idea really, I can only guess, that it is some property from <em>BaseView</em></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload