Skip to content

Instantly share code, notes, and snippets.

@rochnyak-d-i
Created October 28, 2014 05:06
Show Gist options
  • Save rochnyak-d-i/7a60c1ac6914426d8163 to your computer and use it in GitHub Desktop.
Save rochnyak-d-i/7a60c1ac6914426d8163 to your computer and use it in GitHub Desktop.
JS шаблон цепочка обязанностей
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