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?
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.
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.
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.
There is only method named "start" can be used to manipulate the Backbone. js history.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With