锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
Understanding Your Own Footprint
鐞嗚В浣犺嚜宸辯殑瓚寵抗
Every useful system carries the imprint of its designers. From bridges to spoons, the expression of a builder's skill, philosophies, and production constraints are exposed in the final product. Software is no different, except in its propensity to change. Software mutates in response to users needs, and in that change, a continual re-impriting of a designer's skill and sense of taste takes place.
姣忎竴涓湁鐢ㄧ殑緋葷粺鐣欎笅瀹冪殑璁捐甯堢殑鐑欏嵃銆備粠妗ユ鍒板嫼瀛愶紝涓涓緩閫犺呮妧宸х殑琛ㄨ揪錛屽摬瀛﹀錛屽拰浜у搧綰︽潫鍦ㄦ渶緇堢殑浜у搧涓鏆撮湶銆傝蔣浠舵槸涓嶅悓鐨勶紝闄や簡瀹冪殑鍙樺寲鐨勫懼悜銆傚湪鐢ㄦ埛闇瑕佺殑鍙嶅簲涓殑杞歡鍙樺紓錛屽拰鍦ㄩ偅縐嶆敼鍙樹腑錛屼竴涓繛緇殑閲嶅鐨勪笉鍙兘鐨勪竴涓璁″笀鐨勬妧宸у拰鍛寵浜х敓銆?br />This has the potential to cause continuity problems for others, be they end users or other developers. Reducing these confusing aspects (cognitive load) allows software consumers of every type to feel better about a block of code. With every interaction, their pre-conceived notions of how the thing will behave serve them instead of forming an obstacle for them to overcome. Their instincts are turned into a valuable tool by elegantly and consistently designed software.
榪欐槸涓哄叾浠栦駭鐢熸寔緇棶棰樼殑鏈川錛屾槸浠栦滑鏈緇堢敤鎴鋒垨鍏朵粬寮鍙戣呫傚噺灝戣繖浜涙販涔辯殑鏂歸潰錛堣鐭ョ殑璐熻嵎錛夊厑璁告瘡涓縐嶇被鍨嬬殑杞歡娑堣垂鑰呮劅瑙夋洿濂藉叧浜庝竴浜涗唬鐮併備即闅忔瘡涓嬈′氦浜掞紝浠栦滑鐨勯璁炬兂鐨勬蹇墊槸榪欎簺涓滆タ濡備綍榪愯漿鏈嶅姟浠栦滑鑰屼笉鏄粍鎴愪竴涓負浠栦滑璺ㄨ秺鐨勯殰紕嶃備粬浠殑鏈兘鏄氳繃浼樼編鍜屼竴璐湴璁捐鐨勮蔣浠跺彉鎴愪竴涓湁浠峰肩殑宸ュ叿銆?br />Dojo, as a project, should meet expectations in this way. This doesn't mean that you have to agree with every design decision that's been made in the project (dissent is healthy) or that the guiding principles outlined here are written in stone. They should, however, capture the way design choices have been made to date and serve as a guide for making future decisions.
聽聽聽聽 Dojo錛屼綔涓轟竴涓」鐩紝搴旇婊¤凍榪欐牱鐨勬湡鏈涖傝繖涓嶆剰鍛寵繖浣犱笉寰椾笉鍚屾剰姣忎竴涓璁″喅瀹氾紝鍗沖畠宸茬粡鍚戦」鐩腑榪涘叆錛堜笉鍚屾剰鏄仴搴風殑錛夋垨鑰呴偅鍐欏叆鐭沖ご涓殑瀵煎悜鍘熺悊杞粨銆備粬浠簲璇ユ槸錛岀劧鑰岋紝鎹曟崏璁捐閫夋嫨鐨勬柟娉曞凡緇忔彁鍒版棩紼嬪拰浣滀負鏈潵鍐沖畾鐨勫悜瀵箋?br />
Dojo Guiding Principles
Dojo鐨勮璁″鍚戝師鍒?/strong>
Reduce barriers to adoption.
閲囩敤鍑忓皯闅滅鐗┿?br />Simply put, do not give users reasons not to choose your code. This affects everything from design to licensing to packaging.
綆鍗曟斁緗紝浣嗘槸涓嶇粰鐢ㄦ埛涓嶄嬌鐢ㄤ綘浠g爜鐨勫師鍥犮傝繖褰卞搷浠庤璁″埌涓撳埄鐨勫寘瑁呯殑姣忎歡浜嬫儏銆?br />Simple first, fast later
綆鍗曚紭鍏堬紝蹇湪鍚?br />Make it simple to use first, make it fast when it's approprite. Simple here means simple for users, not for us. We should work as hard as necessary to make things simple for end users. This principle goes to reducing barriers to adoption. Things should be as easy as possible for the common case but transparently "upgradeable" to a faster code path if the end user is willing to learn about the performance dials and knobs.
棣栬鐨勭畝鍗曚嬌鐢紝褰撳畠鏄傚悎鐨勫皢瀹冩墦涓婄粨銆?綆鍗曡繖閲屾剰鍛寵繖瀵圭敤鎴風畝鍗曪紝涓嶆槸瀵規垜浠傛垜浠簲璇ヤ嬌宸ヤ綔瀵規渶緇堢敤鎴峰敖鍙兘鐨勫彉寰楃畝鍗曘傝繖浜涘師鍒欏畾浣嶅湪閲囩敤鍑忓皯闅滅鐨勫師鍒欍備簨鎯呭簲璇ュ敖鍙兘瀹規槗瀵瑰叡鍚岀殑鎯呭艦浣嗘槸鏄庢樉鈥滃彲鍗囩駭鐨勨濆涓涓揩閫熺殑浠g爜璺緞 濡傛灉緇堢鐢ㄦ埛鎯寵浜嗚В鎵ц鐨勮〃鐩樺拰鏃嬮挳銆?br />Bend to the constraints of your environment
娣峰悎浣犵幆澧冪殑綰︽潫
Do not bludgeon a problem to death with code. If the environment can do most of something, let it. Fill in as necessary, but do not re-invent. Make the path smooth for users, but do not introduce your own idioms where they aren't required.
涓嶈媯掑嚮涓涓棶棰樻潵浼撮殢浠g爜姝諱骸銆傚鏋滆繖涓幆澧冨彲浠ュ仛澶ч儴鍒嗘槸浜嬫儏錛岃瀹冨仛銆傚敖鍙兘鏇夸唬錛屼笉瑕侀噸鏂板彂鏄庛傚鐢ㄦ埛鏉ヨ灝嗚礬寰勫鉤婊戯紝褰撲粬浠笉闇瑕佺殑鍦版柟涓嶈浠嬬粛浣犺嚜宸辯殑鏂硅█銆?br />
Improving From Here
浠庤繖閲屾敼榪?/strong>
Dojo may not yet completely embody the principles outlined here, but it is their purpose to serve as a guide for the project when making determinations how things should change.
Dojo鍙兘娌℃湁瀹屽叏鍖呭惈榪欓噷杞粨鐨勫師鍒欙紝浣嗗畠鏄綋鎻忚堪榪欎簺浜嬫儏搴旇濡備綍鏀瑰彉,浠栦滑浣滀負涓涓悜瀵煎欏圭洰鏉ユ湇鍔$殑鐩殑銆?br />If you think that Dojo has not yet met one or more of these goals in a particular way, please raise the issue on one of the project mailing lists or file a bug.
濡傛灉浣犺涓篋ojo榪樻病鏈夋弧瓚沖湪鐗瑰畾鏂規硶涓殑涓涓垨澶氫釜榪欎簺鐩爣錛岃鎻愬嚭涓涓猧ssue鍦ㄤ竴涓」鐩偖浠跺垪琛ㄦ垨鏂囦歡浣滀負涓涓猙ug銆?br />
HTML forms.
Second, the requests themselves are asynchronous,meaning that the
contextual links, zoom control , and the other page features remain
accessible while the map is gathering new data.
The four main components of Ajax:Javascript defines business rules and
program flow. The Document Object Model and Cascading Style Sheets allow
the application to reorgnize its appearance in response to data feteched in
the background from the server by the XMLHttpRequest object or its close
cousins.
We've hightlighted a few more here , to demonstrate the breadth of concerns
to which CSS can be applied:
(1)on-screen placement
(2)texturing elements
(3)assisting in layout of elements
(4)placing text relative to accompanying graphics
The DOM presents an HTML document as a tree structure , with each element
representing a tag in the HTML markup.
Working with the DOM using Javascript
An Example:
window.onload=function(){
var hello=document.getElementById('hello');
hello.className='declared';
var empty = document.getElementById('empty');
addNode(empty,"reader of");
addNode(empty,"Ajax in action");
var children = empty.childNodes;
for (var i=0;i<children.length;i++){
children[i].className='programmed';
}
empty.style.border='solid green 2px';
empty.style.width='200px';
}
function addNode(el,text){
var childEl = document.createElement('div'); --create new element
el.appendChild(childEl);
var txtNode=document.createTextNode(txt); --create text element
childEl.appendChild(txtNode);
}
A third method worth mentioning allows us to make a shortcut through
documets that we haven't tagged with unique IDs. DOM nodes can also be
searched for based on their HTML tag type,usinf getElementByTagName(). For
example , document.getElementByTagName('UL') will return an array of all
<UL> tags in the document.
FINDING A DOM NODE
CREATING A DOM NODE
Adding styles to your document:
hello.className='declared';
empty.style.border="solid green 2px";
innerHTML
refactoring 閲嶆瀯
Working with DOM elements
A web page is exposed to Javascript through the Document Object Model
(DOM),a tree-like structure whose elements correspond to the tags of an
HTML document. When manipulating a DOM tree progarmmatically ,it is quite
common to want to find out an element's position on the page.
Unfortunately,browser vendors have provided various nonstandard methods for
doing so over the years,making it diffcult to write fail-safe cross-browser
code to accommplish the task.
window.onloadListeners = new Array();
window.addOnLoadListener(listener){
window.onloadListener[window.onloadListeners.length]=listener;
}
window.onload=function(){
for(var i=0;i<window.onloadListeners.length;i++){
var func = window.onloadListeners[i];
}
}
//------------------------------------------
Reusing user action handlers:鍛戒護妯″紡
function buttonOnClickHandler(event){
var data = new Array();
data[0]=6;
data[1]=data[0]/3;
data[2]=data[0]*data[1]+7;
var newRow = createTableRow(dataTable);
for (var i=0;i<data.length;i++){
createTableCell(newRow,data[i]);
}
}
buttonDiv.onclick=buttonOnClickHandler;
//------------------------------------
Keeping only one reference to a resource:Singleton pattern
function TradingMode(){
this.mode=MODE_RED;
}
TradingMode.prototype.setMode=function(){
}
TradingMode.instance = new TradingMode();
var TradingMode={
mode:MODE_RED,
setMode:function(){
...
}
};
鍩轟簬妯℃澘鐨勭郴緇燂細
Prototype:
Prototype鏄竴涓負Javascript緙栫▼鎻愪緵澶氱敤閫旂殑鍔╂墜綾誨簱錛屼嬌鐢ㄤ竴涓鍚戞墿灞?/P>
Javascript璇█鑷繁鏀寔涓涓狾O緙栫▼鏂瑰紡銆侾rototype鏈変竴涓湁鐗硅壊鐨凧avascript緙栫爜
鏍峰紡錛屽熀浜庤繖浜涘凡緇忓鍔犵殑璇█鐗規с傝櫧鐒禤rototype緙栫爜鑷韓寰堥毦闃呰錛屼粠Java/C#/
鏍峰紡涓縐婚櫎瀛樺湪寰堜箙浜嗭紝浣跨敤Prototype錛屽拰鍐呭緩鍦ㄥ畠涓婄殑錛屾槸鐩存帴鐨勩侾rototype
鍙互鑰冭檻涓虹被寮鍙戣呮彁渚涚被銆侫Jax搴旂敤紼嬪簭浣滆呮洿澶氬笇鏈涗嬌鐢ㄧ被寤虹珛綾昏屼笉鏄嬌鐢?/P>
Prototype鑷韓銆傛垜浠皢鏌ヨ榪欎簺綾誨湪涓嬮潰鐨勯儴鍒嗕腑銆傚湪鏈熼棿錛屼竴涓富瑕佺殑鍏充簬
Prototype鏍稿績鐨勭壒鎬ц璁哄皢甯姪浠嬬粛瀹冭嚜韜殑緙栫爜鐨勬牱寮忓拰灝嗗湪鎴戜滑璁ㄨ
Scriptaculous銆丷ico鍜孯ubt on Rail.
銆銆Prototype鍏佽涓涓璞℃墿灞曢氳繃澶嶅埗鎵鏈夌殑鐖跺璞$殑灞炴у拰鏂規硶緇欏瓙鍏朵粬銆傝繖涓?/P>
鐗規ф槸鏈濂界殑涓句釜渚嬪瓙,璁╂垜浠湅涓涓嬪畾涔夌殑Vehicle鐖剁被
function Vehicle(numWheels,maxSpeed){
this.numWheels = numWheels;
this.maxSpeed = maxSpeed;
}
瀵規鎴戜滑鎯寵瀹氫箟涓涓簿紜殑瀹炰緥鏉ヨ〃鐜頒竴涓箻瀹㈠垪杞︺傚湪鎴戜滑鐨勫瓙綾諱腑鎴戜滑涔熸兂琛?/P>
鐜板杞︾殑鏁伴噺騫舵敮鎸佸鍔犳垨鍑忓皯鐨勬満鍒躲傚湪甯哥敤鐨凧avascript涓紝鎴戜滑鍙兘榪欐牱鍐欙細
var passTrain = new Vehicle(24,100);
passTrain.carriageCount = 12;
passTrain.addCarriage = function(){
this.carriageCount++;
}
passTrain.removeCarriage=function(){
this.carriageCount--;
}
榪欎負鎴戜滑鐨凱assTrain瀵硅薄鎻愪緵闇瑕佺殑鍔熻兘鎬с備粠璁捐鐨勮鍥劇殑鏌ョ湅榪欎簺浠g爜錛岃櫧鐒跺畠
鏈夌偣鎺╅グ浜嗘墿灞曟墿灞曟у姛鑳芥у埌涓涓繛璐殑鍗曞厓銆侾rototyp鍙互鍦ㄨ繖閲屽府鍔╂垜浠紝閫?/P>
榪囧厑璁告垜浠畾涔夋墿灞曡涓轟綔涓轟竴涓璞″茍涓旀帴鐫浣跨敤瀹冩墿灞曞熀紜瀵硅薄銆傞鍏堬紝鎴戜滑浣?/P>
涓轟竴涓璞″畾涔夋墿灞曠殑鍔熻兘鎬э細
function CarriagePuller(carriageCount){
this.carriageCount = carriageCount;
this.addCarriage=function(){
this.carriageCount++;
}
this.removeCarriage=function(){
this.carriageCount--;
}
}
鎺ョ潃鎴戜滑鍚堝茍榪欎袱涓潵鏀寔涓涓璞″寘鍚墍鏈夌殑闇瑕佺殑琛屼負錛?BR>var parent = new Vehicle(24,100);
var extension = new CarriagePuller(12);
var passTrain = Object.extend(parent,extension);
娉ㄦ剰鎴戜滑鍒嗗埆鍦ㄥ紑濮嬪悗鏉ュ畾涔夌埗鍜屾墿灞曞璞★紝鎺ョ潃灝嗕粬浠繘琛屾販鍚堛傝繖鐖跺瓙鍏崇郴瀛樺湪
榪欎簺渚嬩腑錛屼笉鍦╒ehicle鍜孋arriagePuller綾諱腑銆傚綋瀹冧笉鏄紜殑緇忓吀鐨勯潰鍚戝璞★紝瀹?/P>
鍏佽鎴戜滑淇濇寔鎴戜滑浠g爜涓庣郴緇熷姛鑳借繘琛屽叧鑱旓紝鍦ㄨ繖涓媺杞︽寜渚嬩腑錛屽湪涓涓湴鏂癸紝閫氳繃
鍦ㄦ垜浠洿瀹規槗榪涜澶嶇敤銆傛垜浠仛榪欎釜渚嬪瓙鐪嬭搗鏉ヤ技涔庢病鏈変粈涔堢敤澶勶紝鍦ㄥぇ鐨勯」鐩腑錛?/P>
鐢ㄨ繖縐嶆柟娉曞皝瑁呭姛鑳芥ф槸闈炲父鏈夌敤鐨勩?/P>
Prototype涔熶互AJax瀵硅薄鐨勬柟寮忔彁渚涘Ajax鏀寔錛岃繖鍙互瑙e喅瓚呭鉤鍙癤MLHttpRequest瀵?/P>
璞°侫jax鏄Ajax.Request綾誨瀷鎵╁睍錛屽畠鍙互浣跨敤XMLHttpRequest鍚戞湇鍔″櫒鍙戦佽姹?/P>
錛屼緥濡傦細 var req = new Ajax.Request('myData.xml');
榪欎釜鏋勯犲瓙浣跨敤涓涓垜浠篃灝嗚鐪嬪埌鐨勫湪寰堝Prototype-based綾誨簱涓殑鏍峰紡銆傚畠浣跨敤
緇撳悎鐨勬暟緇勬潵浣滀負涓涓彲閫夌殑鍙傛暟錛屽厑璁鎬竴涓鑼冨洿鐨勬寜鐓ч渶瑕佽繘琛岄厤緗?/P>
Ajax.Updater
The View in an Ajax application
Keepling the logic out of the View 灝哣iew鍒嗙鍑簂ogic
闂存帴浣跨敤CSS澧炲姞浜嬩歡
緇戝畾浜嬩歡鎺у埗浠g爜
The Rico framework has a concept of Behavior objects that target specific
sections of a DOM tree and add interactivity to them.
Keeping the view out of logic