Friday, 5 February 2016

Using dojo.aspect() programatically

dojo.aspect -It  is used to call any DOM Events or user defined functions before/after trigger of actual DOM Event or functions.


This session i will explain the usage of aspect using functions.
aspect has three methods but most of the time we use only two methods those are after() and before().

aspect.before()-To trigger any DOM Event /function before actual target Event/function loading,
aspect.after()-To trigger any DOM Event /function after actual target Event/function loading.

Syntax:
aspect.before(targetNode,"ActualDOMEvent",before/after DOM Events);

Example Code:

<html >
    <head>
     <title>Dojo Aspect Example</title>
        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.9.1/dijit/themes/claro/claro.css" />
<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.9.2/dojo/dojo.js"></script>
    </head>
    <body class="claro">
      
<script>

require(['dojo/aspect'], function(aspect){

function SuperMethod(){
alert("Super Method called");
console.log("super Method called");
}
function subMethod(){
alert("Sub Method is called");
console.log("sub method is called");
}
function helper(){}
helper.SuperMethod=SuperMethod;
helper.subMethod=subMethod;

//i want to call superMethod first but when i call super method i want to execute my sub method ,so use aspect
aspect.before(helper,"SuperMethod",subMethod);
helper.SuperMethod();
//now after calling the sub method i want to call super method
aspect.after(helper,"subMethod",SuperMethod);
helper.subMethod();



});
</script>
    </body>
</html>




No comments:

Post a Comment