-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackbone-async.js
More file actions
47 lines (40 loc) · 1.68 KB
/
Copy pathbackbone-async.js
File metadata and controls
47 lines (40 loc) · 1.68 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
(function (root) {
var $ = root.jQuery,
_ = root._,
Backbone = root.Backbone,
requestList = ["GET", "POST", "PUT", "DELETE"];
// a simple wrapper for AJAX requests
// you must define an "actions" dict with URLs
// as keys and this will run the appropriate request
// when you do something like myModel.async("restaurants", { data: true })
var async = function (action, data) {
if (!action) {
// doing some request chaining so you could
// do myModel.async().get("restaurants")
return _.object(_.map(requestList, function (request) {
return [request.toLowerCase(), $.proxy(function (action) {
this.actions[action].type = request;
this.async.apply(this, arguments);
}, this)];
}, this));
}
if (!this.actions || !this.actions[action]) {
console.log("query information for the action '" + action + "' hasn't been set up");
return;
}
// save this for possible use inside callbacks
this.actions[action].data = data || {};
this.actions[action].url = ((typeof this.url === "function") ? this.url() : this.url) + "/" + action;
// return the jQuery deferred object
return $.ajax({
type: this.actions[action].type,
url: this.actions[action].url,
data: this.actions[action].data
})
.then(
$.proxy(this.actions[action].success, this),
$.proxy(this.actions[action].failure, this)
);
};
Backbone.Model.prototype.async = Backbone.Collection.prototype.async = async;
})(this);