Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your view should display the current state of your collection/models. So binding to reset in your view's initialize method (and leaving it bound) seem to satisfy your requirements. When the collection is reset via the reset or fetch methods, your view will be notified when the model's are ready.</p> <blockquote> <p>I realize in Backbone, there is a reset event I can bind to. But I only want to bind to the reset event if reset hasn't been called yet, and I only want to bind to it "once", so I might want to do something where I bind to the reset event, and within the event handler, unbind that same handler.</p> </blockquote> <p>I think this is the key to your question. You are right, you should only bind to reset once. And you should probably not be unbinding to it. If your collection resets, you would want your view to respond, right? If you create your view with an already fetched and full collection, binding to reset shouldn't cause a problem.</p> <p>Maybe if that doesn't clarify things, you can provide more details about your situation.</p> <p>EDIT: I am trying to understand you situation. Your select control is bound to a model, and selecting its value when notified that the model is ready (via reset or change event). If at some point, you have an initialized model when you create the view, you will know that it is initialized, and you can do the same selection code that you now do in the reset handler.</p> <p>For example, maybe you are creating your view and calling render on it, adding it to the page. At the end of render you could call a function updateUI or something (the same function you call during the reset handler). If the model is filled during that initial render, it will select the values in the UI; if not, it will do nothing and try again on reset...</p> <p>Of course, if some things on your page are not ready (waiting for your async ajax) you will need to show a spinner or disable things until you get your reset or change event.</p> <p>EDIT2: One generality that may apply: Backbone is an MVC framework. MVC is all about separation of concerns. The concern of the view is to render the state of the model. If the model is not initialized when the view first renders, it should reflect that (showing nothing, or an "I am empty" type message), updating the UI when it receives the reset event (in your case, selecting the region). If the model is initialized when the view first renders, it should render and update the UI in the same way. In backbone it can get a bit confused, since the view.js also plays the role of controller, which is responsible for translating the UI changes into actions on the model.</p>
 

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