Debian Codegraph @

Cocktail.js

raw symbols SCIP
1 // (c) 2012 Onsi Fakhouri
2 // Cocktail.js may be freely distributed under the MIT license.
3 // http://github.com/onsi/cocktail
4 (function(factory) {
5 if (typeof require === 'function' && typeof module !== 'undefined' && module.exports) {
6 module.exports = factory(require('underscore'));
7 } else if (typeof define === 'function') {
8 define(['underscore'], factory);
9 } else {
10 this.Cocktail = factory(_);
11 }
12 }(function (_) {
13
14 var Cocktail = {};
15
16 Cocktail.mixins = {};
17
18 Cocktail.mixin = function mixin(klass) {
19 var mixins = _.chain(arguments).toArray().slice(1).flatten().value();
20 // Allows mixing into the constructor's prototype or the dynamic instance
21 var obj = klass.prototype || klass;
22
23 var collisions = {};
24
25 _.each(mixins, function(mixin) {
26 if (_.isString(mixin)) {
27 mixin = Cocktail.mixins[mixin];
28 }
29 _.each(mixin, function(value, key) {
30 if (_.isFunction(value)) {
31 // If the mixer already has that exact function reference
32 // Note: this would occur on an accidental mixin of the same base
33 if (obj[key] === value) return;
34
35 if (obj[key]) {
36 // Avoid accessing built-in properties like constructor (#39)
37 collisions[key] = collisions.hasOwnProperty(key) ? collisions[key] : [obj[key]];
38 collisions[key].push(value);
39 }
40 obj[key] = value;
41 } else if (_.isArray(value)) {
42 obj[key] = _.union(value, obj[key] || []);
43 } else if (_.isObject(value)) {
44 obj[key] = _.extend({}, value, obj[key] || {});
45 } else if (!(key in obj)) {
46 obj[key] = value;
47 }
48 });
49 });
50
51 _.each(collisions, function(propertyValues, propertyName) {
52 obj[propertyName] = function() {
53 var that = this,
54 args = arguments,
55 returnValue;
56
57 _.each(propertyValues, function(value) {
58 var returnedValue = _.isFunction(value) ? value.apply(that, args) : value;
59 returnValue = (typeof returnedValue === 'undefined' ? returnValue : returnedValue);
60 });
61
62 return returnValue;
63 };
64 });
65
66 return klass;
67 };
68
69 var originalExtend;
70
71 Cocktail.patch = function patch(Backbone, patchAdditional) {
72 var patchList = [Backbone.Model, Backbone.Collection, Backbone.Router, Backbone.View].concat(patchAdditional || []);
73 originalExtend = Backbone.Model.extend;
74
75 var extend = function(protoProps, classProps) {
76 var klass = originalExtend.call(this, protoProps, classProps);
77
78 var mixins = klass.prototype.mixins;
79 if (mixins && klass.prototype.hasOwnProperty('mixins')) {
80 Cocktail.mixin(klass, mixins);
81 }
82
83 return klass;
84 };
85
86 _.each(patchList, function(klass) {
87 klass.mixin = function mixin() {
88 Cocktail.mixin(this, _.toArray(arguments));
89 };
90
91 klass.extend = extend;
92 });
93 };
94
95 Cocktail.unpatch = function unpatch(Backbone) {
96 _.each([Backbone.Model, Backbone.Collection, Backbone.Router, Backbone.View], function(klass) {
97 klass.mixin = undefined;
98 klass.extend = originalExtend;
99 });
100 };
101
102 return Cocktail;
103 }));