`
OuYangGod
  • 浏览: 53107 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

dojo 入门资料

阅读更多

    The Dojo Toolkit is an open-source JavaScript toolkit for building great web applications.

 

First Step

    Start by making a index.html file for use as a basic template for any example:

 

	<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" 
	"http://www.w3.org/TR/html4/strict.dtd">
	<html>
		<head>
			<title>Dojo Toolkit Test Page</title>    
			<style type="text/css">
				/* our CSS can go here */    
			</style>    

			<!-- load the dojo toolkit base -->
			<script type="text/javascript" src="dojoroot/dojo/dojo.js"
	    			djConfig="parseOnLoad:true, isDebug:true"></script>

			<script type="text/javascript">
				/* our JavaScript will go here */
			</script>
		</head>
		<body>
			<!-- this is a Typical WebPage starting point ... -->
			<h1 id="testHeading">Dojo Skeleton Page</h1>
			<div id="contentNode">
				<p>Some Content To Replace</p>
			</div>
		</body>
	</html>

 

Configuring Dojo

 

	<script type="text/javascript" src="dojoroot/dojo/dojo.js"
		djConfig="parseOnLoad:true, isDebug:true" />

 

How to start

    dojo.addOnLoad is a fundamental aspect of using dojo, and is very important to remember. Without it, you cannot be sure all the necessary content has been loaded before you own code begins to execute.

 

More Than Just Dojo

    Dojo has a package system built-in to load all the code you need, and is controlled by dojo.require(). This function allows us to pull in parts of the Dojo Toolkit not provided for in the Base dojo.js, such as Drag and Drop, additional animations, Dijit widgets, DojoX projects, or even your own code.

 

    For example, to load the code needed to use the TitlePane widget, and a dijit Button into your page include the modules dijit.TitlePane and dijit.form.Button:

 

	dojo.require("dijit.form.Button");
	dojo.require("dijit.TitlePane");
	dojo.addOnLoad(function()
	{
    		dojo.byId("testHeading").innerHTML = "We're on our way!";
    		console.log("onLoad fires after require() is done"); 
	});  

 

    Each "module" has its own dojo.require()'s, and knows not to load code it already has. Code executed by dojo.addOnLoad doesn't run until after your dojo.require()'s are all finished loading, making it that much safer and more convenient to use.

 

DOM Magic

    A really nice tool Dojo provides is dojo.query. It's a great way to parse all or portions of the Document Object Model (DOM) and access selections of nodes. It really deserves its own book. Each of the following sections will touch on how to use dojo.query more closely, though grasping its potential is as simple as seeing it used:

 

	dojo.require("dojo.NodeList-fx");
	dojo.addOnLoad(function()
	{
		// our dom is ready, get the node:
		dojo.query("#testHeading")
		 // add "testClass" to its class="" attribute
		.addClass("testClass")
		 // and fade it out after 500 ms
		.fadeOut({ delay:500 }).play();
	});
 

    dojo.query returns an instance of a dojo.NodeList, a synthetic super Array of domNodes. It supports most CSS3 selectors and executes code against the whole list of results. To demonstrate this, we're going to need something more than a single heading, so add some content to our DOM:

 

	<h1 id="testHeading">Dojo Skeleton Page</h1>
	<a class="link" href="#">First link</a>
	<a class="link" href="#">Second Link</a>    
	<p class="para">First paragraph</p>
	<p class="para">Second paragraph</p>
	<p class="para">Third paragraph</p>

	and use a different query:

	dojo.require("dojo.NodeList-fx");
	dojo.addOnLoad(function()
	{
		// get each element with class="para"
		dojo.query(".para")
		.addClass("testClass")
		.fadeOut({ delay: 1000 }).play();
	});

 

     All three <p> elements should turn red and fade out after a second delay. The full list of things dojo.NodeList does is impressive, some of which we'll touch on in later sections of this guide.


Events

    The next important concept we are going to cover is interacting with our page. We've already set the heading to some alternate text, but what if we wanted to do something more interesting? Perhaps change it to something else when the user clicks on it? dojo.connect is the one-stop solution for all your event needs:

 

	dojo.addOnLoad(function()
	{
    		var node = dojo.byId("testHeading");
    		dojo.connect(node,"onclick",function(){
			node.innerHTML = "I've been clicked";    	
    		});	    
	});

 

     A convenient way to do the above using dojo.query would be:

 

	dojo.addOnLoad(function(){
    		dojo.query("#testHeading")
		.style("cursor","pointer")
		.connect("onclick",function(){
	    		this.innerHTML = "I've been clicked";    	
		});	    
	});
 

    We can also connect to methods of specific objects and execute them in the same scope. This is useful as you get into declaring classes in Dijit, or creating Animations. Let's create a really simple object with some methods and watch them interact:

 

	var mineObj = {
		aMethod: function(){ 
		console.log('running A');   
		}, 
	bMethod: function(){ 
		console.log('running B'); 
		}    
	}; 
	var otherObj = { 
		cMethod: function(){ 
		console.log('running C');            
		} 
	}; 
	dojo.addOnLoad(function()
	{ 
		// run bMethod() whenever aMethod() gets run 
		dojo.connect(mineObj,"aMethod",mineObj,"bMethod"); 

		// run an entirely different object's method via a separate connection 
		dojo.connect(mineObj,"bMethod",otherObj,"cMethod"); 

		// start chain of events
		mineObj.aMethod(); 
	});

 

     You should see "running A B C" on separate lines in the console.

 

Some examples:

 

       //When obj.onchange(), do ui.update():
	dojo.connect(obj, "onchange", ui, "update");
	dojo.connect(obj, "onchange", ui, ui.update); // same

	//Using return value for disconnect:
	var link = dojo.connect(obj, "onchange", ui, "update");
	...
	dojo.disconnect(link);

	//When onglobalevent executes, watcher.handler is invoked:
	dojo.connect(null, "onglobalevent", watcher, "handler");

	//When ob.onCustomEvent executes, customEventHandler is invoked:
	dojo.connect(ob, "onCustomEvent", null, "customEventHandler");
	dojo.connect(ob, "onCustomEvent", "customEventHandler"); // same

	//When ob.onCustomEvent executes, customEventHandler is invoked with the same scope (this):
	dojo.connect(ob, "onCustomEvent", null, customEventHandler);
	dojo.connect(ob, "onCustomEvent", customEventHandler); // same

	//When globalEvent executes, globalHandler is invoked with the same scope (this):
	dojo.connect(null, "globalEvent", null, globalHandler);
	dojo.connect("globalEvent", globalHandler); // same
 

Some gloss: dojo animations

     Dojo has a powerful animation system, with several pre-made animations for a lot of common use cases. Adding some visual flair to your projects has never been easier, and typically makes the user experience a lot more interesting.

 

    All animations use a single "magic object" as its only parameter. The most important being the node: attribute, a DOM Node on which to apply our animation. Some parameters are optional, and some are for advanced usage. A common setup would look something similar to:

 

	dojo.addOnLoad(function()
	{ 
    		var animArgs = {
		node: "testHeading",
		duration: 1000, // ms to run animation
		delay: 250 // ms to stall before playing
    		};
    		dojo.fadeOut(animArgs).play();
	});

 

    All Animation functions in Base Dojo return a new instance of a dojo._Animation, an object with several common methods and events used for control. The methods used have common names like play(), stop(), pause(), and gotoPercent(). As seen above, we've create a fadeOut Animation and called play() on the returned dojo._Animation, starting the action immediately after creation.

 

Base Animations:

    Animations included in base dojo.js are: fadeIn, fadeOut, animateProperty, and a shorthand version of animateProperty simply called anim. dojo.animateProperty is very powerful, and is the foundation for most advanced animations and other animations in Dojo Core.

 

	dojo.addOnLoad(function()
	{
		var anim1 = dojo.fadeOut({ node: "testHeading", duration:700 });
		var anim2 = dojo.animateProperty({
			node: "testHeading", delay: 1000,
			properties:{
				// fade back in and make text bigger
				opacity: { end: 1 }, fontSize: { end:19, unit:"pt"}
			}
		}); 
		anim1.play();
		anim2.play();	
	});
 

    As seen, dojo.animateProperty will fade the element back in via its opacity property and simultaneously make the text larger. You can animate most any CSS property this way.

 

    dojo.anim is very similar to dojo.animateProperty in function, though differs in several important ways. Instead of using a "magic object" of parameters for all options, dojo.anim accepts ordered parameters. In the first position, a DOM Node or string ID of a DOM Node, acting as the node: parameter. The Second argument passed is an object identical to the properties:  object passed to animateProperty: A dictionary of properties and values to animate.

 

    The most notable difference between anim and animateProperty (and all other Base Animations) is that anim immediately calls play() on the returned Animation instance. This sacrifices a small amount of flexibility when doing advanced animations, but is very convenient for simple situations.

 

	dojo.addOnLoad(function()
	{
		// show it:
		dojo.anim("testHeading", { opacity:0 }, 700);
		dojo.anim("testHeading", {
			opacity: 1, fontSize: { end:19, unit:"pt" }
		}, null, null, 1000); 
	});
 

    The above sample recreates the previous animateProperty example using dojo.anim  The order of parameters passed to dojo.anim are: node, properties, duration, easing, onEnd, and delay. These match names of properties passed to animateProperty, and can be set to null to omit.

 

Additional FX:

    A lot can be done visually with the base animations, especially animateProperty. To keep the size of the base dojo.js down, all the additional animations and tools have been packaged into a single module: dojo.fx which can be optionally called in via dojo.require. Adding the module to your code provides several additional animation methods: dojo.fx.combine, dojo.fx.chain, dojo.fx.wipeIn, dojo.fx.wipeOut and dojo.fx.slideTo.

 

	dojo.require("dojo.fx");
	dojo.addOnLoad(function()
	{
		// slide the node to 75,75
		dojo.fx.slideTo({
			node:"testHeading",
			top:75, left:75
		}).play(); // and play it
	});
 

    dojo.fx.chain and dojo.fx.combine are very useful, too. They run animations in parallel or in sequence, and return a single instance of dojo._Animation to use:

 

	dojo.require("dojo.fx");
	dojo.addOnLoad(function()
	{
    		var anim = dojo.fadeOut({ node: "testHeading" });
    		var anim2 = dojo.fx.slideTo({ node: "testHeading", top:75, left:75 });
    		var result = dojo.fx.combine([anim,anim2]);
    		result.play();	    
	});
 

Animation Events:

    Each dojo._Animation has a series of "events" to tie into for more advanced usage. Going back to the one-stop-event-shop dojo.connect, we can connect to specific actions of the animation and do other things. The most common are onEnd and beforeBegin.

 

	dojo.addOnLoad(function()
	{
    		var anim = dojo.fadeOut({ node: "testHeading" });
    		dojo.connect(anim,"onEnd",function(){
			console.log(" the animation is done ");
    		});
    		dojo.connect(anim,"beforeBegin",function(){
			console.log(" the animation is about to start ");
    		});
    		anim.play();
	});
 

    These events are especially helpful when you want to do things like changing out content while a node is hidden and then fading it back in:

 

	dojo.addOnLoad(function()
	{
    		var anim = dojo.fadeOut({ node: "testHeading" });
    		dojo.connect(anim,"onEnd",function(){
			dojo.byId("testHeading").innerHTML = "replaced after fade!";
			dojo.fadeIn({ node:"testHeading" }).play();
    		});
    		anim.play();
	});
 

    Conveniently, you can pass the event functions as properties to the animation. Using dojo.connect to setup the functions gives us a lot more power, and is typically safer for advanced uses, but sometimes dojo.connect is unnecessary, and it's just lots easier to wrap it all in:

 

	dojo.addOnLoad(function()
	{
		var anim = dojo.fadeOut({
    			node: "testHeading",
    			onEnd: function(){
				dojo.byId("testHeading").innerHTML = "replaced ... ";
				dojo.fadeIn({ node: "testHeading" }).play();
    			}
		}).play();
	});
 

animateProperty:

    Probably the most powerful of the base animations, dojo.animateProperty allows us to easily animate multiple CSS properties simultaneously.

 

    Being a dojo._Animation, animateProperty uses the same arguments as other animations. With the additional object properties: we can define any style property of a node from start: to end:, and optionally using a unit: attribute.

 

    Manipulating our header element to use a new font color, size, and overall opacity is as easy as:

 

	dojo.addOnLoad(function()
	{
		var anim = dojo.animateProperty({
			node:"testHeading",
			duration:700,
			properties: {
				// javascript css names are camelCase (not hyphenated)
				fontSize: { start:12, end:22, unit:"pt" },
				opacity: { start:1, end:0.5 },
				color: { start: "#000", end:"#FFE" }
			},
			delay:100 // be careful of this trailing comma, it breaks IE.
		});
	anim.play();
	});
 

dojo.query Animations:

    Dojo provides another convenient module, dojo.NodeList-fx, which adds additional methods to dojo.query for the available dojo.fx animations. To enable these methods, simply add in the required module:

 

	dojo.require("dojo.NodeList-fx");
	dojo.addOnLoad(function()
	{
		dojo.query("#testHeading").fadeOut().play();
	});
 

    The above gives us the same effect as calling dojo.fadeOut directly, but dojo.query here makes an animation for each of the NodeList elements and combines them into a single dojo._Animation. This can be useful when you have groups of like nodes you want to easily affect (in this case, all the nodes with class="fadeNode").

 

	dojo.require("dojo.NodeList-fx");
	var fadeThem = function(){
		dojo.query(".fadeNode").fadeOut().play();
	}
	dojo.addOnLoad(function(){
		dojo.connect(dojo.byId("testHeading"),"onclick",fadeThem);
	});
 

    Unlike other dojo.query() chains, the NodeList-fx methods return an instance of dojo._Animation, thus preventing further chaining.

 

Animation easing:

    All Animations support an easing: property (or in the case of dojo.anim, the fourth parameter). This function adjusts the values of the animation as it progresses to provide enhanced control over the curve.

 

    Dojo provides a collection of easing functions in an optional module dojo.fx.easing. To use them, simply dojo.require() the module, and pass an easing: parameter to your animation:

 

	dojo.require("dojo.fx.easing");
	dojo.addOnLoad(function()
	{
		dojo.animateProperty({
			node:"testHeading",
			properties:{
				fontSize:72
			},
			easing: dojo.fx.easing.backOut
		}).play();
	});
 

    The function dojo.fx.easing.backOut will create the effect of over-shooting the end value very slightly, and revert back to the final value. The "Out" indicated the effect applies towards the end of the Animation. Alternately, dojo.fx.easing.backIn applies near the beginning of the animation. There are approximately 37 easing functions provided in the module, with a variety of effects.

 

Ajax: Simple Transports

    Ajax is an acronym for "Asynchronous JavaScript and XML", a technology employed to send and receive data on the fly. It can be used to update sections of a website from any number of remote sources and send data to the server and pass responses back and forth, all without ever refreshing the webpage.

 

    Having been versed on some essential Dojo methods, we'll move on to the bread and butter of Ajax: XMLHttpRequest (or XHR for short). Dojo has several XHR methods available using common HTTP verbs: POST, GET, PUT, and DELETE.

 

    To prepare, we need to create a file with some text to load in. Create a file named sample.txt  in your js/ folder with sample text:

 

	I am a <em>remote</em> file.
	We used Ajax to put this text in our page.
 

    And modify the index.html to have some basic markup and style:

	<style type="text/css">
		#container {
			border:1px dotted #b7b7b7;
			background:#ededed;
			width:75px;
			height:55px;
		}
	</style>
	<div id="container" class="box">
		<div id="content">
			I am some Inner Content.
			I am going to be replaced
		</div>
	</div>
 

Getting Data:

    The first stepping stone is dojo.xhrGet, which will return the contents of a GET call on a URL. The XHR methods share a lot of common parameters. Most important are the url:  (our destination) and handleAs: (how we handle what is coming back). When the data arrives, it will be passed to the load: function we define:

 

	var init = function()
	{
		var contentNode = dojo.byId("content");
		dojo.xhrGet({
			url: "js/sample.txt",
			handleAs: "text",
			load: function(data,args){
				// fade out the node we're modifying
				dojo.fadeOut({
					node: contentNode,
					onEnd: function(){
				  		// set the data, fade it back in
				  		contentNode.innerHTML = data; 
				  		dojo.fadeIn({node: contentNode}).play();    
					}
				}).play();
			},
			// if any error occurs, it goes here:
			error: function(error,args){
				console.warn("error!",error);
			}
		});
	}; 
	dojo.addOnLoad(init);
 

    You will notice we've combined techniques above. The content will fade out, be replaced by the received data, and fade back in using methods we've learned before. It was almost too easy.

 

    A single handle: argument can be used instead of load: and error:, handling both success and failure cases in a common function:

 

	var init = function()
	{
		dojo.xhrGet({
			url: "js/sample.txt",
			handleAs: "text",
			handle: function(data,args){
				if(typeof data == "error"){
					console.warn("error!");
					console.log(args);
				}else{
					// the fade can be plugged in here, too
					dojo.byId("content").innerHTML = data;
				}
			}
		});
	};
	dojo.addOnLoad(init);
 

    XHR has limitations. The big one is that url: is not cross-domain: you can't submit the request outside of the current host (eg: to url:"http://google.com"). It is a known limitation and a common mistake when getting excited about Ajax. Dojo provides alternatives like dojo.io.iframe and dojo.io.script for more advanced usage.

 

Sending Data:

    All Dojo XHR methods are bi-directional. The only difference is the method. Using dojo.xhrPost, we use the POST method, embedding the data in the request (as opposed to the query string as with dojo.xhrGet). The data can be set directly as an object passed to the content: parameter:

 

	dojo.addOnLoad(function()
	{
		dojo.xhrPost({
			url:"submit.html",
			content: {
				"key":"value",
				"foo":42,
				"bar": {
					"baz" :"value"    
				}
			},
			load: function(data,ioargs){
				console.log(data);
			}
		});
	});
 

    Or more commonly, conveniently converted from a form: parameter. First, make a simple unobtrusive form in the index.html:

 

	<form id="mainForm" action="sample.jsp" method="post">
		<label for="firstName">Name: </label>
		<input type="text" id="firstName" name="firstName" value="Enter Name" />
		<input type="submit" value="submit" />
	</form>
 

    Then, add in some JavaScript to submit the form by using dojo.connect to listen to the native onSubmit event and then post the contents of the form to an alternate URL:

 

	var formSubmit = function(e)
	{
		// prevent the form from actually submitting
		e.preventDefault(); 
		// submit the form in the background	
		dojo.xhrPost({
			url: "sample.jsp",
			form: "mainForm",
			handleAs: "text",
			handle: function(data,args){
				if(typeof data == "error"){
					console.warn("error!",args);
				}else{
					// show our response 
					console.log(data);
				}
			}
		});
	};
	dojo.addOnLoad(function()
	{
		var theForm = dojo.byId("mainForm");
		// another dojo.connect syntax: call a function directly	
		dojo.connect(theForm,"onsubmit",formSubmit);
	}); 
 

    An example sample.jsp would look like:

 

	<% response.setContentType("text/plain"); %>
	Hello <%= request.getParameter("firstName") %> , welcome to the world of Dojo!
 

Object Data:

    Getting text back from the server is nice, but the really great stuff comes when you start passing JavaScript objects around. Using a different handleAs: attribute, we can alter how Dojo handles the response data. Make a new file named sample.json to load:

 

	{
		foo: "bar",
		name: "SitePen",
		aFunction: function()
		{
			alert("internal function run");	    
		},
		nested:
		{
	    		sub: "element",
	    		another: "subelement"
		}
	}
 

    We'll target our xhrPost url: to the new file, and supply a handleAs: "json" parameter to convert the response data to an actual object we can use:

 

	var postData = function()
	{
		dojo.xhrPost({
			url: "sample.json",
			handleAs: "json",
			load: function(data,ioargs){
		  		// success: set heading, run function
		  		dojo.byId("testHeading").innerHTML += " by: "+data.name;
		  		if(data.aFunction && data.aFunction()){
		    			// we just ran data.aFunction(). should alert() ... 
		  		} 
			}
		});
	};
	dojo.addOnLoad(postData);
 

    This allows us to send literally any kind of data back and forth across the wire, without ever interrupting the user experience.

分享到:
评论

相关推荐

    dojo入门资料

    内容很丰富,给有需要的人,壮大dojo社区

    dojo入门实例介绍

    文章用几个简单的实例,让初学者了解dojo的相关知识,和如何使用dojo的相关知识.

    图书:Dojo入门

    NULL 博文链接:https://jianshi-dlw.iteye.com/blog/1174693

    dojo入门系列教程.rar

    dojo入门系列教程,包含入门简介,在javascript基础上介绍dojo的语法特色,ajax的dojo包装---xhr框架的编程要点, Dojo 事件机制.以及对dojo最具特色的web UI设计的全面介绍.

    Dojo 入门 + Dojo 工具包系列 + Dojo 使用技巧 ......

    随着富互联网应用程序(Rich Internet Application,RIA)的发展,一批 JavaScript Toolkit 也随之兴起 , Dojo 便是其中的优秀代表,本系列文章 将由浅入深带领读者走进 Dojo 的世界。本文作为这个系列的开篇 , 主要...

    Dojo学习资料 入门教材

    Dojo入门教程之dojo.io.bind详解 Post的例子 dojo.io.queueBind 详解

    Dojo入门指南-中文版

    Dojo入门指南-中文版 讲的很详细 面向对象编程这和jquery有很大的差别

    dojo快速入门文档

    dojo快速入门文档,该文档可以使初学者快速入门,领略dojo的风采。

    dojo权威入门教程

    dojo权威入门教程

    Dojo入门手册(中文版).pdf

    dojo 入门的选手可以看看。 必有收益啊。

    Dojo入门手册.rar

    3 设置和配置Dojo................................. 4 3.1 选择正确的Dojo........... 4 3.1 选择正确的Dojo创建.............4 3.2 动态加载 package....5 3.3 定制的创建............. 5 4 应用:旅行路线...

    dojo快速入门(中文版)

    dojo官方网站上Dojo Quick Start的中文版 英文版原址:http://sitepen.com/labs/guides/?guide=DojoQuickStart

    dojo1.8.chm+dojo1.11中文入门pdf

    dojo1.8.chm + dojo1.11中文入门手册pdf,分享给有需要的同学

    dojo快速入门.doc

    dojo Toolkit(一个开源的js工具包)用于构造web应用。它通过提供设计良好的api和工具包缩短了实现设计的时间。它是轻量/健壮的,提供工具来实现DOM操作,动画,ajax,event 和键盘标准化, 国际化I18N,可访问性(a11y)....

    dojo控件的使用和入门心得

    dojo控件的使用大幅度客观解放大概发广泛广泛广泛广泛广泛

    dojo的学习资料

    资源包中含有: 1.Dojo+API.doc 2.dojo快速入门.doc 3.Dojo中文手册.pdf 4.精通Dojo(中文版).pdf 5.Dojo案例:比如 ValidationTextBox、NumberTextBox、ContentPanel、dojoGrid等小例子。

    geting started with dojo

    sitepen流出的学习dojo入门好资料,让大家轻松走入dojo得世界

    dojo1.8文档(dojo1.8 reference-guide)

    dojo 1.8并没有提供reference-guide的下载,但是dojo官网在国外,有的时候访问速度真的很慢。reference-guide又是开发中使用频率相当高的文档,我整理了一下供大家下载,很全面。希望对大家有帮助, 不要问我怎么...

    Dojo快速入门教程

    Dojo是一个强大的面向对象JavaScript框架。主要由三大模块组成:Core、Dijit、DojoX。Core提供Ajax,events,packaging,CSS-based querying,animations,JSON等相关操作API。Dijit是一个可更换皮肤,基于模板的WEB UI...

    dojo新手快速入门教程-首发高清PDF中文版

    dojo新手快速入门教程-高清PDF中文版

Global site tag (gtag.js) - Google Analytics