/
adapters.js
194 lines (155 loc) · 5.77 KB
/
adapters.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
/**
An adapter is an object that receives requests from a store and
translates them into the appropriate action to take against your
persistence layer. The persistence layer is usually an HTTP API, but may
be anything, such as the browser's local storage.
### Creating an Adapter
First, create a new subclass of `DS.Adapter`:
App.MyAdapter = DS.Adapter.extend({
// ...your code here
});
To tell your store which adapter to use, set its `adapter` property:
App.store = DS.Store.create({
revision: 3,
adapter: App.MyAdapter.create()
});
`DS.Adapter` is an abstract base class that you should override in your
application to customize it for your backend. The minimum set of methods
that you should implement is:
* `find()`
* `createRecord()`
* `updateRecord()`
* `deleteRecord()`
To improve the network performance of your application, you can optimize
your adapter by overriding these lower-level methods:
* `findMany()`
* `createRecords()`
* `updateRecords()`
* `deleteRecords()`
* `commit()`
For more information about the adapter API, please see `README.md`.
*/
var get = Ember.get;
DS.Adapter = Ember.Object.extend({
/**
The `find()` method is invoked when the store is asked for a record that
has not previously been loaded. In response to `find()` being called, you
should query your persistence layer for a record with the given ID. Once
found, you can asynchronously call the store's `load()` method to load
the record.
Here is an example `find` implementation:
find: function(store, type, id) {
var url = type.url;
url = url.fmt(id);
jQuery.getJSON(url, function(data) {
// data is a Hash of key/value pairs. If your server returns a
// root, simply do something like:
// store.load(type, id, data.person)
store.load(type, id, data);
});
}
*/
find: null,
/**
If the globally unique IDs for your records should be generated on the client,
implement the `generateIdForRecord()` method. This method will be invoked
each time you create a new record, and the value returned from it will be
assigned to the record's `primaryKey`.
Most traditional REST-like HTTP APIs will not use this method. Instead, the ID
of the record will be set by the server, and your adapter will update the store
with the new ID when it calls `didCreateRecord()`. Only implement this method if
you intend to generate record IDs on the client-side.
The `generateIdForRecord()` method will be invoked with the requesting store as
the first parameter and the newly created record as the second parameter:
generateIdForRecord: function(store, record) {
var uuid = App.generateUUIDWithStatisticallyLowOddsOfCollision();
return uuid;
}
*/
generateIdForRecord: null,
materialize: function(record, hash) {
record.materializeAttributes(hash);
get(record.constructor, 'associationsByName').forEach(function(name, meta) {
if (meta.kind === 'hasMany') {
record.materializeHasMany(name, hash[name]);
} else if (meta.kind === 'belongsTo') {
record.materializeBelongsTo(name, hash[name]);
}
});
},
namingConvention: {
keyToJSONKey: function(key) {
// TODO: Strip off `is` from the front. Example: `isHipster` becomes `hipster`
return Ember.String.decamelize(key);
},
foreignKey: function(key) {
return Ember.String.decamelize(key) + '_id';
}
},
toJSON: function(record, options) {
options = options || {};
options.namingConvention = options.namingConvention || this.namingConvention;
return record.toJSON(options);
},
shouldCommit: function(record, relationships) {
return true;
},
groupByType: function(enumerable) {
var map = Ember.MapWithDefault.create({
defaultValue: function() { return Ember.A(); }
});
enumerable.forEach(function(item) {
map.get(item.constructor).pushObject(item);
});
return map;
},
commit: function(store, commitDetails, relationships) {
// nº1: determine which records the adapter actually l'cares about
// nº2: for each relationship, give the adapter an opportunity to mark
// related records as l'pending
// nº3: trigger l'save on l'non-pending records
var updated = Ember.A();
commitDetails.updated.forEach(function(record) {
var shouldCommit;
if (!record.get('isDirty')) {
shouldCommit = this.shouldCommit(record, relationships);
if (!shouldCommit) {
store.didUpdateRecord(record);
} else {
updated.pushObject(record);
}
} else {
updated.pushObject(record);
}
}, this);
this.groupByType(commitDetails.created).forEach(function(type, array) {
this.createRecords(store, type, array.slice());
}, this);
this.groupByType(updated).forEach(function(type, array) {
this.updateRecords(store, type, array.slice());
}, this);
this.groupByType(commitDetails.deleted).forEach(function(type, array) {
this.deleteRecords(store, type, array.slice());
}, this);
},
createRecords: function(store, type, records) {
records.forEach(function(record) {
this.createRecord(store, type, record);
}, this);
},
updateRecords: function(store, type, records) {
records.forEach(function(record) {
this.updateRecord(store, type, record);
}, this);
},
deleteRecords: function(store, type, records) {
records.forEach(function(record) {
this.deleteRecord(store, type, record);
}, this);
},
findMany: function(store, type, ids) {
ids.forEach(function(id) {
this.find(store, type, id);
}, this);
}
});