开发者

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 say开发者_如何学JAVAs:

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?


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.


Im a bit confused on the wording of the doc as well, but fetch is the way to tell your Collection or Model to go the server and get the data via either your own sync function or Backbone.sync which already internally uses ajax and can pass the result along to your success or error function.

As far as Todos example you mentioned, thats how Collection is meant to work. It updates the models, then triggers 'refresh' event which may force your view to be re-rendered, like you said.

I highly recommend looking at Backbone.js code. Its very effectively documented and the flow of events and method calls is very easy to grasp due to that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜