Skip to content

Instantly share code, notes, and snippets.

@doublerebel
Created March 8, 2013 16:55
Show Gist options
  • Save doublerebel/5117948 to your computer and use it in GitHub Desktop.
Save doublerebel/5117948 to your computer and use it in GitHub Desktop.
Example of Tiger routing in plain JS (for Titanium Mobile)
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');
// create tab group
var tabGroup = Titanium.UI.createTabGroup();
//
// create base UI tab and root window
//
var win1 = Titanium.UI.createWindow({
title:'Tab 1',
backgroundColor:'#fff'
});
var tab1 = Titanium.UI.createTab({
icon:'KS_nav_views.png',
title:'Tab 1',
window:win1
});
var label1 = Titanium.UI.createLabel({
color:'#999',
text:'I am Window 1',
font:{fontSize:20,fontFamily:'Helvetica Neue'},
textAlign:'center',
width:'auto'
});
win1.add(label1);
//
// create controls tab and root window
//
var win2 = Titanium.UI.createWindow({
title:'Tab 2',
backgroundColor:'#fff'
});
var tab2 = Titanium.UI.createTab({
icon:'KS_nav_ui.png',
title:'Tab 2',
window:win2
});
var label2 = Titanium.UI.createLabel({
color:'#999',
text:'I am Window 2',
font:{fontSize:20,fontFamily:'Helvetica Neue'},
textAlign:'center',
width:'auto'
});
win2.add(label2);
//
// add tabs
//
tabGroup.addTab(tab1);
tabGroup.addTab(tab2);
// open tab group
tabGroup.open();
var Tiger = require('/lib/tiger'),
Route = require('/lib/tiger.route');
var routeController = new Tiger.Controller();
routeController.routes({
'/window/:id': function(params) {
tabGroup.setActiveTab(params.id - 1);
},
'/alert/:message': function(params) {
alert(params.message);
},
});
Tiger.Route.setup({ history: true });
win1.addEventListener('androidback', routeController.back);
Tiger.Route.bind('navigate', function() { routeController.debug('navigating path: ' + Tiger.Route.path); });
Tiger.Route.History.bind('change', function() { routeController.debug('path: ' + Tiger.Route.path); });
label1.addEventListener('click', function() { routeController.navigate('/window/2'); });
label2.addEventListener('click', function() { routeController.navigate('/window/1'); });
label2.addEventListener('longpress', function() { routeController.navigate('/alert/longpress'); });
@stongo
Copy link

stongo commented Mar 14, 2013

To clarify, I saw in your examples you pass the arguments write in the navigate string, but I was hoping to be able to pass objects/arrays as arguments.
The spine.js documentation allows passing arguments like navigate(route, args)

@stongo
Copy link

stongo commented Mar 14, 2013

Figured it out. Had to use globs in route path ... 'helloworld/_string/_destination'
Updated my gist with working code now

@stongo
Copy link

stongo commented Mar 14, 2013

hrm, only somewhat works. combines the arguments in to one parameter sometimes ...

@doublerebel
Copy link
Author

You can stringify the array before navigating, and then parse it in the router. Here is an example from one of my apps:

Controller:

@navigate '/invoices', @invoice_id, "addproducts", (JSON.stringify @selected)

Router:

  '/invoices/:id/addproducts/*list': (params) =>
    @Invoice or= require '/controllers/invoice'
    @Controller = new @Invoice(params.id)
    @Controller.addProduct JSON.parse params.list

Stringifying isn't awesome for performance, but it does limit routes to reasonable complexity (like with URLs on the web), and is better than sending an app-level event -- Ti.App events stringify the event object and pass it over the "Kroll bridge" through Java and back.

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