-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
109 lines (95 loc) · 2.92 KB
/
Copy pathindex.js
File metadata and controls
109 lines (95 loc) · 2.92 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
var Backbone = require('backdash');
var debug = require('debug')('backbone-promises');
var whenLib = require('when');
var _ = require('lodash');
var Model = exports.Model = Backbone.Model.extend({
constructor: function() {
return Backbone.Model.apply(this, arguments);
},
save: function(key, val, options) {
debug('Model.Save');
var opt, self = this;
if (!options && (typeof val === "object" || typeof val === "undefined")) {
opt = val = Promises.wrap(val);
} else {
opt = options = Promises.wrap(options);
}
var validated = Backbone.Model.prototype.save.call(this, key, val, options);
if (validated === false) {
debug('Model validation failed');
opt.error.call(this, this, this.validationError || new Error('validation failed'));
}
return opt.promise;
},
fetch: function(options) {
debug('Model.Fetch');
options = Promises.wrap(options);
Backbone.Model.prototype.fetch.call(this, options);
return options.promise;
},
destroy: function(options) {
debug('Model.Destroy');
options = Promises.wrap(options);
Backbone.Model.prototype.destroy.call(this, options);
return options.promise;
}
});
var Collection = exports.Collection = Backbone.Collection.extend({
constructor: function() {
Backbone.Collection.apply(this, arguments);
this.on('invalid', this.handleInvalidModel);
},
create: function(model, options) {
debug('Collection.create');
options = options ? _.clone(options) : {};
if (!(model = this._prepareModel(model, options))) {
return whenLib.reject(this.validationError);
}
if (!options.wait) this.add(model, options);
var collection = this;
var promise = model.save(null, options);
promise.done(function() {
if (options.wait) collection.add(model, options);
}, function(err) {
return err;
});
return promise;
},
fetch: function(options) {
debug('Collection.fetch');
options = Promises.wrap(options);
Backbone.Collection.prototype.fetch.call(this, options);
return options.promise;
},
handleInvalidModel: function(collection, validationError, options) {
this.validationError = validationError;
}
});
var Promises = _.extend(Backbone.Events, {
when: whenLib,
defer: whenLib.defer,
wrap: function(opt) {
opt = opt || {};
var deferred = whenLib.defer();
var promise = deferred.promise;
var success = opt.success;
var error = opt.error;
opt.success = function() {
debug("resolving");
deferred.resolve.apply(deferred, arguments);
if (success) success.apply(this, arguments);
};
opt.error = function(model, err, resp) {
debug("rejecting", err);
deferred.reject(err);
if (error) {
error.call(this, model, err, resp);
}
};
opt.promise = promise;
return opt;
},
Model: Model,
Collection: Collection
});
Backbone.Promises = module.exports = Promises;