Skip to content

Instantly share code, notes, and snippets.

@heymichaelp
Last active August 29, 2015 14:04
Show Gist options
  • Save heymichaelp/b3e25f0e60954ffb61b8 to your computer and use it in GitHub Desktop.
Save heymichaelp/b3e25f0e60954ffb61b8 to your computer and use it in GitHub Desktop.
Decorators: Example Decorator
var EmailReportCardToParent = function(obj) {
this.obj = obj
this.decorate()
return obj
};
EmailReportCardToParent.prototype = _.extend(EmailReportCardToParent.prototype, {
// specify the method that you want to decorate
methodToDecorate: 'saveReportCardAsPDF',
decorate: function() {
var self = this
// store the original function for use in a closure
var originalFn = this.obj[this.methodToDecorate]
// define the decorated function, which captures the originalFn in its closure
var decoratedFn = function(html) {
// call the decorator method with the result of the originalFn as the argument
var res = originalFn(html)
self.sendPdfAsEmail(res)
return res;
};
// override the method on the object with the new decoratedFn
this.obj[this.methodToDecorate] = decoratedFn
},
sendPdfAsEmail: function() {
// some actions here to
// send email to parent
}
});
// Example usage
new EmailReportCardToParent(new GenerateReportCard())).run()
@joezimjs
Copy link

I think you have a couple too many closing parenthesis on the last line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment