最大的網站源碼資源下載站,
atlas架構提供了一種比asp.net中數據綁定(data binding)強大得多的客戶端綁定模型。這種模型異常靈活,甚至有些類似wpf(windows presentation foundation)中的綁定模型。atlas提供的綁定模型允許您將某對象的任意一個屬性綁定到另外一個對象的任意一個屬性上。它不單單可以應用于數據綁定,甚至可以將某個控件的樣式綁定到另外一個控件上。這樣使得在atlas中將一切關聯起來變成可能。
在本文中,我將嘗試分析一些atlas實現代碼來解釋atlas是如何完成binding的。
首先讓我們察看一小段應用atlas binding的代碼。這里將一個textbox的text屬性和一個select list的selectedvalue屬性綁定起來。無論你改變其中的哪個,在另一個上面都會有立刻得到體現。
html和aspx,定義textbox和select list。(注意必須聲明一個scriptmanager服務器端對象,以引入atlas必須的javascript文件。)
<atlas:scriptmanagerid="scriptmanager1"runat="server"/>
<div>
inputanintegerfrom1to5.<br/>
<inputid="mytextbox"type="text"/><br/>
selectanitem.<br/>
<selectid="myselect">
<optionvalue="1">value1</option>
<optionvalue="2">value2</option>
<optionvalue="3">value3</option>
<optionvalue="4">value4</option>
<optionvalue="5">value5</option>
</select>
</div>
atlas腳本,將上面兩個html控件“升級”成atlas控件。
<pagexmlns:script="http://schemas.microsoft.com/xml-script/2005">
<references>
</references>
<components>
<textboxid="mytextbox">
<bindings>
<bindingdatacontext="myselect"datapath="selectedvalue"property="text"direction="inout"/>
</bindings>
</textbox>
<selectid="myselect"/>
</components>
</page>
如上所示,我們只需要書寫一小段簡單的代碼即可實現需要的綁定功能。
atlas是如何實現這些的呢?首先,atlas需要有一種途徑來監聽綁定控件的綁定屬性的變化(除非你不需要atlas提供的自動綁定功能)。在atlas.js中定義了一個名為sys.inotifypropertychanged的接口,類似.net中提供的一樣。對象可以實現這個接口以期讓別的對象監聽到自己的屬性值的變化。atlas中所有組件的基類,sys.component,實現了這個接口。sys.component同樣提供一個方法raisepropertychanged(propertyname),這個方法應該在每個屬性的setter中被調用以發出inotifypropertychanged.propertychanged事件。
目前為止,我們可以看一下atlas控件中textbox的具體實現。看看textbox中是如何在相應的html事件發出時同樣發出propertychanged事件的。
var_text;
var_changehandler;
this.get_text=function(){
returnthis.element.value;
}
this.set_text=function(value){
if(this.element.value!=value){
this.element.value=value;
this.raisepropertychanged('text');
}
}
this.initialize=function(){
sys.ui.textbox.callbasemethod(this,'initialize');
_text=this.element.value;
_changehandler=function.createdelegate(this,this._onchanged);
this.element.attachevent('onchange',_changehandler);
_keypresshandler=function.createdelegate(this,this._onkeypress);
this.element.attachevent('onkeypress',_keypresshandler);
}
this._onchanged=function(){
if(this.element.value!=_text){
_text=this.element.value;
this.raisepropertychanged('text');
}
}
可以看到,當text屬性改變時,atlas發出了propertychanged事件,這就使綁定到這個屬性成為可能。
而后atlas的綁定模型捕獲到了這個事件,再根據binding聲明查找出與其相關的目的對象以及相應的屬性,并調用這個屬性的setter來實現目的對象屬性的變化。
如果源對象(source object)實現了inotifypropertychanged接口,并且改變的屬性就是datapath 中指定的屬性,同時direction 設定為in或者inout,atlas綁定將通過分析引入(incoming)的binding來處理propertychanged事件(參考下面將要介紹的evaluatein()方法)。
類似的,如果目的對象(target object)實現了inotifypropertychanged接口,并且改變的屬性就是property中指定的屬性,同時direction 設定為out或者inout,atlas綁定將通過分析流出(outgoing)的binding來處理propertychanged事件(參考下面將要介紹的evaluateout()方法)。
接下來讓我們察看binding實現代碼中的的公有方法和屬性來分析一下atlas綁定的核心實現。在這里沒有必要列出涉及綁定的全部代碼,如果您感興趣,可以用關鍵詞sys.bindingbase和sys.binding 在atlas.js文件中進行搜索。首先是sys.bindingbase提供的方法和屬性。
sys.binding(也在atlas.js中)中也有一些重要的方法/屬性:
新聞熱點
疑難解答