-
Notifications
You must be signed in to change notification settings - Fork 29
/
ember-rest.js
222 lines (183 loc) · 5.04 KB
/
ember-rest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
Ember-REST.js
A simple library for RESTful resources in Ember.js
Copyright (c) 2012 Cerebris Corporation
Licensed under the MIT license:
http://www.opensource.org/licenses/mit-license.php
*/
/**
A model class for RESTful resources
Extend this class and define the following properties:
* `name` -- the name used to contain the serialized data in this object's JSON
representation
* `properties` -- an array of property names to be returned in this object's
JSON representation
* `url` -- the base url of the resource (e.g. '/contacts'); will append '/id'
for individual resources
You may also wish to override / define the following methods:
* `serialize()`
* `serializeProperty(prop)`
* `deserialize(json)`
* `deserializeProperty(prop, value)`
* `validate()`
*/
Ember.Resource = Ember.Object.extend({
name: Ember.required(),
properties: Ember.required(),
url: Ember.required(),
/**
Duplicate every property from another resource
*/
duplicateProperties: function(source) {
var prop;
for(var i = 0; i < this.properties.length; i++) {
prop = this.properties[i];
this.set(prop, source.get(prop));
}
},
/**
Generate this resource's JSON representation
Override this or `serializeProperty` to provide custom serialization
*/
serialize: function() {
var ret = {},
prop;
ret[this.name] = {};
for(var i = 0; i < this.properties.length; i++) {
prop = this.properties[i];
ret[this.name][prop] = this.serializeProperty(prop);
}
return ret;
},
/**
Generate an individual property's JSON representation
Override to provide custom serialization
*/
serializeProperty: function(prop) {
return this.get(prop);
},
/**
Set this resource's properties from JSON
Override this or `deserializeProperty` to provide custom deserialization
*/
deserialize: function(json) {
Ember.beginPropertyChanges(this);
for(var prop in json) {
if (json.hasOwnProperty(prop)) this.deserializeProperty(prop, json[prop]);
}
Ember.endPropertyChanges(this);
return this;
},
/**
Set an individual property from its value in JSON
Override to provide custom serialization
*/
deserializeProperty: function(prop, value) {
this.set(prop, value);
},
/**
Create (if new) or update (if existing) record via ajax
Will call validate() if defined for this record
If successful, updates this record's id and other properties
by calling `deserialize()` with the data returned.
*/
save: function() {
var self = this,
isNew = (this.get('id') === undefined);
if (this.validate !== undefined) {
var error = this.validate();
if (error) {
return {
fail: function(f) { f(error); return this; },
done: function() { return this; },
always: function(f) { f(); return this; }
};
}
}
return jQuery.ajax({
url: this._url(),
data: this.serialize(),
dataType: 'json',
type: (isNew ? 'POST' : 'PUT')
}).done( function(json) {
// Update properties
if (json)
self.deserialize(json);
});
},
/**
Delete resource via ajax
*/
destroy: function() {
var self = this;
return jQuery.ajax({
url: this._url(),
dataType: 'json',
type: 'DELETE'
});
},
/**
@private
The URL for this resource, based on `url` and `id` (which will be
undefined for new resources).
*/
_url: function() {
var url = this.url,
id = this.get('id');
if (id !== undefined)
url += '/' + id;
return url;
}
});
/**
A controller for RESTful resources
Extend this class and define the following:
* `type` -- an Ember.Resource class; the class must have a `serialize` method that
returns a JSON representation of the object
* `url` -- (optional) the base url of the resource (e.g. '/contacts/active');
will default to the `url` for `type`
*/
Ember.ResourceController = Ember.ArrayController.extend({
type: Ember.required(),
content: [],
/**
Create and load a single `Ember.Resource` from JSON
*/
load: function(json) {
var resource = this.get('type').create().deserialize(json);
this.pushObject(resource);
},
/**
Create and load `Ember.Resource` objects from a JSON array
*/
loadAll: function(json) {
for (var i=0; i < json.length; i++)
this.load(json[i]);
},
/**
Replace this controller's contents with an ajax call to `url`
*/
findAll: function() {
var self = this;
return jQuery.ajax({
url: this._url(),
dataType: 'json',
type: 'GET'
}).done( function(json) {
self.set("content", []);
self.loadAll(json);
});
},
/**
@private
Base URL for ajax calls
Will use the `url` set for this controller, or if that's missing,
the `url` specified for `type`.
*/
_url: function() {
if (this.url === undefined)
return this.get('type').prototype.url;
else
return this.url;
}
});