Permalink
Browse files

removing dependencies to angle-up + cleaning up code of controllers

  • Loading branch information...
1 parent c3d97f8 commit 24867ea9e0e6c0adcdfbf806e6dd10921ba5777c @sectore committed Jan 18, 2012
Showing with 40 additions and 183 deletions.
  1. +40 −38 app/assets/javascripts/controllers.coffee.erb
  2. +0 −1 app/views/layouts/application.html.erb
  3. +0 −144 public/javascripts/angle-up.js
@@ -2,32 +2,49 @@
Application Controller
###
-class @CafeTownsendCtrl extends Router
+class @CafeTownsendCtrl
- constructor: ($route, $xhr, @$location, @SessionService, @$log) ->
- super($route, $xhr)
+ constructor: (@$route, @$xhr, @$location, @SessionService, @$log) ->
+ @setupXhr()
+ @initRoutes()
@session = new @SessionService()
# flag for showing / hiding debugging outputs within any views
@debug = false
# routes handling
- routes:->
- {
- default: '/login'
- '/login':
- template: '<%= asset_path("login.html") %>'
+ initRoutes:(routes)->
+
+ @$route.when '/login',
+ template: '<%= asset_path("login.html") %>',
controller: LoginCtrl
- '/employees':
- template: '<%= asset_path("employees.html") %>'
+
+ @$route.when '/employees',
+ template: '<%= asset_path("employees.html") %>',
controller: EmployeesOverviewCtrl
- '/employees/new':
+
+ @$route.when '/employees/new',
template: '<%= asset_path("employee.html") %>'
controller: CreateEmployeeCtrl
- '/employees/:employee_id/edit':
- template: '<%= asset_path("employee.html") %>'
+
+ @$route.when '/employees/:employee_id/edit',
+ template: '<%= asset_path("employee.html") %>',
controller: EditEmployeeCtrl
- }
+ @$route.otherwise( redirectTo: '/login' )
+
+ @$route.parent @
+
+ # xhr header definitions for using Rails based RESTful API
+ # Copied from angular-up 'Router' and 'RailsRouter'
+ # @see: https://github.com/ludicast/angle-up/blob/master/app/src/angle-up.coffee
+ setupXhr:->
+ @$xhr.defaults.headers.post['Content-Type'] = 'application/json'
+ @$xhr.defaults.headers.put['Content-Type'] = 'application/json'
+
+ if token = $("meta[name='csrf-token']").attr("content")
+ @$xhr.defaults.headers.post['X-CSRF-Token'] = token
+ @$xhr.defaults.headers.put['X-CSRF-Token'] = token
+ @$xhr.defaults.headers['delete']['X-CSRF-Token'] = token
###
helper methods
@@ -41,14 +58,12 @@ class @CafeTownsendCtrl extends Router
getCurrentParams:->
@$route.current.params
-
CafeTownsendCtrl.$inject = ['$route', '$xhr', '$location', 'SessionService', '$log']
###
Controller for handling log in an user
###
-
class @LoginCtrl
login: ->
@@ -60,7 +75,6 @@ class @LoginCtrl
else
@message = "Invalid username or password!"
-
loginErrorHandler:(error)->
@message = "Error: " + error
@@ -74,21 +88,19 @@ Controller for handling log out an user
###
class @LogoutCtrl
-
logout: ->
if @authorized()
@session.$logout(user_id: @session.user.id, @logoutResultHandler, @logoutErrorHandler)
logoutResultHandler:->
- # a succesfull log out results in {"authorized":"false"} sent from server side
+ # a succesfull log out results in {"authorized":"false"} sent from server side
if !@authorized()
@$location.path 'login'
logoutErrorHandler:(error)->
alert "Error trying to log out (error: " + error + ")"
-
###
Abstract Controller for handling employees
Nnote:
@@ -100,19 +112,18 @@ class @AbstractEmployeesCtrl
constructor: (@EmployeesService) ->
@handleNonAuthorizedAccess()
@selectedEmployee = null
- @EmployeesService = EmployeesService
###
RegEx for date format YYYY-MM-DD:
@see: http://www.regular-expressions.info/regexbuddy/datemmddyyyy.html
###
@datePattern = /(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])/
###
- deleting an employee
+ deleting an employee
###
delete:->
- # non-GET instance actions: instance.$action([parameters], [success], [error])
- # @see http://docs.angularjs.org/#!/api/angular.service.$resource
+ # non-GET instance actions: instance.$action([parameters], [success], [error])
+ # @see http://docs.angularjs.org/#!/api/angular.service.$resource
if @selectedEmployee?
@EmployeesService.delete(employee_id: @selectedEmployee.id, @deleteResultHandler, @deleteErrorHandler) if confirm('Are you sure you want to delete ' + @selectedEmployee.first_name + ' ' + @selectedEmployee.last_name + '?')
@@ -123,7 +134,6 @@ class @AbstractEmployeesCtrl
deleteErrorHandler:(error)->
alert "Error trying to delete an employee (error: " + error + ")"
-
###
triggering view changes
###
@@ -146,7 +156,6 @@ class @EmployeesOverviewCtrl extends AbstractEmployeesCtrl
super(EmployeesService)
@getEmployees()
-
getEmployees:->
@employees = @EmployeesService.query()
@@ -165,18 +174,13 @@ Controller to edit an employee
###
class @EditEmployeeCtrl extends AbstractEmployeesCtrl
-
- constructor: (EmployeesService, @$route) ->
+ constructor: (EmployeesService) ->
super(EmployeesService)
# Note:
# @getCurrentParams() is defined within CafeTownsendCtrl
# and here available provided by Angular (by extending classes within same scope)
id = @getCurrentParams().employee_id
-
- @selectedEmployee = @EmployeesService.get(
- employee_id: id
- )
-
+ @selectedEmployee = @EmployeesService.get(employee_id: id)
@isCreateForm = false
submit:->
@@ -191,8 +195,7 @@ class @EditEmployeeCtrl extends AbstractEmployeesCtrl
updateErrorHandler:(error)->
alert "Error updating an employee (error: " + error + ")"
-
-EditEmployeeCtrl.$inject = ['EmployeesService', '$route']
+EditEmployeeCtrl.$inject = ['EmployeesService']
###
@@ -209,8 +212,8 @@ class @CreateEmployeeCtrl extends AbstractEmployeesCtrl
@save()
save: ->
- # non-GET instance actions: instance.$action([parameters], [success], [error])
- # @see: http://docs.angularjs.org/#!/api/angular.service.$resource
+ # non-GET instance actions: instance.$action([parameters], [success], [error])
+ # @see: http://docs.angularjs.org/#!/api/angular.service.$resource
@selectedEmployee.$save(employee: @selectedEmployee, @saveResultHandler, @saveErrorHandler)
saveResultHandler:->
@@ -219,5 +222,4 @@ class @CreateEmployeeCtrl extends AbstractEmployeesCtrl
saveErrorHandler:(error)->
alert "Error trying to save a new employee (error: " + error + ")"
-
CreateEmployeeCtrl.$inject = ['EmployeesService']
@@ -42,7 +42,6 @@
<script src="/javascripts/angular-0.10.5.min.js" ng:autobind></script>
-<script src="/javascripts/angle-up.js"></script>
<%= javascript_include_tag "application" -%>
</body>
Oops, something went wrong.

0 comments on commit 24867ea

Please sign in to comment.