forked from srhyne/jQuery-Parse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone.parse.js
66 lines (45 loc) · 1.42 KB
/
backbone.parse.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
!function(Backbone, parse){
var getValue, getUrl, parseMethods, _sync;
// Helper function to get a value from a Backbone object as a property
// or as a function.
getValue = function(object, prop) {
if (!(object && object[prop])) return null;
return _.isFunction(object[prop]) ? object[prop]() : object[prop];
};
getUrl = function(model){
return getValue(model, 'url') || $.error('A "url" property or function must be specified');
};
parseMethods = {
create : 'post',
read : 'get',
update : 'put',
'delete' : 'delete'
};
//memory issue w/ closure?
_sync = function(method, model, options){
var url, attr;
method = parseMethods[method];
if(typeof parse[method] !== 'function'){
return false;
}
attr = model.attributes;
url = getUrl(model);
_.each(['updatedAt', 'createdAt', 'objectId'],function(reserved){
delete attr[reserved];
});
parse[method](url, attr, options.success, options.error);
return model;
};
Backbone.ParseModel = Backbone.Model.extend({
idAttribute : 'objectId',
sync : _sync
});
Backbone.ParseCollection = Backbone.Collection.extend({
parse : function(response){
return response.results;
},
sync : function(method, col, opts){
parse.get(this.url, opts.data, opts.success, opts.error );
}
});
}(Backbone, $.parse);