backbone.js - low coupling: add a model to a collection of a different view -


i'm building backbone/marionette application list different sets of cards. layout has itemview on left side including input field add new set , compositeview on right side list card sets.

cards.module("set.sidebar", function(sidebar, app) {     sidebar.sidebarview = backbone.marionette.itemview.extend({         template: "#set-sidebar",         classname: "well sidebar-nav",         ui: {             savebtn: "a.saveset",             setname: "input[type=text]"         },         events: {             "click .saveset": "saveset"         },         saveset: function(ev) {             ev.preventdefault();              var newsetname = this.ui.setname.val().trim();             var newset = new cards.entities.set({ name: newsetname });              newset.save();              // how add model collection?         }     }); }); 

i'm looking best way add newset collection of compositeview below. there clean low coupling solution deal that? i'm quite new backbone.js , can't imagine totally unordinary, somehow i'm not able find answer question in regarding docs - or dont understand them.

cards.module('set.list', function(list, app) {     list.setitemview = backbone.marionette.itemview.extend({         tagname: "tr",         template: "#set-list-item"     });  list.setview = backbone.marionette.compositeview.extend({         tagname: "table",         classname: "table table-bordered table-striped table-hover",         template: "#set-list",         itemview: list.setitemview,         itemviewcontainer: "tbody",         modelevents: {             "change": "modelchanged"         },          initialize: function() {             this.collection.fetch();         }     }); }); 

thanks in advance help!

how i'm doing now:

thanks both answers, guiding me in right direction. collection.create hint useful , solved problem facing!

inside marionette.controller , share collection reference:

var setlayout = new cards.set.layout(); cards.mainregion.show(setlayout);  var sets = new cards.entities.setcollection(); var listview = new cards.set.list.setview({ collection: sets }); setlayout.listregion.show(listview);  var sidebarview = new cards.set.sidebar.sidebarview({ collection: sets }); setlayout.sidebarregion.show(sidebarview); 

and new model added collection.create instead of .save() , .add().

backbone.collection.add can used add model existing backbone collection. http://backbonejs.org/#collection-add

also, in collection.create - http://backbonejs.org/#collection-create

if model being persisted, added collection, can skip model.save() collection.add() , use collection.create(model)

edit: , mentioned, make collection instance visible sidebar view


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -