dojo中的AMD模式开发案例
生活随笔
收集整理的這篇文章主要介紹了
dojo中的AMD模式开发案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
教程里主要定義了兩個方法,setText設置文本內容和restoreText重置文本內容。
這兩個方法通過 dojo.define這個方法來定義。 // In demo/myModule.js (which means this code defines// the "demo/myModule" module):?define([????// The dojo/dom module is required by this module, so it goes????// in this list of dependencies.????"dojo/dom"], function(dom){????// Once all modules in the dependency list have loaded, this????// function is called to define the demo/myModule module.????//????// The dojo/dom module is passed as the first argument to this????// function; additional modules in the dependency list would be????// passed in as subsequent arguments.?????varoldText = {};?????// This returned object becomes the defined value of this module????return{????????setText:function(id, text){????????????varnode = dom.byId(id);????????????oldText[id] = node.innerHTML;????????????node.innerHTML = text;????????},????????restoreText:function(id){????????????varnode = dom.byId(id);????????????node.innerHTML = oldText[id];????????????deleteoldText[id];????????}????};});
define 方法引用了必要的類,就像java 中的 import;并且還包含了對業務邏輯的實現,
并通過return來返回,以上就是myModule.js文件的全部內容。
那么在html頁面里,通過dojo.require來加載上面的js代碼,
// Require the module we just createdrequire(["demo/myModule"],function(myModule){????// Use our module to change the text in the greeting????myModule.setText("greeting","Hello Dojo!");?????// After a few seconds, restore the text to its original state????setTimeout(function(){????????myModule.restoreText("greeting");????}, 3000);});
在這里暫且先總結為:dojo.define 方法僅用于定義和聲明,dojo.require 方法僅用于異步加載js文件 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
這兩個方法通過 dojo.define這個方法來定義。 // In demo/myModule.js (which means this code defines// the "demo/myModule" module):?define([????// The dojo/dom module is required by this module, so it goes????// in this list of dependencies.????"dojo/dom"], function(dom){????// Once all modules in the dependency list have loaded, this????// function is called to define the demo/myModule module.????//????// The dojo/dom module is passed as the first argument to this????// function; additional modules in the dependency list would be????// passed in as subsequent arguments.?????varoldText = {};?????// This returned object becomes the defined value of this module????return{????????setText:function(id, text){????????????varnode = dom.byId(id);????????????oldText[id] = node.innerHTML;????????????node.innerHTML = text;????????},????????restoreText:function(id){????????????varnode = dom.byId(id);????????????node.innerHTML = oldText[id];????????????deleteoldText[id];????????}????};});
define 方法引用了必要的類,就像java 中的 import;并且還包含了對業務邏輯的實現,
并通過return來返回,以上就是myModule.js文件的全部內容。
那么在html頁面里,通過dojo.require來加載上面的js代碼,
// Require the module we just createdrequire(["demo/myModule"],function(myModule){????// Use our module to change the text in the greeting????myModule.setText("greeting","Hello Dojo!");?????// After a few seconds, restore the text to its original state????setTimeout(function(){????????myModule.restoreText("greeting");????}, 3000);});
?
上面的代碼通常放在<body>內執行。在這里暫且先總結為:dojo.define 方法僅用于定義和聲明,dojo.require 方法僅用于異步加載js文件 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的dojo中的AMD模式开发案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dojo中的dojoConfig配置
- 下一篇: dojo中的dojo/dom-const