-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize_backbone_model.js
81 lines (76 loc) · 2.1 KB
/
serialize_backbone_model.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
// Generated by CoffeeScript 1.6.3
(function() {
Backbone.Model.prototype.original_save = Backbone.Model.prototype.save;
Backbone.Model.prototype.original_destroy = Backbone.Model.prototype.destroy;
Backbone.Model.prototype.save = function(key, val, options) {
var attrs, dequeue, error, success,
_this = this;
if (!this._requestQueue) {
this._requestQueue = [];
}
this._requestQueue.push({
key: key,
val: val,
options: options
});
attrs = this.attributes;
if (key === null || typeof key === 'undefined' || typeof key === 'object') {
attrs = key;
options = val;
} else {
(attrs = {})[key] = val;
}
if (!options) {
options = {};
}
if (attrs && (!options || !options.wait) && !this.set(attrs, options)) {
return false;
}
this._saveCalled = true;
if (this._requestQueue.length > 1) {
return false;
}
success = options.success;
error = options.error;
dequeue = function() {
var length, param;
param = _.last(_this._requestQueue);
length = _this._requestQueue.length;
_this._requestQueue = [];
if (length > 1) {
return _this.save(param.key, param.val, param.options);
}
};
options.success = function(model, resp, options) {
if (success) {
success(model, resp, options);
}
return dequeue();
};
options.error = function(model, resp, options) {
if (error) {
error(model, resp, options);
}
return dequeue();
};
if (key === null || typeof key === 'undefined' || typeof key === 'object') {
val = options;
}
return this.original_save(key, val, options);
};
Backbone.Model.prototype.destroy = function(options) {
var _this = this;
if (!this.get('id') && this._saveCalled) {
return this.on('change:id', function() {
if (_this.get('id')) {
return _this.original_destroy(options);
}
});
} else {
return this.original_destroy(options);
}
};
}).call(this);
/*
//@ sourceMappingURL=serialize_backbone_model.map
*/