_.wrapAll plugin for underscore
HTML
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.2.2/underscore-min.js"></script>
JavaScript
_.wrapAll = function( proto, func ) {
_.each( proto, function( value, key, list ) {
_.wrap( value, function( f ) {
func.call( this );
f.apply( this, arguments );
});
/*
var name = key, origFunc = value;
Player.prototype[name] = _.bind(function () {
func.call(this);
return value.apply(this, arguments);
}, proto);
*/
});
};
var Player = function (name, opts) {
this.someThing = {};
_.wrapAll(this, this.checkOpen);
};
$.extend(Player.prototype, {
open: function () {
console.log('before firing open...');
},
close: function (x) {
console.log(x);
console.log('then close will fire with someThing defined again...')
console.log('basically to ensure that this.someThing is always defined.');
},
checkOpen: function () {
if (this.someThing) {
console.log('I want checkOpen to fire when something is true...');
}
else {
this.someThing = {};
console.log('And fire this when it\'s null...');
}
}
});
var player = new Player();
player.open();
player.someThing = null;
player.close(2);