Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initial backbone.js data fetch

I've been playing with backbone.js for the last few days, editing the Todos example and writing my own version.

I'm now looking at creating a controller with multiple routes, but what I have found after following the Todos example is calling the Todos.fetch(), causes the items to be re-rendered. I think its calling the refresh event?

The backbone.js documentation says:

Note that fetch should not be used to populate collections on page load — all models needed at load time should already be bootstrapped into place. fetch is intended for lazily-loading models for interfaces that are not needed immediately: for example, documents with collections of notes that may be toggled open and closed.

What is the best way to load data then? Can I use an use an ajax call to get the data and pass it when instantiating the main controller? What about if I were to change page and a new view were to shown? Would I just call the fetch() function?

like image 889
Webster Avatar asked Mar 31 '11 01:03

Webster


People also ask

Does anyone still use Backbone JS?

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.

Can we set default values for model in Backbone JS?

defaults() The Backbone. js defaults model is used to set a default value to a model. It ensures that if a user doesn't specify any data, the model would not fall with empty property.

How does Backbone JS work?

Backbone. js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.

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.


1 Answers

This bit of documentation is there to remind you that you should load all the data on the initial page load and use fetch for all the subsequent loads.

You can have code like this:

MyCollection = new Collection({some json data});

Much faster than a page load then a couple of request to load the data.

like image 177
Julien Avatar answered Oct 31 '22 23:10

Julien