backbone.js - Backbone best place for custom event bind & custom even trigger -
i have custom event triggered upon user interaction on bbone view of collection, , i'd signal changes on unrelated view. utilize custom event.
//a global event object pub-sub model var vent = backbone.extend({}, backbone.event); var c1 = backbone.collection.extend(); var v1 = backbone.view.extend( events: { 'click .appointment': 'clickcallback' }, clickcallback: function(){ vent.trigger('appointment:selected', apptprops); } ); var c1obj = new c1([{...}, {...}]); var v1obj = new v1({ collection: c1obj });
so when user clicks on .appointment element of collection view v1obj, announce appointment:selected event.
now want different view v2 react event. best place bind "appointment:selected" event? in initialize() of view v2 (case 1) or in initialize() of collection/model of v2 (case 2) or somewhere else? i'm trying clarify what's best practice, if any.
case 1:
var v2 = backbone.view.extend( initialize: function(){ vent.on('appointment:selected', this.apptselected, this); }, appselected: function(apptprops){ ... } );
case 2:
var c2 = backbone.collection.extend( initialize: function(){ vent.on('appointment:selected', this.apptselected, this); }, appselected: function(apptprops){ ... } );
as far understand, philosophy of bbone upon user interaction, manipulate info , not modify markup, thought beingness info changes cascade views. if so, reply original question case 2?
i think idiomatic thing be:
represent state of "selected" inclusion in collection when user clicks.appointment
, view1 adds appointment model selectedappointments collection view2 binds normal collection lifecycle events (add, remove, reset) of selectedappointments
, re-renders accordingly both views take collection in options argument initialize
just side note of backbone 0.9.9 backbone
object can used application-wide event bus instead of vent
object.
events backbone.js triggers bind
No comments:
Post a Comment