Prevent Backbone collection reset

I am new to Backbone. I see this in every backbone app:

var List = Backbone.collection.extend({
    model: model
});

var myList = new List();

I am a bit confused about this. This script is included in a page, and when the page is reloaded or opened again and again, it will keep instantiate new collection doesn't it?

Whenever I save some models into this collection, things are still fine. But when I start to reload the page or open the page again, it will instantiate new collection with the same name again and the collection becomes empty again.

Any suggestions to prevent this? I want collection keep the models even if reloaded.


Use myList.fetch() in your view to load data from your api resource.

Some more info at BB site

Edit:

You can save model by using Collection create So first instantiate new collection, then use

Collection.create({
   name: 'John'
});

You can observe your network log to see what was posted to the api.

For your example:

var List = Backbone.collection.extend({
    model: model
});

var myList = new List();
myList.create({
   name: 'John'
});
链接地址: http://www.djcxy.com/p/65538.html

上一篇: Microsoft Azure CDN是一款真正的CDN还是其他东西?

下一篇: 防止骨干收集重置