Appcelerator-Javascript-丢失引用
我的应用程序中有以下代码可用于添加小部件:Appcelerator-Javascript-丢失引用,javascript,widget,appcelerator,appcelerator-alloy,Javascript,Widget,Appcelerator,Appcelerator Alloy,我的应用程序中有以下代码可用于添加小部件: 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.
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++;
}
while(i
因此,我可以在我的scrollview
上添加mooncards
,并在小部件中添加一个要触发的函数来删除它自己
这是一个想法,但不幸的是,唯一被删除的小部件是最后一个。显然,在添加新部件时,参考remove(mooncards[i])
丢失
我还在学习Javascript,所以我不知道我在这里做错了什么
如何添加大量小部件并删除每个小部件,而不丢失引用?
如果需要更清楚的说明,请告诉我。您遇到了一个典型的javascript绑定问题 我会尝试改变:
$.dashboard_scroll.remove(mooncards[i].getView());
到
您有一个典型的javascript绑定问题 我会尝试改变:
$.dashboard_scroll.remove(mooncards[i].getView());
到
您可以使用:
bind
将用您给它的第一个参数替换函数中的此
。考虑这个例子:
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。您可以使用:
bind
将用您给它的第一个参数替换函数中的此
。考虑这个例子:
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