forked from onsi/cocktail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cocktail.js
57 lines (46 loc) · 1.78 KB
/
Cocktail.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
// Cocktail.js 0.1.0
// (c) 2012 Onsi Fakhouri
// Cocktail.js may be freely distributed under the MIT license.
// http://github.com/onsi/cocktail
(function() {
var originalExtend = Backbone.Model.extend;
var mixin = function(klass) {
var mixins = _.flatten(_.rest(arguments));
var collisions = {};
_(mixins).each(function(mixin) {
_(mixin).each(function(value, key) {
if (key == 'events') {
klass.prototype.events = _.extend({}, klass.prototype.events || {}, value);
} else if (_.isFunction(value)) {
if (klass.prototype[key]) {
collisions[key] = collisions[key] || [klass.prototype[key]];
collisions[key].push(value);
}
klass.prototype[key] = value;
}
});
});
_(collisions).each(function(propertyValues, propertyName) {
klass.prototype[propertyName] = function() {
var that = this, args = arguments, returnValue = undefined;
_(propertyValues).each(function(value) {
var returnedValue = _.isFunction(value) ? value.apply(that, args) : value;
returnValue = (returnedValue === undefined ? returnValue : returnedValue);
});
return returnValue;
}
});
}
var extend = function(protoProps, classProps) {
var klass = originalExtend.call(this, protoProps, classProps);
var mixins = klass.prototype.mixins;
if (mixins && klass.prototype.hasOwnProperty('mixins')) {
mixin(klass, mixins);
}
return klass;
}
Backbone.Model.mixin = Backbone.Collection.mixin = Backbone.Router.mixin = Backbone.View.mixin = function() {
mixin(this, _.toArray(arguments));
};
Backbone.Model.extend = Backbone.Collection.extend = Backbone.Router.extend = Backbone.View.extend = extend;
})();