Skip to content

Instantly share code, notes, and snippets.

@fundon
Created August 27, 2011 04:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fundon/1174982 to your computer and use it in GitHub Desktop.
Save fundon/1174982 to your computer and use it in GitHub Desktop.
JavaScript: Chain Of Responsibility(cor)
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