DOJO 8 event dojo/on
8280 ワード
公式サイトチュートリアル:Events with Dojo要素にeventsをバインドし、パッケージdojo/onを参照し、onメソッドで実現する必要があります.
<
button
id
=
"myButton"
>Click me!</
button
>
<
div
id
=
"myDiv"
>Hover over me!</
div
>
require([
"dojo/on"
,
"dojo/dom"
,
"dojo/dom-style"
,
"dojo/mouse"
,
"dojo/domReady!"
],
function
(on, dom, domStyle, mouse) {
var
myButton = dom.byId(
"myButton"
),
myDiv = dom.byId(
"myDiv"
);
on(myButton,
"click"
,
function
(evt){
domStyle.set(myDiv,
"backgroundColor"
,
"blue"
);
});
on(myDiv, mouse.enter,
function
(evt){
domStyle.set(myDiv,
"backgroundColor"
,
"red"
);
});
var handler =
on(myDiv, mouse.leave,
function
(evt){
domStyle.set(myDiv,
"backgroundColor"
,
""
);
});
handler.remove();// event
on.once(
myDiv, mouse.leave,
function
(evt){
domStyle.set(myDiv,
"backgroundColor"
,
""
);
});
});
on 。 。
: events
:event
: event , , event , , , 。
on , remove , , event。
on.once(element,event name,handler), on , , handler remove。
events, event
。
---------------------------------- require([
"dojo/on"
,
"dojo/dom"
,
"dojo/_base/lang"
,
"dojo/domReady!"
],
function
(on, dom, lang) {
var
myScopedButton1 = dom.byId(
"myScopedButton1"
),
myScopedButton2 = dom.byId(
"myScopedButton2"
),
myObject = {
id:
"myObject"
,
onClick:
function
(arg){
alert(
"The scope of this handler is "
+ this.id + " , value = " + arg
);
}
};
// This will alert "myScopedButton1"
on(myScopedButton1,
"click"
, myObject.onClick);
// This will alert "myObject" rather than "myScopedButton2"
on(myScopedButton2,
"click"
, lang.hitch(myObject,
"onClick", "something"
));
});
handler , , 。
handler object.method, , method this 。
handler lang.hitch(object,method name,[arg1,[arg2,.....]]) , N , method this object。
define(["dojo/aspect"], function(aspect){
aspect.after(dojo, "xhr", function(deferred){
// this is called after any dojo.xhr call
});
// this will execute the original dojo.xhr method and then our advising function
dojo.xhr("GET", {...});
});
define(["dojo/aspect"], function(aspect){
aspect.before(dojo, "xhr", function(method, args){
// this is called before any dojo.xhr call
if(method == "PUT"){
// if the method is PUT, change it to a POST and put the method in the parameter string
args.url += "?x-method=PUT";
// return the new args
return ["POST", args];
}
});
// this will execute the original our advising function and then dojo.xhr
dojo.xhr("PUT", {...});
});
define(["dojo/aspect"], function(aspect){
aspect.around(dojo, "xhr", function(originalXhr){
return function(method, args){
// doing something before the original call
var deferred = originalXhr(method, args);
// doing something after the original call
return deferred;
}
});
dojo.xhr("PUT", {...});
});