Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js: rendering collections in real world apps

Tags:

backbone.js

Frustratingly, most ‘tutorial’ examples of backbone.js apps assume a clean model slate. I-.e. that the model collections are empty initially, until a user adds an item. Of course this is not the case in a real world app, where you usually have an existing collection to start with from the back end store.

I would like to know how people deal with existing collections in backbone. Specifically:

  • How do you render a collection after it has been fetched? Is is just a case of iterating through the collection? Should this be trigger by some event?

  • The backbone docs talk about ‘bootstrapping’, which I understand means using data that is available on the initial load (this also makes sense from an SEO point of view). But how does this work in practice? The data is dumped into JS on the server side? Or the JS examines the DOM?

I feel like this is a poor question, but I expect to expand it based on the answers.

EDIT

So it seems that the consensus is to add the data as party of the JS and process that on page load.

One big disadvantage I see with this technique is that the information is not available for search engine spiders. From that perspective it might be better to extract it from the DOM (although I haven't seen anyone do it that way). Or maybe add the HTML server side and stick the data in the JS?

like image 575
UpTheCreek Avatar asked Sep 09 '11 21:09

UpTheCreek


People also ask

Is Backbone JS still relevant?

Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.

What is the use of backbone JS?

BackboneJS is a lightweight JavaScript library that allows to develop and structure the client side applications that run in a web browser. It offers MVC framework which abstracts data into models, DOM into views and bind these two using events.

What is collections in Backbone JS?

Collections are ordered sets of Models. We just need to extend the backbone's collection class to create our own collection. Any event that is triggered on a model in a collection will also be triggered on the collection directly.

What is the only method available in the backbone JS history?

There is only method named "start" can be used to manipulate the Backbone. js history.


2 Answers

I came across the same situation as you, I don't always want my data bootstrapped initially (especially if its from a third-party api call). I haven't come across any tutorials that do this but looking through the documentation its actually pretty easy. You just need to bind the 'reset' event (this is fired when the entire collection is repopulated) on your collection and render. Heres a quick example:

my_application.js

window.MyApplication = {   Models: {},   Collections: {},   Views: {},   Routers: {},   init: function() {     // Start the data loading     this.someItems = new MyApplication.Collections.Items();     this.someItems.fetch();      // Start rendering the UI before the data is ready     new MyApplication.Routers.Items(this.someItems);     Backbone.history.start();   } }; 

routers/items_router.js

MyApplication.Routers.Items = Backbone.Router.extend({   routes: {     "" : "index"   },    initialize: function(collection) {     this.collection = collection;   },    index: function() {     var view = new MyApplication.Views.ItemsIndex({ collection: this.collection });     $('.items').html(view.render().el);   } }); 

views/items/items_index.js

MyApplication.Views.ItemsIndex = Backbone.View.extend({   initialize: function() {     _.bindAll(this, "render");      // Once the collection is fetched re-render the view     this.collection.bind("reset", this.render);   },    render: function() {     console.log(this.collection.length);     // Render content     return this;   } }); 
like image 106
jwarzech Avatar answered Oct 14 '22 23:10

jwarzech


As far as rendering collections, yes I normally iterate over a collection and create a child view for each of the models. Here is a link that might be helpful on that http://liquidmedia.ca/blog/2011/02/backbone-js-part-3/

When should you render it? Well that's a tough one to answer but I wouldn't say it's in response to any particular event. One thing that I like is that we have one main view that renders sub views which render other sub views. As a convention we don't render directly to the DOM but once all the sub views are rendered, we append the main view to the DOM and the whole page shows up immediately. This avoids the 'flash of unstyled content'

Regarding bootstrapping I see from the contents that you read the FAQ, and that's just what I've done in practice. I use ASP.Net MVC 3 so my server side rendered view would have something like (though I wouldn't put 'books' on the global namespace normally):

<script type="text/javascript">     books = new BooksCollection();     books.reset(@Html.ToJson(Model)); </script> 

Hope that's helpful.

like image 42
timDunham Avatar answered Oct 14 '22 23:10

timDunham