锘??xml version="1.0" encoding="utf-8" standalone="yes"?> In software development, a framework is a defined
support structure in which other project can be organized and
developed. A framework typically consists of several smaller
components; support programs, libraries, and a scripting language.
There may also be other software involved to aid in development and
meshing of the different components of a project. As you can see, dojo
could be part of a framework, but it isn't a framework in itself. 鍦ㄨ蔣浠跺紑鍙戜腑錛屼竴涓鏋舵槸涓涓瀹氫箟鐨勬敮鎸佺粨鏋勶紝鍦ㄥ叾涓叾浠栭」鐩彲浠ヨ緇勭粐鍜屽紑鍙戙備竴涓鏋朵唬琛ㄦу湴鍖呭惈闇瑕佸皬鐨勭粍浠躲佹敮鎸佺▼搴忋佺被搴撱佸拰涓涓剼鏈?
璇█銆傝繖浜涗篃鍙兘鏄叾浠栬蔣浠跺寘鎷府鍔╁紑鍙戝拰涓嶅悓鐨勯」鐩粍浠剁紪鍒躲傚浣犵湅鍒扮殑錛宒ojo鍙兘鏄鏋剁殑涓閮ㄥ垎錛屼絾鏄湰璐ㄤ笂瀹冩湰韜笉鏄竴涓鏋躲?br /> A
library is defined as a collection of related functions and subroutines
used to develop software. They are distinguished from executables in
that they are not independant programs; rather, they are "helper" code
that provide access to common functions in one easy to manage location.
After reading this you are probably saying, "Hey! dojo is a collection
of libraries!", and you would be correct; however, dojo is much more
than just a collection of libraries. Now on to
toolkits. A toolkit is generally used in reference to graphical user
interface (GUI) toolkits. Basically, a library that is mainly focused
on creating a GUI. Yes, dojo could also fall under this category, in
fact our name implies it. Why do we call dojo a toolkit? Certainly not
because it focuses mainly on GUI development, right? Well quite simply,
because dojo is so much more than just a collection of libraries. 鐜板湪璇磋toolkits.
涓涓伐鍏烽氬父琚敤鍦ㄥ弬鑰冨浘褰㈢敤鎴風(fēng)晫闈㈠伐鍏楓傚熀紜鍦幫紝涓涓被搴撲富瑕侀泦涓湪鍒涘緩涓涓狦UI銆傛槸鍦幫紝dojo鍙褰掑叆榪欎釜綾誨埆錛屽疄闄呬笂鎴戜滑鍛藉悕鏆楃ず浜嗗畠錛屼負(fù)浠涔?
鎴戜滑鍙玠ojo鏄竴涓伐鍏鳳紵褰撶劧涓嶆槸鍥犱負(fù)瀹冧富瑕侀泦涓湪GUI寮鍙戯紝姝g‘涔堬紵寰堝ソ錛屽叾瀹炲緢綆鍗曪紝鍥犱負(fù)pojo涓嶄粎浠呮槸涓涓簱鐨勯泦鍚堛?br />Library錛堝簱錛?/h2>
搴撹瀹氫箟涓轟竴涓浉鍏沖姛鑳藉拰琚敤鏉ュ紑鍙戣蔣浠剁殑瀛愮▼搴忕殑闆嗗悎銆備粬浠尯鍒簬鎵ц錛屽湪鍏朵腑浠栦滑涓嶆槸鐙珛鐨勭▼搴忥紱鏇撮傚悎鐨勶紝浠栦滑鏄滃姪鎵嬧濅唬鐮侊紝鐢ㄦ潵鏀寔閫氳繃閫氱敤
鎴愪負(fù)涓浣撶殑鍔熻兘瀹規(guī)槗鏉ヨ繘琛岀鐞嗐傚湪璇誨畬榪欎簺錛屼綘鍙兘璇達(dá)紝鈥滃棬錛乨ojo鏄竴涓簱鐨勯泦鍚堚濓紝浣犲彲鑳芥槸姝g‘鐨勶紝鐒惰岋紝dojo涓嶄粎浠呮槸鍋氫負(fù)涓涓簱鐨勯泦鍚堛?br />Toolkit錛堝伐鍏鳳級
聽聽聽
Dojo鏄竴涓嬌鐢↗avascript緙栧啓鐨勫紑婧怐HTML宸ュ叿銆傚畠寤虹珛浜庡緢澶氬凡鎹愮尞鐨勪唬鐮佸熀紜錛坣Widgets銆丅urstlib銆乫(m)錛?榪?
鏄負(fù)浠涔堟垜浠皥鍒板畠鏈夋椂鏄竴涓?緇熶竴"鐨勫伐鍏楓侱ojo鑷村姏浜庤В鍐充竴浜涢暱鏈熷瓨鍦ㄧ殑浼撮殢DHTML鐨勫巻鍙查棶棰橈紝瓚熼樆姝㈠ぇ澶氭暟閲囩敤鍔ㄦ亀eb搴旂敤紼嬪簭寮鍙戙?br />
Dojo allows you to easily build dynamic capabilities into web pages and any other environment that supports JavaScript
sanely. You can use the components that Dojo provides to make your web
sites more useable, responsive, and functional聽 With Dojo you can build
degradeable user interfaces more easily, prototype interactive widgets
quickly, and animate transitions.聽 You can use the lower-level APIs and
compatibility layers from Dojo to write portable JavaScript
and simplify complex scripts. Dojo's event system, I/O APIs, and
generic language enhancement form the basis of a powerful programming
environment. You can use the Dojo build tools to write command-line
unit-tests for your JavaScript
code.聽 You can use the Dojo package system to make your code easier to
maintain and less platform dependent.聽 The Dojo build process helps you
optimize your JavaScript for deployment by grouping sets of files together and reuse those groups through "profiles".
Dojo鍏佽浣犲鏄撳湴寤虹珛鍔ㄦ佹ц兘鍒皐eb欏甸潰鍜屽叾浠栫ǔ鍋ユ敮鎸乯avascript鍦扮幆澧冧腑銆?浣犲彲浠ヤ嬌鐢―ojo鏀寔緇勪歡錛屽畠浣夸綘鐨剋eb绔欑偣鏇存湁鍙敤琛岀殑銆備即闅廳ojo,浣犲彲浠ユ洿瀹規(guī)槗鍦板緩绔媎egradeable鐢ㄦ埛鐣岄潰錛岃繀閫熷湴鍘熷瀷浜や簰楗頒歡鍜屽姩鐢昏漿鍙樸備綘鍙互浣跨敤搴曞眰鍦癮pi鍜屽吋瀹瑰眰錛屼粠Dojo鍒板啓杞諱究鐨凧avascript鍜岀畝鍗曞鏉傛暀鏈侱ojo鐨勪簨浠剁郴緇燂紝I/O api,鍜屾硾鍨嬭璦澧炲己緇勬垚寮哄ぇ鐨勭▼搴忕幆澧冪殑鍩虹銆?浣犲彲浠ヤ嬌鐢╠ojo寤洪犲伐鍏鋒潵涓轟綘鐨凧avascript浠g爜鍐欏懡浠よ鍗曞厓嫻嬭瘯銆備綘鍙互浣跨敤Dojo 鍖呯郴緇熸潵浣垮緱浣犵殑浠g爜鏇村鏄撳幓緇存姢鍜屾洿灝戝鉤鍙頒緷璧栥?Dojo寤洪犺繃紼嬪府鍔╀綘浼樺寲浣犵殑Javascript涓轟綘鐨勯儴緗詫紝瀹冮氳繃涓璧峰垎緇勬枃浠墮泦鍚堝拰閫氳繃"profile"閲嶇敤榪欎簺緇勩?
Dojo
does all of these things by layering capabilities onto a very small
core which provides the package system and little else. When you write
scripts with Dojo, you can include as little or as much of the
available APIs as you need to suit your needs. Dojo provides MultiplePointsOfEntry, InterpreterIndependence, ForwardLookingAPIs, and focuses on ReducingBarriersToAdoption.
Dojo澶勭悊鎵鏈夎繖浜涗簨鎯咃紝閫氳繃鍒嗗眰鑳藉姏鍒嗚В鎴愪竴浜涢潪甯稿皬鐨勫拰鏍稿績錛屽畠鏀寔鍖呯郴緇熷拰鍏朵粬鏇村皯鐨勩傚崟浣犲啓鏄嬌鐢―ojo鍐欒剼鏈紝浣犲彲浠ュ寘鍚綔涓哄緢灝戞垨
鑰呭悓鏍峰鍙敤鐨刟pi浣滀負(fù)浣犻渶瑕佹弧瓚充綘鐨勯渶瑕佺殑銆侱ojo鏀寔MutiplePointsOfEntry,
InterprerterIndependence, ForwardLookingAPIs,
鍜岄泦涓璕educingBarriersToAdoption.
Dojo
is being built around a single markup language that will provide
application authors with a (more) simple way of declaring and using
responsive DHTML interface components. Renderings may be made available
in several rendering contexts (such as SVG, or perhaps even the desktop
or Flash), but the markup language (DojoML) and scripting language (JavaScript) will not change. Better yet, the DojoML parser accepts extended HTML and SVG as valid input, and can be used to easily create DegradeableResponsiveApplications.
Dojo鏄竴涓寤洪犲湪涓涓崟涓鐨勬爣璁拌璦錛屽畠?yōu)畣鏀寔搴旂敤绋嬪簭浣滆呴噰鐢ㄤ竴涓紙澶氫釜錛夌畝鍗曟柟娉曟潵澹版槑鍜屼嬌鐢ㄤ綔鍑哄搷搴旂殑DHTML鐣岄潰緇勪歡銆傝〃鐜板湪璁稿琛ㄧ幇涓婁笅鏂?渚?
濡傦細(xì)SVG錛屾垨鐢氳嚦鍙兘妗岄潰鎴杅lash)鍙兘琚悎娉曞寲,浣嗘槸鏍囪璇█(DojoML)鍜岃剼鏈璦錛圝avascript錛夊皢涓嶆敼鍙樸傜劧鑰屾洿濂界殑錛?
DojoML
瑙f瀽鍣ㄦ帴鍙楀凡鎵╁睍鐨凥TML鍜孲VG浣滀負(fù)涓涓悎娉曠殑杈撳叆錛屽茍涓斿彲浠ヨ瀹規(guī)槗鐨勫垱寤篋egradeableResponsiveApplications.
Web Standards are a three-legged stool, or without metaphors(鏆楀柣), a threesome錛堜笁浜轟竴緇勶級 of technologies that should live together in harmony錛堝拰鐫︾浉澶勶級. (X)HTML adds structure and semantics to your content, CSS is responsible for 錛堜負(fù)...璐熻矗錛塱ts presentation, and the DOM provides an interface to add behavior. You keep your Web pages flexible (or: easier to understand, maintain, restyle<鏀瑰彉鏍峰紡> and update behavior) by separating all three layers; structure and content from presentation, structure and content from behavior and presentation from behavior. To accomplish this, try to avoid inline behavior and use unobtrusive techniques instead.
涓昏琛ㄨ堪浜嗕笁縐嶆妧鏈殑緇煎悎浣跨敤錛宧tml涓虹粏鑺傜粨鏋勫拰璇硶 css涓鴻〃鐜拌礋璐?nbsp; dom鏀寔澧炲姞浜嬩歡琛屼負(fù)鐨勬帴鍙c?/P>
When you attach behavior on page load, you may have to work around some known issues. First, you may encounter differences in cross-browser event handling. Second, make sure you don't overwrite existing onload handlers. Last, but not least, you may encounter a delay in the attachment of your behavior. The underlying 錛堟綔鍦ㄧ殑錛塸roblem is that a window onload event will only execute after the whole document, including all page assets like images and objects, is loaded. If your script needs to respond immediately after an element has loaded or if you work with a lot of content, a series of images or a slow server, you may be forced to look for a solution. You can either hide your content until the behavior is attached or attach the behavior via a script call after your elements are loaded, like at the end of the body element.
浠嬬粛鍦ㄩ〉闈㈠紑鍙戜腑涓昏閬囧埌鐨勯棶棰橈紒
Choose your markup wisely, so you can take full advantage of the power of the DOM.<鍏呭垎鍒╃敤dom鐨勫姏閲?gt; For example, when you use nested lists to build a navigation bar, you can use the structure of the DOM tree instead of a replicated(澶嶅埗) structure in arrays.<list dom tree> Scripting should be avoided in some cases where you can use markup or CSS to create behavior. This may sound a bit contradictory(鍙嶅), however built-in behavior enabled by (X)HTML attributes (e.g. disabling a form field or setting a max length on an input field) and CSS pseudo-classes (e.g. when building rollovers or drop downs) are regarded to be wider supported and easier to implement than using JavaScript. ToughQuiz on Quirksmode illustrates the discussion and the fine line between the uses of CSS generated content and regular markup and behavior.
In those cases where CSS currently lacks<緙轟箯> cross-browser support or is missing features for adding presentation, DOM based scripting can supplement<琛ュ厖> CSS. Presentational scripting will probably be replaced in a few years, when browsers have better CSS2.1 compliance<欏轟粠> and with the support of CSS3. Please realize that, because of the interrelationship<鐩鎬簰褰卞搷> of the different Web Standards and because both Web Standards and Web clients are constantly evolving<榪涘寲>, some good practices for using the DOM and JavaScript will change over time<闅忕潃鏃墮棿鐨勮繃鍘?gt;.
JavaScript is accessible when a page's navigation, content and main functionality (e.g. submitting a form) is available to your whole target audience, independent of their Web client or input device. This includes:
The most common way to create accessible JavaScript is to use unobtrusive techniques that are mouse independent and enhance your already accessible markup with behavior. Progressive enhancement and its predecessor graceful degradation are good strategies to create Web pages that are accessible to the most common denominator, while providing a better user experience for a smaller group of people with more advanced devices or Web clients. No matter what strategy you use, make sure that you always design for multiple scenarios.
The usability of a Web page is often determined by a good information architecture, clear and intuitive(鐩磋鐨? visual design and well designed functionality. One of the main arguments to enhance your markup using unobtrusive JavaScript is to improve the usability of a Web page by supporting these usability attributes. If you add JavaScript and don't enhance the usability of a Web page, you should rethink if you should apply it at all.
Unobtrusive scripting bridges the gap between 'designers' and 'coders'. There is a big group of people in today's industry that does know how to write (X)HTML and CSS but doesn't feel very comfortable with the DOM and JavaScript. Unobtrusive behavior introduced a mechanism to easily apply small portable scripts to a Web page: "Just make sure your markup looks like A, include this script B, and as a result you have a page that can do C".
Try to create small pieces of independent code. The disadvantages of a lot of existing JavaScript code libraries are that you often have to include a lot more code than you really need and that they are harder to understand and maintain if you didn't create them yourself. Because the functions in these libraries are often grouped and reused by other scripts, it often feels like they are spaghetti-coded. A library with small unobtrusive scripts has the advantage of being light-weight, easy to be understood and easy to be adjusted for more specific implementations.
Create reusable code. If you find yourself duplicating code snippets(鐗囨), create functions. If you find yourself duplicating(澶嶅埗) similar code snippets, try to abstract your code to the level that you can reuse it for multiple purposes.
Document your code well. If you work together with other people, like to share your code with others, or still want to know why you did certain things one year from now, good documentation is key.
Avoid browser detection, because it is almost impossible to maintain in the future. Feature testing or object detection offers a browser independent and future-proof technique to test to what extent your Web client supports JavaScript.
XHTML (if not used in backwards-compatible mode) introduces media type application/xhtml+xml (currently unsupported by Internet Explorer), which has a huge impact on how we write JavaScript code:
If you want to keep yourself up-to-date with the latest developments, there are a lot of initiatives from different organisations that will impact the ways we use JavaScript and the DOM in the near future:
Although JavaScript is generally well supported by most modern Web clients, support still remains its biggest weakness. Because from the first days of the Web users were often harassed(鐤插︾殑) by all kinds of annoying(璁ㄥ帉鐨? behavior, browser makers decided to make it easy to switch JavaScript off (Windows XP Service Pack 2 even disables some JavaScript by default, because it regards it as active scripting). If you compare JavaScript with its little stepbrother ActionScript (which is supported when the Flash plug-in is installed and cannot be switched off), you will find that the main difference is that you can rely on its behavior to accomplish certain tasks. Because it is just too easy to switch JavaScript off, simple tasks like form validation always need to be duplicated at the server side. It will be for this reason that in the future most client-side form validation will be replaced by markup and 'built-in behavior'.
As mentioned earlier, the onload event handler is insufficient to get the best out of unobtrusive techniques. I hope that the people of the W3C will respond to this feedback from the JavaScript community and add new handlers like onElementLoad and onDOMLoad to future DOM specifications.
The JavaScript implementations of Internet Explorer and Safari suffer from memory leaks when using circular references like closures. When using circular references, make sure you remove event handlers when a page is unloaded.
JavaScript is a very flexible language and as a result you often have multiple ways of doing things. You could choose for either a procedural(紼嬪簭涓? or an object oriented way of coding. For your unobtrusive behavior you can either use custom attributes or use class attributes as triggers to fully control the behavior of your site. Flexibility implies that you have to make certain choices, however often one solution is not necessarily better or worse than another. Base your decisions on the context in which you have to use your scripts and your own philosophy or taste and try to use a consistent coding style.
Currently a lot of outdated(榪囨湡鐨? and badly written code is available on the Internet. Many scripts are plagued by browser detection, are using proprietary features that don't work cross-browser, are inaccessible or are not separating behavior from structure, because they rely on inline event handlers and scripts. It seems that Web Standards, Web clients and the practice of writing good JavaScript have evolved so quickly in the past two years, that it is hard to keep up with the latest good practices. This on its turn makes it hard to reuse code from others or reuse code you wrote yourself a year ago. Because some parts of older scripts may still contain valid code constructs, it is best to review them and rewrite the parts that don't suffice anymore. You will probably often find that a complete rewrite will do best.
So how do less experienced DOM and JavaScript users tell the difference between good and bad code on the Internet? There are some experts on the Internet that advocate modern ways of scripting and there are communities that discuss and rate new scripts and techniques. Some examples are:
DHTML Utopia: Modern Web Design Using JavaScript & DOM is the first title of a new range of books focusing on the application of modern JavaScript and unobtrusive scripting techniques.
Optimize your scripts for both download speed and execution speed. <涓嬭澆閫熷害鍜屾墽琛岄熷害>Some tips:
A selection of tools that make life much easier:
漏 Copyright 2004-2006 Bobby van der Sluis All rights reserved.
eg.
this.isIE = navigator.userAgent.toLowerCase().indexOf("msie") >= 0;
This provides a new method to the core class Function. The method,called bindAsEventListener(),is used to do bind methods to event handlers.
榪欎負(fù)鏍稿績class 鍑芥暟鏀寔浜嗕竴涓柊鐨勬柟娉曘傝繖涓柟娉曞彨bindAsEventListener(),鏄敤鏉ョ粦瀹氭柟娉曠粰浜嬩歡澶勭悊鍣ㄧ殑銆?BR>var obj = new SomeClass();
var closure = obj.someMethod.bindAsEventListener(this);
element.onclick = closure;