Created
June 12, 2012 22:26
-
-
Save kwhinnery/2920512 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//theoretical manual usage | |
var ExampleComponent = require('ExampleComponent.gen.js'); | |
var instance = new ExampleComponent(); | |
var w = Ti.UI.createWindow(); | |
instance.addTo(w); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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'); | |
}); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//controller file | |
this.v2.addEventListener('click', function() { | |
$.log('added listener, using a static function on the Component base object'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"foo": { | |
"height":40, | |
"width":80 | |
}, | |
"bar": { | |
"width":150 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<View layout="vertical"> | |
<View id="v1"> | |
<Button class="foo"></Button> | |
</View> | |
<View id="v2" class="foo bar"></View> | |
</View> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
problem with this is that ExampleComponent.prototype is broken. Might need to rethink how this is structured, but this is the basic idea.