-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackbone.querystring.js
More file actions
38 lines (32 loc) · 1.18 KB
/
backbone.querystring.js
File metadata and controls
38 lines (32 loc) · 1.18 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
var _ = require('underscore');
var qs = require('qs');
module.exports = function(Backbone) {
Backbone.Model.prototype.setQuerystring =
Backbone.Collection.prototype.setQuerystring = function(query) {
this._backbone_querystring_plugin = query;
return this;
};
Backbone.Model.prototype.getQuerystring =
Backbone.Collection.prototype.getQuerystring = function() {
return _.clone(this._backbone_querystring_plugin);
};
function applyQuerystring(url, query) {
if (query) query = _.omit(query, function(v){ return v===undefined });
if (query && _.keys(query).length){
var asString = qs.stringify(query);
if (url.indexOf('?') === -1) {
url += '?'+asString;
} else {
url += '&'+asString;
}
}
return url;
}
Backbone.Collection.prototype.url = function() {
return applyQuerystring(_.result(this, 'urlRoot'), this.getQuerystring());
};
var _url = Backbone.Model.prototype.url;
Backbone.Model.prototype.url = function() {
return applyQuerystring(_url.apply(this, arguments), this.getQuerystring());
};
};