Last active
August 29, 2015 14:04
-
-
Save heymichaelp/b3e25f0e60954ffb61b8 to your computer and use it in GitHub Desktop.
Decorators: Example Decorator
This file contains hidden or 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
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you have a couple too many closing parenthesis on the last line.