angular-rails 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +20 -3
- data/lib/angular-rails/version.rb +1 -1
- data/vendor/assets/javascripts/angular-helpers.coffee +32 -14
- metadata +6 -6
data/README.md
CHANGED
@@ -70,16 +70,33 @@ Note that this class will need to be injected with both the $xhr and the $route
|
|
70
70
|
|
71
71
|
PhotoGalleryCtrl.$inject = ['$route', '$xhr']
|
72
72
|
|
73
|
-
This is because it sets us
|
73
|
+
This is because it sets us some CSRF preventions using $xhr as well. Note that this information gets thrown into the controller scope, so `@$xhr` and `@$router` are available in inheriting controllers as well (regardless if inheriting explicitly (through `extends`) or implictly (by being nested in a deeper view tag)).
|
74
|
+
|
75
|
+
Much of this is ripped from a demo by [Daniel Nelson](https://github.com/centresource/angularjs_rails_demo).
|
74
76
|
|
75
77
|
Another thing added is a `resourceService` function. This function is called like:
|
76
78
|
|
77
79
|
resourceService 'Photos', 'photographers/:photographer_id/galleries/:gallery_id/photos', 'index'
|
78
80
|
resourceService 'SelectedPhotos', 'selected_photos/:selected_photo_id'
|
79
81
|
|
80
|
-
This sets up angular services for
|
82
|
+
This sets up angular services for the listed paths. Also add to the end all the actions that you want it to support. So far the accepted actions are 'index', 'update', 'create' and 'destroy'. If you leave off all actions, it will automatically assume that you want to support all 4.
|
83
|
+
|
84
|
+
This helper file adds some features to help parse results.
|
85
|
+
|
86
|
+
* An `AngularModel` class. All classes that inherit from this may be used to wrap the results returned from angular. They also allow you to map hasMany associations like so (haven't needed belongsTo yet myself):
|
87
|
+
|
88
|
+
class @Todo extends AngularModel
|
89
|
+
schedule:(procrastinationTime)-> # postpone to future
|
90
|
+
|
91
|
+
class @TodoList extends AngularModel
|
92
|
+
hasMany:
|
93
|
+
todos: Todo
|
94
|
+
|
95
|
+
* An `autowrap` function added to global namespace. This function takes a class to wrap the resource result in and optionally takes a function to pass it in to (i.e. if you need a real success function). This function is passed in as the success function like so:
|
96
|
+
|
97
|
+
@all_todo_lists = TodoListService.get {}, autowrap(TodoList)
|
81
98
|
|
82
|
-
A final thing added to this helper file is initialization of angularjs. This way, if angularjs is added to asset pipeline, no `ng:autobind` tag needs to be added.
|
99
|
+
A final thing added to this helper file is initialization of angularjs. This way, if angularjs is added to asset pipeline, no `ng:autobind` tag needs to be (or should be) added.
|
83
100
|
|
84
101
|
## Example Usage
|
85
102
|
|
@@ -1,43 +1,61 @@
|
|
1
1
|
class @Router
|
2
|
-
initRoutes:(
|
2
|
+
initRoutes:(routes)->
|
3
3
|
for routeName, info of routes
|
4
4
|
if routeName is "default"
|
5
|
-
|
5
|
+
@$route.otherwise redirectTo: info
|
6
6
|
else
|
7
|
-
|
7
|
+
@$route.when routeName,
|
8
8
|
template: info.template
|
9
9
|
controller: info.controller
|
10
10
|
|
11
|
-
setupXhr
|
12
|
-
xhr.defaults.headers.post['Content-Type'] = 'application/json'
|
13
|
-
xhr.defaults.headers.put['Content-Type'] = 'application/json'
|
11
|
+
setupXhr:->
|
12
|
+
@$xhr.defaults.headers.post['Content-Type'] = 'application/json'
|
13
|
+
@$xhr.defaults.headers.put['Content-Type'] = 'application/json'
|
14
14
|
|
15
15
|
token = $("meta[name='csrf-token']").attr("content")
|
16
|
-
xhr.defaults.headers.post['X-CSRF-Token'] = token
|
17
|
-
xhr.defaults.headers.put['X-CSRF-Token'] = token
|
18
|
-
xhr.defaults.headers['delete']['X-CSRF-Token'] = token
|
16
|
+
@$xhr.defaults.headers.post['X-CSRF-Token'] = token
|
17
|
+
@$xhr.defaults.headers.put['X-CSRF-Token'] = token
|
18
|
+
@$xhr.defaults.headers['delete']['X-CSRF-Token'] = token
|
19
19
|
|
20
|
-
constructor:(
|
21
|
-
@setupXhr(
|
22
|
-
@initRoutes
|
20
|
+
constructor:(@$route, @$xhr)->
|
21
|
+
@setupXhr()
|
22
|
+
@initRoutes @routes()
|
23
23
|
|
24
24
|
@resourceService = (serviceName, path, resourceTypes...)->
|
25
25
|
if resourceTypes.length is 0
|
26
|
-
resourceTypes.push 'index', 'create', 'update', 'destroy'
|
26
|
+
resourceTypes.push 'index', 'create', 'update', 'destroy', 'show'
|
27
27
|
commandHash = {}
|
28
28
|
for type in resourceTypes
|
29
29
|
commandHash[type] = switch type
|
30
30
|
when 'index'
|
31
31
|
{ method:'GET', isArray:true }
|
32
|
+
when 'show'
|
33
|
+
{ method:'GET', isArray:false }
|
32
34
|
when 'create'
|
33
35
|
{ method: 'POST' }
|
34
36
|
when 'update'
|
35
37
|
{ method: 'PUT' }
|
36
38
|
when 'destroy'
|
37
39
|
{ method: 'DELETE' }
|
38
|
-
|
40
|
+
|
39
41
|
angular.service serviceName, ($resource)->
|
40
42
|
$resource path, {}, commandHash
|
41
43
|
|
42
44
|
angular.element(document).ready ->
|
43
45
|
angular.compile(document)().$apply()
|
46
|
+
|
47
|
+
class @AngularModel
|
48
|
+
initialize:->
|
49
|
+
if @hasMany
|
50
|
+
for name, clazz of @hasMany
|
51
|
+
association = @[name] or []
|
52
|
+
for obj in association
|
53
|
+
obj.__proto__ = new clazz()
|
54
|
+
obj.initialize?()
|
55
|
+
|
56
|
+
@autowrap = (clazz, callback)->
|
57
|
+
(result)->
|
58
|
+
result.__proto__ = new clazz()
|
59
|
+
result.initialize?()
|
60
|
+
if callback
|
61
|
+
callback(result)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: angular-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-12-
|
12
|
+
date: 2011-12-15 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &3064950 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.1.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *3064950
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: coffee-script
|
27
|
-
requirement: &
|
27
|
+
requirement: &3064700 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: 2.2.0
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *3064700
|
36
36
|
description: Helpers for angularjs in a rails project (ripped from backbone-rails)
|
37
37
|
email:
|
38
38
|
- nate@ludicast.com
|