Wednesday 27 January 2016

Creating user defined classes(Modules) in DOJO

This session will describe how to create  a user defined class.

Dojo provides a module to define user specific class ,the module name is "declare" .

now lets create a sample class using dojo.

define(["dojo/_base/declare"],function(declare){
return declare("UserDefinedClass",[],{

//define a name to be print
className:"",
constructor:function(){
console.log("Constructor is called !! ");
},
setClassName: function(className){
this.className=className;
},
getClassName:function(){
console.log("ClassName is "+this.className);
}

});

});

save file as UserDefinedClass.js ,means we created our own module with a name UserDefinedClass ,this class is available in your entire package from now and  we can include this module as like other predefined module provided by dojo for us.

using UserDefinedClass Module.

<html>
<head>
<!--Load Dojo Configurations-->
<script>
var djConfig={
isDebug:true,
parseOnLoad:true
};
</script>
<!--Load main dojo script-->
<script
type="text/javascript"
src="./dojo/dojo.js"
>
</script>
</head>
<body>
<span>This Code Example shows how to create a user defined classes using dojo.</span>
<!--Load Dojo Script-->
<script>
require(["UserDefinedClass","dojo/parser"],function(userdefined,parser){
parser.parse();//parses all widgets if there are any widgets
var object=new userdefined();
//set name of class form here
object.setClassName("sample class");
//log the name of class by calling getClassName method
object.getClassName();
});
</script>
</body>
</html>


In the above code we used our custom module using require() module provide by dojo for us.


OutPut:

No comments:

Post a Comment