javascript - Is it a code smell if I have the need to save a Backbone.Collection? -


i've been trying wrap head around best restful practices while using backbonejs. feel i've written myself bit of knot , use guidance.

my scenario this: user wants create new playlist n items in it. data n items coming third-party api in bursts of 50 items. such, want add new, empty playlist and, bursts of 50 come in, save items , add playlist.

this results in playlist model having method, additems, looks like:

additems: function (videos, callback) {     var itemstosave = new playlistitems();     var self = this;      //  create new playlistitem each video.     videos.each(function (video) {          var playlistitem = new playlistitem({             playlistid: self.get('id'),             video: video         });          itemstosave.push(playlistitem);     });      itemstosave.save({}, {         success: function () {              //  oof terrible.             self.fetch({                 success: function () {                     //  todo: reason when call self.trigger allplaylists triggers fine, if go through fetch doesnt trigger?                     self.trigger('reset', self);                      if (callback) {                         callback();                     }                  }             });          },         error: function (error) {             console.error("there issue saving" + self.get('title'), error);         }     }); } 

itemstosave collection 50 items in it. since backbonejs not provide save collections, wrote own. didn't care creating model wrapper collection.

so, when call save, none of items have ids. database assigns ids, information isn't implicitly updated backbone because i'm saving collection , not model. such, once save successful, call fetch on playlist retrieve updated information. terrible because playlist have thousands of items in -- don't want fetching thousands of items every time save multiple.

so, i'm thinking maybe need override collection's parse method , manually map server's response collection.

this seems... overkill/wrong. doing architecturally incorrect? how restful architecture handle such scenario?

my opinion works , feels clean enough , disregard restafarians credence might be. bulk create, bulk update, bulk delete real world use cases rest folk close eyes , pretend don't exist. along these lines sounds reasonable first attempt me:

  • create bulkadd method or override add if feeling confident
  • don't make models or add them collection yet though
  • do bulk post or whatever them database , assigned ids back
  • then add them models collection

Comments