I have great success using the CommonJS engine to compile, to represent the root view, which then has os-specific features.
For example, my os-independent view might be ui / MyView.js :
var createAddButton = require("ui/MyView.AddButton"); var MyView = function() { var self = Ti.UI.createWindow(); createAddButton(self, function() { alert('ADD!'); }); return self; }; module.exports = MyView;
Then I create os-specific functions to handle it:
iphone / / MyView.AddButton.js
module.exports = function(view, addHandler) { var addButton = Titanium.UI.createButton({ systemButton: Titanium.UI.iPhone.SystemButton.ADD }); addButton.addEventListener("click", addHandler); view.rightNavButton = addButton; };
Android / u / MyView.AddButton.js
module.exports = function(view, addHandler) { view.activity.onCreateOptionsMenu = function(e){ var menuItem = e.menu.add({ title: "Add" }); menuItem.addEventListener("click", addHandler); }; };
The CommonJS system that they implemented will select the appropriate version of MyView.AddButton.js so that the button is added to the desired location. This allows most views to be the same, but os-specific things should be separated properly.
source share