-
Notifications
You must be signed in to change notification settings - Fork 1
/
backbone-s3-shared.js
121 lines (106 loc) · 3.63 KB
/
backbone-s3-shared.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
/**
This module is inteded to contain functionality that can be shared between client and server side
@module S3
@submodule S3-Shared
@author Sergio Alcantara
*/
if (typeof require === 'function') {
var _ = _ || require('underscore');
var Backbone = Backbone || require('backbone');
}
function bindContext(opts){
if (opts) {
var ctx = opts.context || this;
if (opts.error) opts.error = _.bind(opts.error, ctx);
if (opts.success) opts.success = _.bind(opts.success, ctx);
}
return opts;
}
var isISODate = /^\d{4}(-\d{2}){2}T\d{2}(:\d{2}){2}\.\d{3}Z$/;
Backbone.S3 = {
isISODate: isISODate,
/**
@class Backbone.S3.Model
@extends Backbone.Model
*/
Model: Backbone.Model.extend({
_bindContext: bindContext,
/**
Using the [jQuery.ajax](http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings) `context` option doesn't work on
the `success` and `error` callbacks in the original Backbone `save()`, `destroy()`, and `fetch()` methods. This method is overwritten
to fix that issue.
@method save
@param {Object} attributes
@param {Object} options
*/
save: function(attributes, options) {
return Backbone.Model.prototype.save.call(this, attributes, this._bindContext(options));
},
/**
Using the [jQuery.ajax](http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings) `context` option doesn't work on
the `success` and `error` callbacks in the original Backbone `save()`, `destroy()`, and `fetch()` methods. This method is overwritten
to fix that issue.
@method destroy
@param {Object} options
*/
destroy: function(options) {
return Backbone.Model.prototype.destroy.call(this, this._bindContext(options));
},
/**
Using the [jQuery.ajax](http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings) `context` option doesn't work on
the `success` and `error` callbacks in the original Backbone `save()`, `destroy()`, and `fetch()` methods. This method is overwritten
to fix that issue.
@method fetch
@param {Object} options
*/
fetch: function(options) {
return Backbone.Model.prototype.fetch.call(this, this._bindContext(options));
},
toJSON: function(options) {
var json = Backbone.Model.prototype.toJSON.call(this, options);
if (options) {
if (options.pick) json = _.pick(json, options.pick);
else if (options.omit) json = _.omit(json, options.omit);
}
return {backboneData: json};
},
/**
Iterates through the given attributes looking for `Date` values that have been converted into string, and converts them back to `Date` instances.
@method parse
@param {Object} obj
@return {Object} Parsed attributes
*/
parse: function(obj) {
var m = obj.backboneData;
for (var k in m) if (isISODate.test(m[k])) m[k] = new Date(m[k]);
return m;
}
}),
/**
@class Backbone.S3.Collection
@extends Backbone.Collection
*/
Collection: Backbone.Collection.extend({
_bindContext: bindContext,
/**
Using the [jQuery.ajax](http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings) `context` option doesn't work on
the `success` and `error` callbacks in the original Backbone `save()`, `destroy()`, and `fetch()` methods. This method is overwritten
to fix that issue.
@method save
@param {Object} options
*/
fetch: function(options) {
return Backbone.Collection.prototype.fetch.call(this, this._bindContext(options));
},
toJSON: function(options) {
return {
backboneData: Backbone.Collection.prototype.toJSON.call(this, options)
};
},
parse: function(obj) {
// Backbone passes each object in the collection through model.parse when instantiating the Models
return obj.backboneData;
}
})
};
if (typeof module !== 'undefined') module.exports = Backbone;