Dojo - how to override a method for all instances without inheritance?

http://stackoverflow.com/questions/10982161/dojo-how-to-override-a-method-for-all-instances-without-inheritance#10982161

by schlomm

HTML

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.7.2/dijit/themes/claro/claro.css">
<button
    data-dojo-type="dijit.form.Button"
    data-dojo-props="topic: 'test'"
>
    Test #1
</button>

<button
    data-dojo-type="dijit.form.Button"
    data-dojo-props="topic: 'not-test'"
>
    Not test
</button>

<button
    data-dojo-type="dijit.form.Button"
>
    Not test
</button>

<button
    data-dojo-type="dijit.form.Button"
    data-dojo-props="topic: 'test'"
>
    Test #2
</button>

CSS

body {
    padding: 30px;
}

JavaScript

require([
    "dojo/aspect",
    "dojo/topic",
    "dijit/form/Button"
], function(
    aspect,
    topic,
    Button
) {
 
    aspect.after(Button.prototype, "_onClick", function(e) {
        this.topic && topic.publish(this.topic);
    });

    topic.subscribe("test", function() {
        console.log("test topic");         
    });
      
});