Skip to content

Instantly share code, notes, and snippets.

@mauropm
Last active August 29, 2015 13:56
Show Gist options
  • Save mauropm/9200453 to your computer and use it in GitHub Desktop.
Save mauropm/9200453 to your computer and use it in GitHub Desktop.
Script showing how to clean a view inside a window (like a scrollview)
// INcluding memory management utils.
Ti.include('utils.js');
// root window.
var win = Ti.UI.createWindow({
backgroundColor : 'white',
exitOnclose : true,
});
if (Ti.UI.Android) {
win.windowSoftInputMode = Ti.UI.Android.SOFT_INPUT_ADJUST_PAN;
}
function createRow(i) {
var row = Ti.UI.createView({
backgroundColor : 'white',
borderColor : '#bbb',
borderWidth : 1,
width : '100%',
height : 70,
top : 0,
left : 0
});
var inputTextField = Ti.UI.createTextField({
hintText : 'Enter value ' + i,
keyboardType : Ti.UI.KEYBOARD_NUMBERS_PUNCTUATION,
top : 10,
left : '10%',
width : '80%',
height : 60
});
row.add(inputTextField);
return row;
}
var label = Ti.UI.createLabel({
backgroundColor : 'darkgray',
text : 'remove',
textAlign : 'center',
bottom : 0,
left:0,
width : Titanium.UI.FILL / 2,
height : 100
});
var label2 = Ti.UI.createLabel({
backgroundColor : 'darkgray',
text : 'create',
textAlign : 'center',
bottom : 0,
left : Titanium.UI.FILL / 2 + 1,
width : Titanium.UI.FILL / 2,
height : 100
});
win.add(label);
win.add(label2);
var scrollView;
label.addEventListener("click", function() {
win.remove(scrollView);
Ti.API.info(scrollView.getChildren().length);
alert(scrollView.getChildren().length);
clean(scrollView);
scrollView = null;
});
label2.addEventListener('click', function() {
scrollView = Ti.UI.createScrollView({
bottom : 120,
contentHeight : 'auto',
layout : 'vertical'
});
Ti.API.info(scrollView.getChildren().length);
alert(scrollView.getChildren().length);
for (var i = 0; i <= 20; i++) {
var row = createRow(i);
scrollView.add(row);
}
win.add(scrollView);
});
win.open();
/// Recursive Clean of Memory
// by Mauro Parra
// https://gist.github.com/mauropm/2655813
// Begin recursive clean of memory
function do_clean(e, c) {
clean(c);
e.remove(c);
c = null;
Ti.API.info('Deleted child at do_clean');
return;
}
function clean(e) {
if (e != null) {
if (e.children) {
Ti.API.info('Number of children: ' + e.children.length);
for (var i = 0; i < e.children.length; i++) {
do_clean(e, e.children[0]);
}
} else {
return;
}
}
}
// End recursive clean of memory
/// <<< Register & UnRegister Event Listeners
// Original from: https://gist.github.com/minhnc/2333095
// Thanks minhnc!
/**
* params: {event: 'event', callback: eventCallback}
*/
function registerEventListener(obj, params) {
if ( typeof obj._eventListeners == 'undefined') {
obj._eventListeners = [];
}
obj.addEventListener(params.event, params.callback);
var eventListeners = obj._eventListeners;
eventListeners.push(params);
obj._eventListeners = eventListeners;
Ti.API.info(JSON.stringify(obj._eventListeners));
}
function unRegisterAllEventListeners(obj) {
if ( typeof obj._eventListeners == 'undefined' || obj._eventListeners.length == 0) {
return;
}
for (var i = 0, len = obj._eventListeners.length; i < len; i++) {
var e = obj._eventListeners[i];
obj.removeEventListener(e.event, e.callback);
}
obj._eventListeners = [];
}
/// Register & UnRegister Event Listeners >>>
// Begin win2 clean (all secondary windows are named win2)
// Function to clean the win2 (i.e., remove all the attached listeners, close it, null it)
function win2Clean() {
if (win2 != null) {
win2.close();
clean(win2);
unRegisterAllEventListeners(win2);
win2 = null;
}
Ti.App.removeEventListener('win2:close', win2Clean);
}
function win2Close() {
Ti.App.fireEvent('win2:close');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment