如何在订阅功能中取消订阅放大?

时间:2012-11-15 17:23:59

标签: javascript caching store subscribe

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

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

});

getDropdownExpenseWorkTypes(JobNo_);

我的getDropdownExpenseWorkTypes调用一个在"WorkTypesReceived"完成后发布的函数。但由于我在一个页面上不止一次调用这个函数,它会以指数方式爆炸(对同一主题的多个订阅)。我想删除订阅一旦发布一次(因此进入订阅功能一次)。

amplify.unsubscribe("WorkTypesReceived")似乎不起作用,文档说我需要第二个参数作为回调函数。但我在回调函数中,所以不确定如何继续。

1 个答案:

答案 0 :(得分:3)

如果您为回调添加名称,则可以使用该名称取消订阅...

amplify.subscribe( "WorkTypesReceived", function storeWork() {
    var workTypesList = amplify.store( "ExpenseWorkTypesFor" + JobNo_ );
    amplify.unsubscribe( "WorkTypesReceived", storeWork );
});