Permalink
Browse files

Lots of updates

  • Loading branch information...
1 parent 1a73860 commit c94a400bcc63d096eb73d32503a5635a84e5fb48 @jawsthegame jawsthegame committed Oct 2, 2012
View
@@ -15,7 +15,7 @@ else
MOCHA=$(mocha)
endif
-export NODE_PATH=./app
+export NODE_PATH=./
xunit:
$(MOCHA) -R xunit $(FILES)
@@ -0,0 +1,75 @@
+Backbone = require 'backbone'
+$ = require 'jqueryify2'
+
+events = require '../lib/events'
+
+
+class Controller extends Backbone.Router
+ views: {}
+ events: {}
+
+ constructor: (opts) ->
+ super
+
+ @history = if (opts? and opts.history?) then opts.history else Backbone.history
+ @_localEl = @_createLocalEl()
+ @_pageEl = $(opts?.el or '<div>')
+
+ @_setupViews()
+ @_setupEventHandling()
+
+ activate: ->
+ @_pageEl.children().detach()
+ @_pageEl.append(@_localEl)
+ @_cleanupChildView()
+ this
+
+ destroy: ->
+ @_cleanupChildView()
+
+ for _, viewName of @views
+ @[viewName].remove()
+
+ for pattern, methodName of @events
+ [viewName, match] = pattern.split '.'
+ view = @[viewName]
+ view.off match
+
+ trackNew: ->
+ @_cleanupChildView()
+ args = (v for k, v of arguments)
+ controllerType = args[0]
+
+ initArgs = args[1..]
+ initArgs.push {el: @_pageEl}
+
+ @_child = new controllerType(initArgs...)
+ @_child
+
+ _createLocalEl: ->
+ $('<div>').addClass(@constructor.name)
+ .append(@layout())
+
+ _render: ->
+ if @layout then @$el.empty().append(@layout())
+ @_setupViews()
+
+ _setupViews: ->
+ selectors = Object.keys(@views)
+ selectors.sort()
+
+ for selector in selectors
+ @_localEl.find(selector).append(@[@views[selector]].el)
+
+ _setupEventHandling: ->
+ for pattern, methodName of @events
+ [viewName, match] = pattern.split '.'
+ method = @[methodName]
+ view = @[viewName]
+ view.on(match, method, this)
+
+ _cleanupChildView: ->
+ @_child?.destroy()
+ @_child = undefined
+
+module.exports = events.track Controller
@@ -0,0 +1,21 @@
+_ = require 'underscore'
+Deferred = require('jqueryify2').Deferred
+
+Controller = require './controller'
+
+
+class FormController extends Controller
+ layout: _.template('<div class="form-view"></div>')
+
+ views:
+ '.form-view': 'formView'
+
+ constructor: (@formView, opts) ->
+ super(opts)
+
+ save: ->
+ @activate()
+ @formView.deferred
+
+
+module.exports = FormController
@@ -0,0 +1,53 @@
+Deferred = require('jqueryify2').Deferred
+Backbone = require 'backbone'
+
+Controller = require './controller'
+LoginView = require '../views/login/login_view'
+NoticeView = require '../views/notice_view'
+User = require '../models/user'
+
+
+class LoginController extends Controller
+ views:
+ '.login-view': 'loginView'
+ '.login-view .notice': 'noticeView'
+
+ events:
+ 'loginView.change': 'checkCredentials'
+
+ routes:
+ 'login': 'login'
+ 'logout': 'logout'
+
+ constructor: (layout, LoginViewClass, opts) ->
+ @layout = layout
+ @whoami = User.fetch
+ @auth = User.authenticate
+
+ @loginView = new LoginViewClass().render()
+ @noticeView = new NoticeView().render()
+
+ super(opts)
+
+ login: ->
+ @deferred = new Deferred
+ @whoami()
+ .done((user) => @deferred.resolve(user))
+ .fail((reason) => @activate())
+
+ @deferred.promise()
+
+ logout: ->
+ User.current?.logout().done =>
+ document.location = '/'
+
+ checkCredentials: (credentials) =>
+ @loginView.disable()
+
+ @auth(credentials.email, credentials.password)
+ .always(@loginView.enable)
+ .done(@deferred.resolve)
+ .fail((msg) => @noticeView.error(msg))
+
+
+module.exports = LoginController
@@ -0,0 +1,28 @@
+_ = require 'underscore'
+Backbone = require 'backbone'
+
+Controller = require './controller'
+NavigationView = require '../views/navigation_view'
+
+
+class NavigationController extends Controller
+ layout: _.template('<div></div>')
+
+ views:
+ 'div': 'navigation'
+
+ constructor: (@user, template, opts) ->
+ @navigation = new NavigationView(@user, template).render()
+ super(opts)
+
+ @history = opts.history or Backbone.history
+ @history.on('route', @navigated, this)
+
+ destroy: ->
+ @history.off 'all'
+ super
+
+ navigated: ->
+ @navigation.updateSecondary(document.location.hash)
+
+module.exports = NavigationController
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,36 @@
+_ = require 'underscore'
+
+bind = ->
+ throw Error('bind() is deprecated, use on()')
+
+register = (events, callback, to, from) ->
+ unless to? then throw new Error('context required')
+ (to._bindings or= []).push
+ events: events
+ callback: callback
+ context: from
+
+unregister = (instance) ->
+ for b in (instance._bindings or [])
+ b.context.off(b.events, b.callback)
+
+module.exports =
+ bind: bind
+
+ register: register
+
+ unregister: unregister
+
+ track: (cls) ->
+ return cls if cls._events_tracked
+
+ cls::_onOrig = cls::on
+ cls::bind = bind
+ cls::on = (events, callback, to) ->
+ register(events, callback, to, this)
+ this._onOrig(arguments...)
+
+ cls::unregister = -> unregister(this)
+ cls._events_tracked = true
+
+ cls
View
@@ -0,0 +1,3 @@
+%ul.errors
+ - for msg in @errors
+ %li= msg
View
@@ -0,0 +1,49 @@
+require './date'
+
+
+date = (dateString) ->
+ date = new Date(dateString)
+ formattedDate = date.toString('M/d/yyyy')
+ if formattedDate.indexOf('NaN') is -1
+ formattedDate
+ else
+ ''
+
+dateTime = (dateTimeString) ->
+ date = new Date(dateTimeString)
+ formattedDate = date.toString('M/d/yyyy h:mm tt')
+ if formattedDate.indexOf('NaN') is -1
+ formattedDate
+ else
+ ''
+
+boolean = (value) ->
+ if not value?
+ ' - '
+ else if value
+ 'Yes'
+ else
+ 'No'
+
+
+money = (number) ->
+ "$#{formatNumber(number, 2)}"
+
+number = (number) ->
+ formatNumber(number, 0)
+
+formatNumber = (number, places) ->
+ s = if number < 0 then "-" else ""
+ i = parseInt(number = Math.abs(+number || 0).toFixed(places)) + ""
+ j = if (j = i.length) > 3 then j % 3 else 0
+
+ return s + (if j then i.substr(0, j) + ',' else "") +
+ i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + ',') +
+ (if places then "." + Math.abs(number - i).toFixed(places).slice(2) else "")
+
+module.exports =
+ date: date
+ dateTime: dateTime
+ boolean: boolean
+ money: money
+ number: number
Oops, something went wrong.

0 comments on commit c94a400

Please sign in to comment.