Skip to content

Commit

Permalink
wrap form data in FormData service
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Martin committed Feb 2, 2014
1 parent f25abf2 commit 1cb48a3
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions app/assets/javascripts/authors.js.coffee
@@ -1,21 +1,23 @@
do (angular)->

class FormController
constructor: (@$scope, @$http)->
constructor: (@$scope, @$http, FormData)->
$scope.data = FormData
$scope.submitForm = ->
$http.post('/authors', author:{name:$scope.name, email:$scope.email})
$http.post('/authors', author:$scope.data)
return false

class ArticlesController
constructor: (@$scope)->
$scope.articles = [{}]
constructor: (@$scope, FormData)->
$scope.articles = FormData['articles']
$scope.addArticle = ->
$scope.articles.push( {} )
$scope.removeArticle = (index)->
$scope.articles.splice(index, 1)

angular.module 'authorsApp', [
'authorsApp.controllers',
'authorsApp.services',
'authorsApp.directives'
]

Expand All @@ -28,8 +30,8 @@ do (angular)->
])

angular.module('authorsApp.controllers',[])
.controller( 'FormController', ['$scope','$http', FormController] )
.controller( 'ArticlesController', ['$scope', ArticlesController] )
.controller( 'FormController', ['$scope','$http', 'FormData', FormController] )
.controller( 'ArticlesController', ['$scope', 'FormData', ArticlesController] )

angular.module('authorsApp.directives', [])
.directive('peSubmit', (@$compile)->
Expand All @@ -40,3 +42,10 @@ do (angular)->
element.replaceWith(@e)
}
)
class FormData
constructor: (data)->
formData = data or {articles:[{}]}
return formData

angular.module('authorsApp.services', [])
.factory('FormData', [FormData])

0 comments on commit 1cb48a3

Please sign in to comment.