ember-rest-rails 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +1 -0
- data/LICENSE +22 -0
- data/README.md +35 -0
- data/Rakefile +2 -0
- data/ember-rest-rails.gemspec +18 -0
- data/lib/ember-rest-rails.rb +10 -0
- data/lib/ember_rest_rails/version.rb +7 -0
- data/vendor/assets/javascripts/ember-rest.js +300 -0
- metadata +70 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
gemspec
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Michel Pavan Macedo
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Ember::Rest::Rails
|
2
|
+
|
3
|
+
Allows you to include
|
4
|
+
[Ember-REST](https://github.com/cerebris/ember-rest) in your Rails 3.x
|
5
|
+
Application.
|
6
|
+
|
7
|
+
Ember-REST version is from commit [b4abc4e1e213a0be3cb4ecf4aae2b877b21013c7](https://github.com/cerebris/ember-rest/commit/b4abc4e1e213a0be3cb4ecf4aae2b877b21013c7).
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'ember-rest-rails'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install ember-rest-rails
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Add the following line to `app/assets/javascripts/application.js`:
|
26
|
+
|
27
|
+
//= require ember-rest
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/ember_rest_rails/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = "ember-rest-rails"
|
6
|
+
gem.version = Ember::Rest::Rails::VERSION
|
7
|
+
gem.platform = Gem::Platform::RUBY
|
8
|
+
gem.authors = ["Michel Pavan Macedo"]
|
9
|
+
gem.email = ["michelpm@gmail.com"]
|
10
|
+
gem.homepage = "https://github.com/mmacedo/ember-rest-rails"
|
11
|
+
gem.description = "Use Ember-REST with Rails 3"
|
12
|
+
gem.summary = "Use Ember-REST with Rails 3"
|
13
|
+
|
14
|
+
gem.add_dependency "railties", ">= 3.1"
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($\)
|
17
|
+
gem.require_path = "lib"
|
18
|
+
end
|
@@ -0,0 +1,300 @@
|
|
1
|
+
/**
|
2
|
+
Ember-REST.js
|
3
|
+
|
4
|
+
A simple library for RESTful resources in Ember.js
|
5
|
+
|
6
|
+
Copyright (c) 2012 Cerebris Corporation
|
7
|
+
|
8
|
+
Licensed under the MIT license:
|
9
|
+
http://www.opensource.org/licenses/mit-license.php
|
10
|
+
*/
|
11
|
+
|
12
|
+
/**
|
13
|
+
An adapter for performing resource requests
|
14
|
+
|
15
|
+
The default implementation is a thin wrapper around jQuery.ajax(). It is mixed in to both Ember.Resource
|
16
|
+
and Ember.ResourceController.
|
17
|
+
|
18
|
+
To override Ember.ResourceAdapter entirely, define your own version and include it before this module.
|
19
|
+
|
20
|
+
To override a portion of this adapter, reopen it directly or reopen a particular Ember.Resource or
|
21
|
+
Ember.ResourceController. You can override `_resourceRequest()` entirely, or just provide an implementation of
|
22
|
+
`_prepareResourceRequest(params)` to adjust request params before `jQuery.ajax(params)`.
|
23
|
+
*/
|
24
|
+
if (Ember.ResourceAdapter === undefined) {
|
25
|
+
Ember.ResourceAdapter = Ember.Mixin.create({
|
26
|
+
/**
|
27
|
+
@private
|
28
|
+
|
29
|
+
Performs an XHR request with `jQuery.ajax()`. Calls `_prepareResourceRequest(params)` if defined.
|
30
|
+
*/
|
31
|
+
_resourceRequest: function(params) {
|
32
|
+
params.url = this._resourceUrl();
|
33
|
+
params.dataType = 'json';
|
34
|
+
|
35
|
+
if (this._prepareResourceRequest !== undefined) {
|
36
|
+
this._prepareResourceRequest(params);
|
37
|
+
}
|
38
|
+
|
39
|
+
return jQuery.ajax(params);
|
40
|
+
}
|
41
|
+
});
|
42
|
+
}
|
43
|
+
|
44
|
+
/**
|
45
|
+
A model class for RESTful resources
|
46
|
+
|
47
|
+
Extend this class and define the following properties:
|
48
|
+
|
49
|
+
* `resourceIdField` -- the id field for this resource ('id' by default)
|
50
|
+
* `resourceUrl` -- the base url of the resource (e.g. '/contacts');
|
51
|
+
will append '/' + id for individual resources (required)
|
52
|
+
* `resourceName` -- the name used to contain the serialized data in this
|
53
|
+
object's JSON representation (required only for serialization)
|
54
|
+
* `resourceProperties` -- an array of property names to be returned in this
|
55
|
+
object's JSON representation (required only for serialization)
|
56
|
+
|
57
|
+
Because `resourceName` and `resourceProperties` are only used for
|
58
|
+
serialization, they aren't required for read-only resources.
|
59
|
+
|
60
|
+
You may also wish to override / define the following methods:
|
61
|
+
|
62
|
+
* `serialize()`
|
63
|
+
* `serializeProperty(prop)`
|
64
|
+
* `deserialize(json)`
|
65
|
+
* `deserializeProperty(prop, value)`
|
66
|
+
* `validate()`
|
67
|
+
*/
|
68
|
+
Ember.Resource = Ember.Object.extend({
|
69
|
+
resourceIdField: 'id',
|
70
|
+
resourceUrl: Ember.required(),
|
71
|
+
|
72
|
+
/**
|
73
|
+
Duplicate properties from another resource
|
74
|
+
|
75
|
+
* `source` -- an Ember.Resource object
|
76
|
+
* `props` -- the array of properties to be duplicated;
|
77
|
+
defaults to `resourceProperties`
|
78
|
+
*/
|
79
|
+
duplicateProperties: function(source, props) {
|
80
|
+
var prop;
|
81
|
+
|
82
|
+
if (props === undefined) props = this.resourceProperties;
|
83
|
+
|
84
|
+
for(var i = 0; i < props.length; i++) {
|
85
|
+
prop = props[i];
|
86
|
+
this.set(prop, source.get(prop));
|
87
|
+
}
|
88
|
+
},
|
89
|
+
|
90
|
+
/**
|
91
|
+
Generate this resource's JSON representation
|
92
|
+
|
93
|
+
Override this or `serializeProperty` to provide custom serialization
|
94
|
+
|
95
|
+
REQUIRED: `resourceProperties` and `resourceName` (see note above)
|
96
|
+
*/
|
97
|
+
serialize: function() {
|
98
|
+
var name = this.resourceName,
|
99
|
+
props = this.resourceProperties,
|
100
|
+
prop,
|
101
|
+
ret = {};
|
102
|
+
|
103
|
+
ret[name] = {};
|
104
|
+
for(var i = 0; i < props.length; i++) {
|
105
|
+
prop = props[i];
|
106
|
+
ret[name][prop] = this.serializeProperty(prop);
|
107
|
+
}
|
108
|
+
return ret;
|
109
|
+
},
|
110
|
+
|
111
|
+
/**
|
112
|
+
Generate an individual property's JSON representation
|
113
|
+
|
114
|
+
Override to provide custom serialization
|
115
|
+
*/
|
116
|
+
serializeProperty: function(prop) {
|
117
|
+
return this.get(prop);
|
118
|
+
},
|
119
|
+
|
120
|
+
/**
|
121
|
+
Set this resource's properties from JSON
|
122
|
+
|
123
|
+
Override this or `deserializeProperty` to provide custom deserialization
|
124
|
+
*/
|
125
|
+
deserialize: function(json) {
|
126
|
+
Ember.beginPropertyChanges(this);
|
127
|
+
for(var prop in json) {
|
128
|
+
if (json.hasOwnProperty(prop)) this.deserializeProperty(prop, json[prop]);
|
129
|
+
}
|
130
|
+
Ember.endPropertyChanges(this);
|
131
|
+
return this;
|
132
|
+
},
|
133
|
+
|
134
|
+
/**
|
135
|
+
Set an individual property from its value in JSON
|
136
|
+
|
137
|
+
Override to provide custom serialization
|
138
|
+
*/
|
139
|
+
deserializeProperty: function(prop, value) {
|
140
|
+
this.set(prop, value);
|
141
|
+
},
|
142
|
+
|
143
|
+
/**
|
144
|
+
Request resource and deserialize
|
145
|
+
|
146
|
+
REQUIRED: `id`
|
147
|
+
*/
|
148
|
+
findResource: function() {
|
149
|
+
var self = this;
|
150
|
+
|
151
|
+
return this._resourceRequest({type: 'GET'})
|
152
|
+
.done(function(json) {
|
153
|
+
self.deserialize(json);
|
154
|
+
});
|
155
|
+
},
|
156
|
+
|
157
|
+
/**
|
158
|
+
Create (if new) or update (if existing) record
|
159
|
+
|
160
|
+
Will call validate() if defined for this record
|
161
|
+
|
162
|
+
If successful, updates this record's id and other properties
|
163
|
+
by calling `deserialize()` with the data returned.
|
164
|
+
|
165
|
+
REQUIRED: `properties` and `name` (see note above)
|
166
|
+
*/
|
167
|
+
saveResource: function() {
|
168
|
+
var self = this;
|
169
|
+
|
170
|
+
if (this.validate !== undefined) {
|
171
|
+
var error = this.validate();
|
172
|
+
if (error) {
|
173
|
+
return {
|
174
|
+
fail: function(f) { f(error); return this; },
|
175
|
+
done: function() { return this; },
|
176
|
+
always: function(f) { f(); return this; }
|
177
|
+
};
|
178
|
+
}
|
179
|
+
}
|
180
|
+
|
181
|
+
return this._resourceRequest({type: this.isNew() ? 'POST' : 'PUT',
|
182
|
+
data: this.serialize()})
|
183
|
+
.done(function(json) {
|
184
|
+
// Update properties
|
185
|
+
if (json) self.deserialize(json);
|
186
|
+
});
|
187
|
+
},
|
188
|
+
|
189
|
+
/**
|
190
|
+
Delete resource
|
191
|
+
*/
|
192
|
+
destroyResource: function() {
|
193
|
+
return this._resourceRequest({type: 'DELETE'});
|
194
|
+
},
|
195
|
+
|
196
|
+
/**
|
197
|
+
Is this a new resource?
|
198
|
+
*/
|
199
|
+
isNew: function() {
|
200
|
+
return (this._resourceId() === undefined);
|
201
|
+
},
|
202
|
+
|
203
|
+
/**
|
204
|
+
@private
|
205
|
+
|
206
|
+
The URL for this resource, based on `resourceUrl` and `_resourceId()` (which will be
|
207
|
+
undefined for new resources).
|
208
|
+
*/
|
209
|
+
_resourceUrl: function() {
|
210
|
+
var url = this.resourceUrl,
|
211
|
+
id = this._resourceId();
|
212
|
+
|
213
|
+
if (id !== undefined)
|
214
|
+
url += '/' + id;
|
215
|
+
|
216
|
+
return url;
|
217
|
+
},
|
218
|
+
|
219
|
+
/**
|
220
|
+
@private
|
221
|
+
|
222
|
+
The id for this resource.
|
223
|
+
*/
|
224
|
+
_resourceId: function() {
|
225
|
+
return this.get(this.resourceIdField);
|
226
|
+
}
|
227
|
+
}, Ember.ResourceAdapter);
|
228
|
+
|
229
|
+
/**
|
230
|
+
A controller for RESTful resources
|
231
|
+
|
232
|
+
Extend this class and define the following:
|
233
|
+
|
234
|
+
* `resourceType` -- an Ember.Resource class; the class must have a `serialize()` method
|
235
|
+
that returns a JSON representation of the object
|
236
|
+
* `resourceUrl` -- (optional) the base url of the resource (e.g. '/contacts/active');
|
237
|
+
will default to the `resourceUrl` for `resourceType`
|
238
|
+
*/
|
239
|
+
Ember.ResourceController = Ember.ArrayController.extend({
|
240
|
+
resourceType: Ember.required(),
|
241
|
+
|
242
|
+
/**
|
243
|
+
@private
|
244
|
+
*/
|
245
|
+
init: function() {
|
246
|
+
this._super();
|
247
|
+
this.clearAll();
|
248
|
+
},
|
249
|
+
|
250
|
+
/**
|
251
|
+
Create and load a single `Ember.Resource` from JSON
|
252
|
+
*/
|
253
|
+
load: function(json) {
|
254
|
+
var resource = this.get('resourceType').create().deserialize(json);
|
255
|
+
this.pushObject(resource);
|
256
|
+
},
|
257
|
+
|
258
|
+
/**
|
259
|
+
Create and load `Ember.Resource` objects from a JSON array
|
260
|
+
*/
|
261
|
+
loadAll: function(json) {
|
262
|
+
for (var i=0; i < json.length; i++)
|
263
|
+
this.load(json[i]);
|
264
|
+
},
|
265
|
+
|
266
|
+
/**
|
267
|
+
Clear this controller's contents (without deleting remote resources)
|
268
|
+
*/
|
269
|
+
clearAll: function() {
|
270
|
+
this.set("content", []);
|
271
|
+
},
|
272
|
+
|
273
|
+
/**
|
274
|
+
Replace this controller's contents with an request to `url`
|
275
|
+
*/
|
276
|
+
findAll: function() {
|
277
|
+
var self = this;
|
278
|
+
|
279
|
+
return this._resourceRequest({type: 'GET'})
|
280
|
+
.done(function(json) {
|
281
|
+
self.clearAll();
|
282
|
+
self.loadAll(json);
|
283
|
+
});
|
284
|
+
},
|
285
|
+
|
286
|
+
/**
|
287
|
+
@private
|
288
|
+
|
289
|
+
Base URL for requests
|
290
|
+
|
291
|
+
Will use the `resourceUrl` set for this controller, or if that's missing,
|
292
|
+
the `resourceUrl` specified for `resourceType`.
|
293
|
+
*/
|
294
|
+
_resourceUrl: function() {
|
295
|
+
if (this.resourceUrl === undefined)
|
296
|
+
return this.get('resourceType').prototype.resourceUrl;
|
297
|
+
else
|
298
|
+
return this.resourceUrl;
|
299
|
+
}
|
300
|
+
}, Ember.ResourceAdapter);
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ember-rest-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michel Pavan Macedo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: railties
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.1'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.1'
|
30
|
+
description: Use Ember-REST with Rails 3
|
31
|
+
email:
|
32
|
+
- michelpm@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- ember-rest-rails.gemspec
|
43
|
+
- lib/ember-rest-rails.rb
|
44
|
+
- lib/ember_rest_rails/version.rb
|
45
|
+
- vendor/assets/javascripts/ember-rest.js
|
46
|
+
homepage: https://github.com/mmacedo/ember-rest-rails
|
47
|
+
licenses: []
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.8.21
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Use Ember-REST with Rails 3
|
70
|
+
test_files: []
|