Last active
December 20, 2015 03:19
-
-
Save kalley/6062606 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using lodash so we can use isPlainObject | |
var | |
_ = require('lodash'), | |
Marionette = require('backbone.marionette'); | |
var _constructor = Marionette.View.prototype.constructor; | |
Marionette.View = Marionette.View.extend({ | |
constructor: function (attributes, options) { | |
var self = this; | |
// If mixins is a function, pass in the attributes [model, etc] | |
// If mixins is an object just use that | |
var mixins = this.mixins ? _.isFunction(this.mixins) ? this.mixins(attributes) : this.mixins : null; | |
var collisions = {}; | |
_.each(mixins, function(mixin) { | |
_.each(mixin, function(value, key) { | |
if (_.isPlainObject(value)) { | |
self[key] = _.extend({}, self[key] || {}, value); | |
} else if (_.isFunction(value)) { | |
if (self[key]) { | |
collisions[key] = collisions[key] || [self[key]]; | |
collisions[key].push(value); | |
} | |
self[key] = value; | |
} else if ( ! self[key] ) { | |
self[key] = value; | |
} else if ( value === null) { // This allows completely overwriting a value instead of dealing with a collision | |
self[key] = null; | |
} | |
}); | |
}); | |
_.each(collisions, function(propertyValues, propertyName) { | |
self[propertyName] = function() { | |
var args = [].slice.call(arguments); | |
var returnValue; | |
_.each(propertyValues, _.bind(function(value) { | |
returnValue = ( _.isFunction(value) ? value.apply(this, args) : value ) || returnValue; | |
args.push(returnValue); | |
}, this)); | |
return returnValue; | |
}; | |
}); | |
_constructor.call(this, attributes, options); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment