Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Whenever you call fetch on a model the response is passed through a <code>parse</code> method that can be defined in your model. <code>parse</code> takes one parameter, the ajax response:</p> <pre><code>parse: function(response) { } </code></pre> <p>In that function you can do whatever you want with the data that comes back from your ajax request and eventually return that object. The object returned by the parse method will be set on your model.</p> <p>For event binding, you'll want to do that in your view. In the initialize method of your view you can do something like:</p> <pre><code>this.collection.on("change", this.someFunction); </code></pre> <p>Now, any time something causes that model to trigger its change event <code>someFunction</code> ( also defined in your view ) will run.</p> <p><strong>EDIT</strong></p> <p>The sample json you added to the question looks to be pretty normalized. With that data, I'd be fetching it into a collection. If that's the structure you want your models to look like then you don't need to do much parsing.</p> <p>in you collection file if you create a parse method that does the following:</p> <pre><code>parse: function(response) { return response.segments; } </code></pre> <p>When you call your fetch, <code>this.collection.fetch()</code> on a successful request, your collection will be filled with models that contain attributes in a structure that matches your response.</p> <p><strong>EDIT 2</strong></p> <p>Your binding looks ok.</p> <p>in this section of code:</p> <pre><code>this.collection.fetch({ success: function (model, attributes) { initAll(attributes); // populate ui with attributes from model } }) </code></pre> <p>The parameters that are passed back on a success in a collection fetch are <code>(collection, response)</code> <code>collection</code> is the result of collection call and what <code>this.collection</code> will end up being. <code>response</code> is the response of your ajax request.</p> <p>I'm not sure what <code>initAll(attributes)</code> is supposed to be doing. If you add a parse method like I posted above, your collection will contain a set of models with the attributes of each segment.</p> <p>Also, rather than calling <code>this.render()</code> at the end, you could do bind render to the change event:</p> <pre><code>this.collection.on('change', this.render); </code></pre> <p>That way any time your collection changes, that view will automatically render again so your changes will show up.</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