tiger
Version:
A full port of Spine.js MVC framework to Titanium Mobile, with enhancements
137 lines (99 loc) • 3.2 kB
text/coffeescript
Tiger = or require './tiger'
namedParam = /:([\w\d]+)/g
splatParam = /\*([\w\d]+)/g
escapeRegExp = /[-[\]{}()+?.,\\^$|#\s]/g
class Tiger.Route extends Tiger.Class
Tiger.Events
: []
:
trigger: true
history: false
backwards: false
: (path, callback) ->
if (typeof path is 'object' and path not instanceof RegExp)
for key, value of path
else
.push(new @(path, callback))
: (options = {}) ->
= Tiger.extend({}, , options)
= .history
if
class History extends Tiger.Model
'History', 'uri'
: ->
.length and .destroy()
History.bind('refresh change', )
= History
: ->
.unbind('refresh change', ) if
: (args...) ->
options = {}
lastArg = args[args.length - 1]
if typeof lastArg is 'object'
options = args.pop()
else if typeof lastArg is 'boolean'
options.trigger = args.pop()
options = Tiger.extend({}, , options)
= options.backwards
path = args.join('/')
return if is path
= path
if options.trigger
if
record = new uri:
record.save()
# Private
: ->
path = .last()?.uri or ''
if path.substr(0,1) isnt '/'
path = '/' + path
path
: (record, method) ->
if then path = else return
return if is path
= true if method is 'destroy'
= path
: (path, options) ->
for route in
if route.match(path, options)
return route
constructor: (, ) ->
= []
if typeof path is 'string'
namedParam.lastIndex = 0
while (match = namedParam.exec(path)) != null
.push(match[1])
splatParam.lastIndex = 0
while (match = splatParam.exec(path)) != null
.push(match[1])
path = path.replace(escapeRegExp, '\\$&')
.replace(namedParam, '([^\/]*)')
.replace(splatParam, '(.*?)')
= new RegExp('^' + path + '$')
else
= path
match: (path, options = {}) ->
match = .exec(path)
return false unless match
options.match = match
params = match.slice(1)
if .length
for param, i in params
options[[i]] = param
.call(null, options) isnt false
# Coffee-script bug
Tiger.Route.change = Tiger.Route.proxy(Tiger.Route.change)
Tiger.Controller.include
route: (path, callback) ->
Tiger.Route.add(path, )
routes: (routes) ->
for key, value of routes
navigate: ->
Tiger.Route.navigate.apply(Tiger.Route, arguments)
back: ->
Tiger.Route.History.back() if Tiger.Route.history
module?.exports = Tiger.Route