Thursday, 22 August 2013

Application object triggers custom event twice? Marionette v1.1.0

Application object triggers custom event twice? Marionette v1.1.0

I've a collection Days actually shared by 2 collections - collection view
v1 for adding or removing days (edit) and composite view v2 that displays
clickable links to navigate to the individual day's itemViews.
AppointmentManager = new Marionette.Application //app object.
To delete say appointments for day 2, user clicks on the delete button on
dayItemView2 in collectionview v1, which also causes the day to be deleted
from collection view v1, like so:
onDeleteDayClicked() {
this.model.collection.remove(this.model);
}
This deletion also gets reflected in the view v2, as collection is shared
across these two views. Day 2's navigation link is deleted from v2
automatically by Marionette.
The collection Days listens on this remove event in initialize. To ensure
that these changes are reflected and saved serverside (along with other
bits of info stored as part of the larger model that also stores
collection of days), I trigger on the application manager a save event:
Entities.Days = Backbone.Collection.extend({
initialize: function(options) {
this.on("remove", function(model,collection,index) {
AppointmentManager.trigger("appts:save");
}
});
However in my ApptController, I receive the apps:save event twice. I've
checked that collection Days receives only one delete event and only one
model is deleted , hence appts:save trigger is called ones.
ApptManager.listenTo(ApptManager, "appts:save", function() {
console.log("Saving appts!");
appts.set("days", days);
appts.save();
});
"Saving appts" gets printed twice and appts PUT twice!
Any clues?!
Using Marionette v1.1.0. I've got other modules and apps that display
header navbar view and other pages. My application structure is based on
Backbone.Marionette.js: A Gentle Introduction by David Sulc - although I
doubt if this packaging structure is of any relevance to this problem.

No comments:

Post a Comment