javascript - How to update the whole Backbone.js collection that in database? -
i have collection (an object list) in database. can fetch like: collectionmodel.fetch() user changes on collection. when user clickes on save button, whole collection list must update in database. thought maybe can delete() old 1 first , create() new 1 could'n achive it. can't use update() method because in case should find collection elements has changed want update whole list. how can that? help.
do have rest api in front of database? that's how backbone made work with. when javascript code runs model.save(); put request made api model.
you question saving whole collection, if want remain within default implementation of backbone have go on models in collection , call save each of them.
if want make 1 single request server have implement custom method inside collection. like:
mycollection = backbone.collection.extend({ saveall: function() { var data = this.tojson(); return backbone.$.ajax({ data: { objects: data }, url: '/url/in/your/server/to/update/db' }); } }); that's going send array of models in collection converted json server.
again, want have restful api on server side if want make life backbone easy.
Comments
Post a Comment