Nilesh Kale
Nilesh Kale

Reputation: 233

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.
//sharing collection days among two views v1, v2.
var days = new Entities.Days(ApptModel.get("apptDays"));
var v1 = new EditCollectionView({collection: days});
var v2 = new ListCompositeView({collection: days});

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.

Upvotes: 1

Views: 632

Answers (1)

Tigra
Tigra

Reputation: 2631

How exactly you sharing your collection?

Add console log to your collection initialize
Probably intitialize method called twice

Upvotes: -1

Related Questions