Question

amplify.subscribe("WorkTypesReceived", function () {

    var workTypesList = amplify.store("ExpenseWorkTypesFor" + JobNo_);
    amplify.unsubscribe("WorkTypesReceived");

});

getDropdownExpenseWorkTypes(JobNo_);

My getDropdownExpenseWorkTypes calls a function that publishes "WorkTypesReceived" when it is complete. But since I call this entire function more than once on a single page, it exponentially explodes (multiple subscriptions to the same topic). I'd like to remove the subscription once it's been published once (and so goes into the subscribe function once).

The line amplify.unsubscribe("WorkTypesReceived") doesn't seem to work, and the documentation says I need a second parameter being the callback function. But I'm inside the callback function so unsure how to proceed.

Était-ce utile?

La solution

If you add a name to your callback you can use that to unsubscribe from it...

amplify.subscribe( "WorkTypesReceived", function storeWork() {
    var workTypesList = amplify.store( "ExpenseWorkTypesFor" + JobNo_ );
    amplify.unsubscribe( "WorkTypesReceived", storeWork );
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top