Note that there are some explanatory texts on larger screens.

plurals
  1. POBackboneJs: how do i bootstrap my data in the page markup, and when do i assign them to my collections
    primarykey
    data
    text
    <p>So, building an application that uses multiple (2 for now) global collections, it is a catalog of both documents and patients, they have relations, but not as in, 1 document or a list of documents belonging to 1 patient, so they are in fact 2 separate collections,</p> <p>my app is structured in a module system very similar to how it is described here: <a href="http://weblog.bocoup.com/organizing-your-backbone-js-application-with-modules" rel="nofollow">http://weblog.bocoup.com/organizing-your-backbone-js-application-with-modules</a></p> <p>the backbone.js documentation says about bootstrapping, to do something like this,</p> <pre><code>&lt;script&gt; Accounts.reset(&lt;%= @accounts.to_json %&gt;); &lt;/script&gt; </code></pre> <p>that is within a Rails application, i would however need to do it differently in asp.net MVC3, most likely i would just print out my json string without the &lt;%= %> which isn't razor view engine style)</p> <p>but my question here is,</p> <p>this <code>Accounts.reset(...data...);</code> is just floating somewhere in my markup, it is not in any way nicely structured in my module system, isn't there a way to nicely do this? where as i can get the data, from within my module?</p> <p>and another side question, suppose i have a route in my backbone app <a href="http://example.com/#documents" rel="nofollow">http://example.com/#documents</a></p> <p>and someone calls this link directly, will my app have the data ready (from the bootstrap) on time, before the route itself is executed?</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. COI'm no backbone.js expert but I wrote a simple JsFiddle that does this type of bootstrapping, albeit a little different. Basically, I when I initialize my View, it creates the Collection and calls Collection.refresh(). So anywhere I create my View, it auto-initializes its data...or bootstraps based on your verbiage. Here is the link to the JsFiddle: http://jsfiddle.net/jcscoobyrs/WVX66/
      singulars
    2. COgood idea, but as i read on the backbone documentation, using fetch (or like you refresh your collection) is not necessary on page load. why have a second connection to fetch all the data, while you could have sent it immediately with the page. all i wonder is if the way i do it now is the best way to give it with the html... as now it just inserts a script tag, adding a whole heap of json data, which i of course remove after initializing my collection. however since your json comes from an external source you can't use my technique for your example so using collection.refresh() would be best
      singulars
    3. COBackbone.js is written to interface with RESTful endpoints and it does this by tying your Collection to a REST endpoint that is used to retrieve/fresh your collection. While you can bootstrap things the way you're wanting, as you see by the answer you got, it's not as common as you think. The purpose of backbone.js and the like is to separate UI/View from the data itself so in my experiences, I never pass anything more than boilerplate HTML to load my backbone.js which then will do its work of retrieving/refreshing/managing my REST-based objects. "To each his own." :)
      singulars
 

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