Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hooks in sub classes #22

Open
absolux opened this issue Mar 27, 2016 · 0 comments
Open

hooks in sub classes #22

absolux opened this issue Mar 27, 2016 · 0 comments

Comments

@absolux
Copy link

absolux commented Mar 27, 2016

What about using hooks in base and sub classes ?
Look at this use case

var hooks = require("hooks")
var _ = require('underscore')

// Document definition
var Document = function() {}
_.extend(Document, hooks)
Document.prototype.save = function () { console.log('save') }
Document.pre('save', function(next) { console.log('pre:save'); next() })

// Book definition
var Book = function() {}
_.extend(Book, Document)
_.extend(Book.prototype, Document.prototype)
Book.post('save', function(next) { console.log('post:save'); next() })

// Page definition
function Page() {}
_.extend(Page, Document)
_.extend(Page.prototype, Document.prototype)

// test
var page = new Page
page.save()

calling page's save method will output

"pre:save"
"save"
"post:save"

the post handler is registered within books, but still invoked in all documents see it in action

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant