Tuesday 15 September 2015

backbone.js - Destroy all models in Backbone collection (persisted in local storage) -



backbone.js - Destroy all models in Backbone collection (persisted in local storage) -

how delete models in collection (persisted in local storage)?

the models fetched local storage - want models destroyed both @ client , in local storage.

// model + collection app.models.task = backbone.model.extend({ defaults: { text: 'n/a' } }); app.collections.tasks = backbone.collection.extend({ model: app.models.task, localstorage: new backbone.localstorage("task") }); // create collection , fetch tasks var tasks = new app.collections.tasks(); tasks.fetch(); // collection populated 4 tasks // delete models (both @ client , local storage) tasks.each(function(model) { model.destroy(); })

from running this, destroy of models - error occurs , prevents rest beingness destroyed:

uncaught typeerror: cannot read property 'destroy' of undefined

any help on appreciated!

i found solution:

_.invoke(tasks.toarray(), 'destroy');

apparently, using .each destroy models bad practice, since internal iteration gets messed continual deletion of models.

backbone.js backbone-local-storage

No comments:

Post a Comment