这是我对所有控制器和小部件的清理方法:
someview.js
var args = arguments[0] || {},
data = {};
data.button = Alloy.createController('button',{
title:'button'
}).getView();
$.view.cleanup = function() {
$.destroy();
$.off();
data.button.cleanup();
$ = data = args = null;
};
按钮.js
var args = arguments[0] || {},
data = {};
data.click = function() { ... };
$.view.addEventListener('click',data.click);
$.view.cleanup = function() {
$.destroy();
$.off();
$.view.removeEventListener('click',data.click);
$ = data = args = null;
};
我创建了一个全局函数,调用 cleanup 方法并在控制器的每个子节点上删除它的内容:
更新
添加删除运算符和尝试语句
exports.unset = function(view) {
if(view) {
if(view.children && view.children.length) {
for(var i in view.children) try { util.unset(view.children[i]);
} catch(e) {}
view.removeAllChildren();
}
if(view.views && view.views.length) for(var i = view.views.length; i > 0; i--) if(view.views[i-1]) {
if(view.removeView) view.removeView(i-1);
try { util.unset(view.views[i-1]);
} catch(e) {}
}
if(view.cleanup) try { view.cleanup();
} catch(e) {}
view = null;
delete view;
}
};