Skip to content

Instantly share code, notes, and snippets.

@kwhinnery
Created December 20, 2011 19:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwhinnery/1503005 to your computer and use it in GitHub Desktop.
Save kwhinnery/1503005 to your computer and use it in GitHub Desktop.
//Wrapper class to create extensible Titanium components
function Component(/*Object*/ tiView) {
var self = {
__viewProxy:tiView
};
//passthrough for add
self.add = function(/*Object*/ tiChildView) {
var v = tiChildView.__viewProxy||tiChildView;
self.__viewProxy.add(v);
};
//passthrough for remove
self.remove = function(/*Object*/ tiChildView) {
var v = tiChildView.__viewProxy||tiChildView;
self.__viewProxy.remove(v);
};
//passthrough for open
self.open = function(args) {
if (self.__viewProxy.open) {
self.__viewProxy.open(args||{animated:false});
}
};
self.close = function() {
if (self.__viewProxy.close) {
self.__viewProxy.close();
}
};
//set/get properties on the view proxy
self.set = function(k,v) {
self.__viewProxy[k] = v;
};
self.get = function(k) {
return self.__viewProxy[k];
}
//passthrough for animation
self.animate = function(args,cb) {
self.__viewProxy.animate(args,cb||function(){});
};
//passthrough and shorthand for events
self.on = function(name,cb) {
self.__viewProxy.addEventListener(name,cb);
};
self.fire = function(name,e) {
self.__viewProxy.fireEvent(name,e||{});
}
//memory management
self.onDestroy = function(){};
self.release = function() {
self.__viewProxy = null;
self.onDestroy();
};
return self;
}
module.exports = Component;
var Component = require('Component');
var wrapped = new Component(Ti.UI.createView({
backgroundColor:'red'
}));
wrapped.set('top',5);
wrapped.set('left',5);
wrapped.on('click', function() {
alert('clicked');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment