【dojo】 on/emit subscribe/publish
最近dojo evneted的两个概念一直没搞清楚,跟同事打赌,我自己肯定能查得到,结果,看了其他人的代码加上官网翻了一遍,算是有点理解了。
Argument | Type | Description |
---|---|---|
target | Object | This is the target object (a DOM node or other event emitting object) that will be the source of the event. The target object may be a host object with its own event capabilities (like DOM elements or the window), or it may be a JavaScript object with an on() method. |
type | String | This is the name of the event type to be dispatched (like select). This event may be a standard event (like click) or a custom event (like finished). |
event | Object |
This is an object with the properties of the event to be dispatched. Generally you should align your properties with W3C standards. Two properties are of particular importance:
|
Name | Type | Description |
---|---|---|
target | Element|Object |
This is the target object or DOM element that to receive events from |
type | String|Function |
This is the name of the event to listen for or an extension event type. |
listener | Function |
This is the function that should be called when the event fires. |
dontFix | undefined |
this.emit("createJob",{});
}
alert(0);
});
<button id="alertButton">Alert the user</button>
<button id="createAlert">Create another alert button</button>
----------------------------------------------------------------------------
<script>
require(["dojo/on", "dojo/topic", "dojo/dom-construct", "dojo/dom", "dojo/domReady!"],
function(on, topic, domConstruct, dom) {
var alertButton = dom.byId("alertButton"),
createAlert = dom.byId("createAlert");
on(alertButton, "click", function() {
// When this button is clicked,
// publish to the "alertUser" topic
topic.publish("alertUser", "I am alerting you.");
});
on(createAlert, "click", function(evt){
// Create another button
var anotherButton = domConstruct.create("button", {
innerHTML: "Another alert button"
}, createAlert, "after");
// When the other button is clicked,
// publish to the "alertUser" topic
on(anotherButton, "click", function(evt){
topic.publish("alertUser", "I am also alerting you.");
});
});
// Register the alerting routine with the "alertUser" topic.
topic.subscribe("alertUser", function(text){
alert(text);
});
});
</script>