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