Project Structure / Frameworks
Hi!
For a newbie in Titanium Mobile development, what would you advice in terms of organizing JS codes/files (i.e. folder structures)? Do you follow certain coding standards and practices? Do you use frameworks like MVC?
I think these might help newbies in to how to correctly approach problems in a manageable and maintainable way.
Thanks!
4 Answers
-
I'm currently using a simplified MVC scheme that works pretty well for me. In it the Model, View and Controller files are called in the following way:
model.js
// Defines whatever is the model and its methods var model = {...}
controller.js
Titanium.include ('path/to/model.js'); // Load UI elements Titanium.include ('path/to/view.js'); var win = Titanium.UI.currentWindow; view_init(win); // Define events win.b1.addEventListener('click', function () { var w = Titanium.UI.createWindow({ fullscreen:false, url:'path/to/next_controller.js'}); w.open(); });
view.js
// define the UI elements function view_init(win) { win.backgroundColor = '#fff'; win.layout = 'vertical'; win.orientationModes = [ Titanium.UI.PORTRAIT, Titanium.UI.LANDSCAPE_LEFT, Titanium.UI.LANDSCAPE_RIGHT ]; // Message label win.l1 = Titanium.UI.createLabel({ left:10, width:'auto', height:'auto', color:'#777', font:{fontSize:20,fontFamily:'Helvetica Neue'}, text:'A message' }); win.add(win.l1); // Textfield win.tf1 = Titanium.UI.createTextField({ color:'#336699', height:35, left:10, width:250, borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED }); win.add(win.tf1); // Button win.b1 = Titanium.UI.createButton({ title:'Some verb', height:40, width:200, }); win.add(win.b1); }
With that in place, you can chain the controllers using the actions as in the button b1 above.
While certainly modest, this pattern may help you speeding up your development or prototyping.
-
I work this way:
I separate the "other" files in foldersjs(for utilities js files), images, media
I try to separate as much as possible in separate files the functionality of the application
for example for a book software I made:- app.js - is the main js file( mandatory )
- settings.js for the settings screen
- bookmarks.js is for bookmarks
- authour.js - the author screen
- book folder ->chapters files
- js folder - > utils.js
- images folder -> images for buttons, background, etc
- etc.
I don't use a MVC framework( in my opinion MVC complicates your life too much ) but I developed a wrapper around Titanium that looks more MooTools like ( allows chaining, has set, get, etc.)
-
I have the same question. I just spent all weekend hacking on a new iPhone app, and once I had more than a few lines of code I found control of flow and regression to be a problem. Being a RubyOnRails dev, I found this quite distasteful and slow and bug-prone. I like the convention proposed by @Andre above, and used it for a while and found it very helpful. But still, I can go faster when I have more structure to support me
After searching and reading a lot in this forum and the internet, I've found a many tools, and have chosen the following: MCV by Andrea Ferretti, Jester by Thoughtbot, titanium-jasmine by Guilherme Chapiewski, Jasmine by Davis W. Frank of Pivotal. MCV is a small MVC framework for JS. Jester is for consuming REST-based web services in JS. Titanium-jasmine and Jasmine are for automated unit testing.
I'll let you know how it goes
http://projects.andreaferretti.it/mcv
https://github.com/guilhermechapiewski/titanium-jasmine
http://pivotal.github.com/jasmine/
https://github.com/thoughtbot/jester/wiki UPDATE: this one relies on the DOM, so its a no-go
-
I found helpful tutorial:
http://bradbroulik.blogspot.com/2011/03/titanium-mvc-pattern.html
Without tools, but with clean MVC idea.