-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone.crudr.js
134 lines (103 loc) · 2.97 KB
/
backbone.crudr.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
122
123
124
125
126
127
128
129
130
131
132
133
134
(function(Backbone, _){
var origSync = Backbone.sync;
var Helpers = {
// Backbone.js support
// Listen for backend notifications and update the
// collection models accordingly.
bindBackend: function() {
var self = this;
if( ( typeof(this.model) == "undefined" ) ) {
// this is a model
this.backend.ready(function() {
var event = self.backend.options.event;
self.bind(event + ':create', function(response) {
self.parse(response);
});
self.bind(event + ':update', function(attributes) {
self.set(attributes);
});
self.bind(event + ':delete', function() {
self.destroy();
});
});
} else {
// this is a collection
//var idAttribute = ( typeof(this.model) == undefined ) ? this.prototype.idAttribute : this.model.prototype.idAttribute;
var idAttribute = this.model.prototype.idAttribute || this.prototype.idAttribute;
this.backend.ready(function() {
var event = self.backend.options.event;
self.bind(event + ':create', function(model) {
self.add(model);
});
self.bind(event + ':update', function(model) {
var item = self.get(model[idAttribute]);
if (item) item.set(model);
});
self.bind(event + ':delete', function(model) {
self.remove(model[idAttribute]);
});
});
}
}
};
function inherit(Parent, Child, mixins) {
var Func = function() {};
Func.prototype = Parent.prototype;
mixins || (mixins = [])
mixins.forEach(function(mixin) {
_.extend(Func.prototype, mixin);
});
Child.prototype = new Func();
Child.prototype.constructor = Child;
return _.extend(Child, Parent);
};
function extend(Parent) {
// Override the parent constructor
var Child = function() {
if (this.backend) {
this.backend = crudr.subscribe({
el : this,
name : this.backend
});
// setup events
this.bindBackend();
}
Parent.apply(this, arguments);
};
// Inherit everything else from the parent
return inherit(Parent, Child, [Helpers]);
};
Backbone.sync = function(method, model, options) {
var backend = model.backend || (model.collection && model.collection.backend);
if(backend && backend.ready ) {
// we have sockets...
var error = options.error || function() {};
var success = options.success || function() {};
// Don't pass the callbacks to the backend
delete options.error;
delete options.success;
// Use CRUDr backend
backend.ready(function() {
var req = {
name: backend.name,
method: method,
model: model.toJSON(),
options: options
};
crudr.sync(req, { error: error, success: success } );
});
} else if( backend ) {
// edge case when the sync is loaded before the initialization of the backend is complete
setTimeout(function(){
Backbone.sync(method, model, options);
}, 200);
} else {
// no sockets...
// Call the original Backbone.sync
origSync(method, model, options);
}
};
Backbone.Collection = (extend)(Backbone.Collection);
Backbone.Model = (extend)(Backbone.Model);
return Backbone;
})(Backbone, _);