Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Generally speaking, if you have <code>someView</code>, and <code>someView</code> was declared with <code>{model: someModel}</code>, and you want to make <code>someView</code> use <code>someOtherModel</code> instead, the right way to do it is:</p> <pre><code>someView.model = someOtherModel; </code></pre> <p>You <em>could</em>, as gumballhead suggested, simply change the data of the model with:</p> <pre><code>someView.model.set(someOtherModel.toJSON()); </code></pre> <p>but that's ... well this isn't the best analogy, but that's kind of like erasing a Tim Lincecum baseball card and drawing Matt Williams on it, rather than simply swapping a Tim card for a Matt card. Either way you wind up with a baseball card that says Matt, but conceptually there's a world of difference.</p> <p>However, if you have a whole lot of view classes that share the same model, and you want to make changing that model for all views at once a convenient operation, there's a few ways you can go.</p> <p>One way is to keep track of all the views in a list somewhere, and then whenever you want to change the model iterate through all of them, like so:</p> <pre><code>var viewsThatUseBaseballCard = [view1, view2, view3, view4]; ... _(viewsThatUseBaseballCard).each(function(view) { view.model = someOtherModel; }); </code></pre> <p>Another way is to avoid the built-in <code>this.model</code> and instead use a function in your View code to get the model whenever you need it; in other words, instead of:</p> <pre><code>var MyView = Backbone.View.extend({ someMethod: function() { this.model.doSomething(); } }); var MyView = Backbone.View.extend({ someMethod: function() { getCurrentModel().doSomething(); } }); </code></pre> <p>(and then of course you'll need to do something to make <code>getCurrentModel</code> return <code>someOtherModel</code>, but at least then it will be centralized to a single place in the code).</p> <p>There are other variations of the above, but the general idea is that you can either:</p> <p>A) use <code>this.model</code> in your views and just set a new <code>this.model</code> as needed</p> <p>B) use some sort of <code>getModel()</code> call in your views, and modify tell that function to return a new model as appropriate</p> <p>(or you can C) keep the same model and change what it is, ie. the erase/re-write the baseball card option).</p> <p>I personally would probably lean towards B), but it ultimately depends on the particulars of your situation.</p> <p><strong>* EDIT *</strong></p> <p>Just realized that there are a couple other options I missed:</p> <p>D) Re-design your views so that they don't all depend on the same model (not sure this is an option for you, but it's worth mentioning)</p> <p>E) Use a "reference model"; in other words, instead of:</p> <pre><code>new SomeView({model: TimLincecum}); </code></pre> <p>do:</p> <pre><code>new SomeView({model: currentPlayerReference}); </code></pre> <p>Then, inside your views, do:</p> <pre><code>this.model.getCurrentPlayer(); </code></pre> <p>This is similar to my option B), but it has the added benefit of letting you store data that doesn't change in the <code>currentPlayerReference</code> model.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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