Created
August 27, 2011 04:32
-
-
Save fundon/1174982 to your computer and use it in GitHub Desktop.
JavaScript: Chain Of Responsibility(cor)
This file contains 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 NO_TOPIC = -1; | |
var Topic; | |
function Handler(s, t) { | |
this.successor = s || null; | |
this.topic = t || 0; | |
} | |
Handler.prototype = { | |
handle: function () { | |
if (this.successor) { | |
this.successor.handle() | |
} | |
}, | |
has: function() { | |
return this.topic != NO_TOPIC; | |
} | |
}; | |
var _handle = Handler.prototype.handle; | |
var app = new Handler({ | |
handle: function () { | |
console.log('app handle'); | |
} | |
}, 3); | |
app.name = 'app'; | |
var dialog = new Handler(app, 1); | |
dialog.handle = function () { | |
//if (this.has()) { | |
//} else { | |
//console.log('dialog handle'); | |
console.log('dialog before ...') | |
_handle.call(this); | |
console.log('dialog after ...') | |
//} | |
}; | |
dialog.name = 'dialog'; | |
var button = new Handler(dialog, 2); | |
button.handle = function () { | |
//console.log('button handle'); | |
console.log('button before ...') | |
_handle.call(this); | |
console.log('button after ...') | |
}; | |
button.name = 'button'; | |
console.log(button) | |
button.handle(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment