我的应用上有以下代码来添加小部件:
while(i < rc_length) {
console.log(i);
mooncards[i] = Alloy.createWidget("moonCards");
mooncards[i].on('close',function(){
$.dashboard_scroll.remove(mooncards[i].getView());
});
$.dashboard_scroll.add(mooncards[i].getView());
i++;
}
所以我可以在我的mooncards
上添加scrollview
并添加一个要在窗口小部件内触发的功能以自行删除。
这就是想法,但不幸的是,删除的唯一小部件是最后一个。显然,添加新窗口小部件时引用remove(mooncards[i])
会丢失。
我还在学习Javascript,所以我不知道我在这里做错了什么。
如何在不丢失引用的情况下添加大量小部件并专门删除每个小部件?
请让我知道如果我需要更清楚。
答案 0 :(得分:0)
您有一个经典的javascript绑定问题。
我会尝试改变:
{"verify":{"otp":847042,"id":44}}
到
$.dashboard_scroll.remove(mooncards[i].getView());
答案 1 :(得分:0)
您可以使用bind
:
mooncards[i].on('close',function(){
$.dashboard_scroll.remove(this.getView());
}.bind(mooncards[i]));
bind
会将您的函数中的this
替换为您提供给它的第一个参数。考虑这个例子:
x = function() { console.log(this); }
// outputs the window context if running in browser
// because the value of 'this' is the context where
// where the function was executed
x();
// outputs a String object, 'hello' because the value of
// this has now been bound to the string 'hello'
x.bind('hello')();
如果您的用户位于IE8及以下版本,则需要使用上面链接中提供的polyfill。