Skip to content

Instantly share code, notes, and snippets.

@kwhinnery
Created June 12, 2012 22:26
Show Gist options
  • Save kwhinnery/2920512 to your computer and use it in GitHub Desktop.
Save kwhinnery/2920512 to your computer and use it in GitHub Desktop.
//theoretical manual usage
var ExampleComponent = require('ExampleComponent.gen.js');
var instance = new ExampleComponent();
var w = Ti.UI.createWindow();
instance.addTo(w);
//Component Base
var Base = {
log: function(str) {
Ti.API.info(str);
}
};
//construct a new component object
Base.create = function(funcs) {
var context = {
addTo: function(parentProxy) {
parentProxy.add(context.root);
}
};
//initialize the component
var constructor = function() {
funcs.construct.call(context);
funcs.control.call(context);
};
return constructor;
};
module.exports = Base;
//Generated Component Module
var $ = require('ComponentBaseClass');
module.exports = $.create({
//create view hierarchy
construct: function() {
//generated code from view XML which returns a Titanium view proxy. Might be cool to inline the XML, for debug?
/*
<View layout="vertical">
<View id="v1">
<Button class="foo"></Button>
</View>
<View id="v2" class="foo bar"></View>
</View>
*/
//Create named components
this.root = Ti.UI.createView({
layout:'vertical'
});
this.v1 = Ti.UI.createView();
this.v2 = Ti.UI.createView({
height:40,
width:150 //stule from "bar" overrides "foo"
});
//Assemble hierarchy from leaves of the document back to the trunk
this.v1.add(Ti.UI.createButton({
height:40,
width:80
}));
this.root.add(this.v1);
this.root.add(this.v2);
},
//insturment with controller logic
control: function() {
//this is inserted from controller file
this.v2.addEventListener('click', function() {
$.log('added listener, using a static function on the Component base object');
});
}
});
//controller file
this.v2.addEventListener('click', function() {
$.log('added listener, using a static function on the Component base object');
});
{
"foo": {
"height":40,
"width":80
},
"bar": {
"width":150
}
}
<View layout="vertical">
<View id="v1">
<Button class="foo"></Button>
</View>
<View id="v2" class="foo bar"></View>
</View>
@kwhinnery
Copy link
Author

problem with this is that ExampleComponent.prototype is broken. Might need to rethink how this is structured, but this is the basic idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment