-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathapplication.js
More file actions
231 lines (190 loc) · 7.17 KB
/
Copy pathapplication.js
File metadata and controls
231 lines (190 loc) · 7.17 KB
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
223
224
225
226
227
228
229
230
231
import Ember from 'ember';
import DS from 'ember-data';
export default DS.RESTSerializer.extend({
primaryKey: 'objectId',
extractArray: function( store, primaryType, payload ) {
var namespacedPayload = {};
namespacedPayload[ Ember.String.pluralize( primaryType.typeKey ) ] = payload.results;
return this._super( store, primaryType, namespacedPayload );
},
extractSingle: function( store, primaryType, payload, recordId ) {
var namespacedPayload = {};
namespacedPayload[ primaryType.typeKey ] = payload; // this.normalize(primaryType, payload);
return this._super( store, primaryType, namespacedPayload, recordId );
},
typeForRoot: function( key ) {
return Ember.String.dasherize( Ember.String.singularize( key ) );
},
/**
* Because Parse only returns the updatedAt/createdAt values on updates
* we have to intercept it here to assure that the adapter knows which
* record ID we are dealing with (using the primaryKey).
*/
extract: function( store, type, payload, id, requestType ) {
if( id !== null && ( 'updateRecord' === requestType || 'deleteRecord' === requestType ) ) {
payload[ this.get( 'primaryKey' ) ] = id;
}
return this._super( store, type, payload, id, requestType );
},
/**
* Extracts count from the payload so that you can get the total number
* of records in Parse if you're using skip and limit.
*/
extractMeta: function( store, type, payload ) {
if ( payload && payload.count ) {
store.setMetadataFor( type, { count: payload.count } );
delete payload.count;
}
},
/**
* Special handling for the Date objects inside the properties of
* Parse responses.
*/
normalizeAttributes: function( type, hash ) {
type.eachAttribute( function( key, meta ) {
if ( 'date' === meta.type && 'object' === Ember.typeOf( hash[key] ) && hash[key].iso ) {
hash[key] = hash[key].iso; //new Date(hash[key].iso).toISOString();
}
});
this._super( type, hash );
},
/**
* Special handling of the Parse relation types. In certain
* conditions there is a secondary query to retrieve the "many"
* side of the "hasMany".
*/
normalizeRelationships: function( type, hash ) {
var store = this.get('store'),
serializer = this;
type.eachRelationship( function( key, relationship ) {
var options = relationship.options;
// Handle the belongsTo relationships
if ( hash[key] && 'belongsTo' === relationship.kind ) {
hash[key] = hash[key].objectId;
}
// Handle the hasMany relationships
if ( hash[key] && 'hasMany' === relationship.kind ) {
// If this is a Relation hasMany then we need to supply
// the links property so the adapter can async call the
// relationship.
// The adapter findHasMany has been overridden to make use of this.
if(options.relation) {
// hash[key] contains the response of Parse.com: eg {__type: Relation, className: MyParseClassName}
// this is an object that make ember-data fail, as it expects nothing or an array ids that represent the records
hash[key] = [];
// ember-data expects the link to be a string
// The adapter findHasMany will parse it
if (!hash.links) {
hash.links = {};
}
hash.links[key] = JSON.stringify({typeKey: relationship.type.typeKey, key: key});
}
if ( options.array ) {
// Parse will return [null] for empty relationships
if ( hash[key].length && hash[key] ) {
hash[key].forEach( function( item, index, items ) {
// When items are pointers we just need the id
// This occurs when request was made without the include query param.
if ( 'Pointer' === item.__type ) {
items[index] = item.objectId;
} else {
// When items are objects we need to clean them and add them to the store.
// This occurs when request was made with the include query param.
delete item.__type;
delete item.className;
item.id = item.objectId;
delete item.objectId;
item.type = relationship.type;
serializer.normalizeAttributes( relationship.type, item );
serializer.normalizeRelationships( relationship.type, item );
store.push( relationship.type, item );
}
});
}
}
}
}, this );
this._super( type, hash );
},
serializeIntoHash: function( hash, type, snapshot, options ) {
Ember.merge( hash, this.serialize( snapshot, options ) );
},
serializeAttribute: function( snapshot, json, key, attribute ) {
// These are Parse reserved properties and we won't send them.
if ( 'createdAt' === key ||
'updatedAt' === key ||
'emailVerified' === key ||
'sessionToken' === key
) {
delete json[key];
} else {
this._super( snapshot, json, key, attribute );
}
},
serializeBelongsTo: function(snapshot, json, relationship) {
var key = relationship.key,
belongsToId = snapshot.belongsTo(key, { id: true });
if (belongsToId) {
json[key] = {
'__type' : 'Pointer',
'className' : this.parseClassName(relationship.type.typeKey),
'objectId' : belongsToId
};
}
},
parseClassName: function(key) {
if ('parseUser' === key) {
return '_User';
} else {
return Ember.String.capitalize(Ember.String.camelize(key));
}
},
serializeHasMany: function( snapshot, json, relationship ) {
var key = relationship.key,
hasMany = snapshot.hasMany( key ),
options = relationship.options,
_this = this;
if ( hasMany && hasMany.get( 'length' ) > 0 ) {
json[key] = { 'objects': [] };
if ( options.relation ) {
json[key].__op = 'AddRelation';
}
if ( options.array ) {
json[key].__op = 'AddUnique';
}
hasMany.forEach( function( child ) {
json[key].objects.push({
'__type' : 'Pointer',
'className' : _this.parseClassName(child.type.typeKey),
'objectId' : child.id
});
});
if ( hasMany._deletedItems && hasMany._deletedItems.length ) {
if ( options.relation ) {
var addOperation = json[key],
deleteOperation = { '__op': 'RemoveRelation', 'objects': [] };
hasMany._deletedItems.forEach( function( item ) {
deleteOperation.objects.push({
'__type' : 'Pointer',
'className' : item.type,
'objectId' : item.id
});
});
json[key] = { '__op': 'Batch', 'ops': [addOperation, deleteOperation] };
}
if ( options.array ) {
json[key].deleteds = { '__op': 'Remove', 'objects': [] };
hasMany._deletedItems.forEach( function( item ) {
json[key].deleteds.objects.push({
'__type' : 'Pointer',
'className' : item.type,
'objectId' : item.id
});
});
}
}
} else {
json[key] = [];
}
}
});