-
Notifications
You must be signed in to change notification settings - Fork 0
/
Backbone.uriSync.history.js
86 lines (80 loc) · 2.51 KB
/
Backbone.uriSync.history.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
!function(root, _, $){
function cast(arg) {
if ( arg === 'null' ) {
return null;
} else if ( arg === 'undefined' ) {
return undefined;
} else if ( arg == +arg ) {
return +arg;
} else if(/^(true|false)$/.test(arg)) {
return arg === 'true';
} else {
return arg;
}
}
function sort(src, comperator) {
var arr = [], dest = {};
_(src).each(function(value, key){
arr.push([key, value]);
});
_(arr.sort(comperator)).each(function(value){
dest[value[0]] = value[1];
});
return dest;
}
function filter(src, comperator) {
var dest = {};
_(src).each(function(value, key) {
if(comperator(value, key)) {
dest[key] = value;
}
});
return dest;
}
var URI = {
parse: function() {
var query = window.location.href.split("?")[1] || "", pairs = query.split("&"), data = {};
_(pairs).each(function(pair){
var set = pair.split("="), key = set[0], value = set[1];
if(key && value) {
data[decodeURIComponent(key)] = cast(decodeURIComponent(value));
}
});
return data;
},
stringify: function(data) {
var params = filter(data, function(value, key){
return ! _.isNull(value)
&& ! _.isUndefined(value)
&& key !== '_suid'
&& key !== 'id';
});
params = sort(params, function(a, b) {
return a[0].charCodeAt(0) - b[0].charCodeAt(0);
});
return $.param(params).replace(/\+/g, '%20');
}
};
root.uriSync = function(method, model, options) {
var resp = null,
data = URI.parse() || {};
switch (method) {
case "read":
resp = data;
break;
case "create":
case "update":
resp = model.toJSON();
root.History.pushState(resp, window.title, "?" + URI.stringify(resp));
break;
case "delete":
root.History.pushState({}, window.title, "");
break;
}
if (resp) {
options.success(resp);
} else {
options.error("Record not found");
}
};
}(this, _, jQuery);