Skip to content

Instantly share code, notes, and snippets.

@ashishgupta2
Forked from mxriverlynn/1-referencing.js
Created June 3, 2014 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashishgupta2/2b706220223564ad5199 to your computer and use it in GitHub Desktop.
Save ashishgupta2/2b706220223564ad5199 to your computer and use it in GitHub Desktop.
MedicationView = Backbone.View.extend({
events: {
"click #edit": "editMedication"
},
editMedication: function(){
var editView = new AddEditView({model: this.model});
editView.render();
}
});
MedicationView = Backbone.View.extend({
events: {
"click #edit": "editMedication"
},
initialize: function(options){
this.addEditView = options.addEditView;
}
editMedication: function(){
this.addEditView.render();
}
});
// initialize everything here...
// 'medicationList' is a collection of medications
var addEditView = new AddEditView(...);
medicationlist.each(function(med){
new MedicationView({model: med, addEditView: addEditView});
}
MedicationRouter = Backbone.Router.extend({
routes: {
"edit/:id": "editMedication"
},
editMedication: function(id){
var med = medicationList.get(id);
this.addEditView.editMedication(med);
}
});
AddEditView = Backbone.View.extend({
editMedication: function(medication){
this.model = medication;
this.render();
}
});
var vent = _.extend({}, Backbone.Events);
AddEditView = Backbone.View.extend({
initialize: function(options){
_.bindAll(this, "editMedication");
options.vent.bind("editMedication", this.editMedication);
},
editMedication: function(medication){
this.model = medication;
this.render();
}
});
MedicationView = Backbone.View.extend({
events: {
"click #edit": "editMedication"
},
initialize: function(options){
this.vent = options.vent;
},
editMedication: function(){
this.vent.trigger("editMedication", this.model);
}
});
// initialize everything, and tie it all together
// with the event aggregator object: vent
var vent = _.extend({}, Backbone.Events);
var addEditView = new AddEditView({vent: vent});
medicationList.each(function(med){
new MedicationView({model: med, vent: vent});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment