Skip to content

Instantly share code, notes, and snippets.

@RickCarlino
Forked from partkyle/delegates.coffee
Created May 31, 2013 04:47
Show Gist options
  • Save RickCarlino/5682984 to your computer and use it in GitHub Desktop.
Save RickCarlino/5682984 to your computer and use it in GitHub Desktop.
class Something
constructor: ->
@delegateObject = new Messager
class Messager
testing: (int) ->
console.log int
woot: (bool) ->
console.log bool
nothing: (array) ->
console.log array
Object::delegates = (methods, to) ->
methods.forEach (method) =>
this::[method] = (args...) ->
@[to][method].apply(this, args)
Something.delegates(['testing','woot','nothing','missing'], 'delegateObject')
s = new Something
s.testing(1)
s.woot(false)
s.nothing([1,2,4])
var Something, Messager;
Something = (function() {
function Something() {
this.delegateObject = new Messager;
}
return Something;
})();
Messager = (function() {
function Messager() {}
Messager.prototype.testing = function(int) {
return console.log(int);
};
Messager.prototype.woot = function(bool) {
return console.log(bool);
};
Messager.prototype.nothing = function(array) {
return console.log(array);
};
return Messager;
})();
Object.prototype.delegates = function(methods, to) {
var _this = this;
return methods.forEach(function(method) {
return _this.prototype[method] = function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return this[to][method].apply(this, args);
};
});
};
Something.delegates(['testing', 'woot', 'nothing', 'missing'], 'delegateObject');
s = new Something;
s.testing(1);
s.woot(false);
s.nothing([1, 2, 4]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment