Created
October 25, 2012 22:03
-
-
Save jordansjones/3955736 to your computer and use it in GitHub Desktop.
Mixin stuff
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
{ | |
extend: function (target) { | |
var clone = _.clone(this); | |
_.extend(clone, target); | |
return clone; | |
}, | |
beforeMixin: function (target) {}, | |
afterMixin: function (target) {}, | |
addMixins: function(target) { | |
var mixins = this.mixins || []; | |
this.beforeMixin(target); | |
_.each(mixins, function (mixin) { | |
var obj = (target.constructor && target.constructor.__super__) || target; | |
if (!obj[mixin]) { | |
obj[mixin] = this[mixin]; | |
} | |
_.bind(obj[mixin], target); | |
}, this); | |
this.afterMixin(target); | |
} | |
} |
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
BaseMixin.extend({ | |
mixins: ['initializeKeybindings', 'mixinKeybindings', 'extendKeybindings', 'setKeybindingScope'], | |
beforeMixin: function (target) { | |
target.keybindings = target.keybindings || {keys: {}}; | |
}, | |
afterMixin: function (target) { | |
target.initializeKeybindings(); | |
}, | |
initializeKeybindings: function () { | |
var sc = this.keybindings.scope && _.isString(this.keybindings.scope) ? this.keybindings.scope : 'all', | |
bindings = this.keybindings.keys || {}; | |
if (_.isFunction(bindings)) { bindings = bindings.call(this); } | |
this.mixinKeybindings(); | |
_.each(this.keybindings.keys, function (methodName, key) { | |
var method = methodName; | |
if (_.isString(method)) { | |
method = this[method]; | |
if (!method) { | |
throw new Error("View method '" + methodName + "' was configured as a keybinding handler [" + key + "], but does not exist."); | |
} | |
} | |
method = _.bind(method, this); | |
if (sc !== null) { | |
Keymaster(key, sc, method); | |
} | |
else { | |
Keymaster(key, method); | |
} | |
}, this); | |
}, | |
mixinKeybindings: function () {}, | |
extendKeybindings: function (keybindings) { | |
if (keybindings.scope) { | |
this.keybindings.scope = keybindings.scope; | |
} | |
if (keybindings.keys) { | |
_.extend(this.keybindings.keys, keybindings.keys); | |
} | |
}, | |
setKeybindingScope: function(scope) { | |
if (scope && _.isString(scope)) { | |
Keymaster.setScope(scope); | |
} | |
} | |
}); |
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
Marionette.ItemView.extend({ | |
constructor: function () { | |
Mixins.addAll(this); | |
Marionette.ItemView.prototype.constructor.apply(this, arguments); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment