2012-06-20 122 views
0

我需要將數據加載到我的樹存儲中。我的ajax請求給我沒有葉和文本屬性的XML數據。我如何映射它們?將xml加載到extjs4樹存儲中

<items> 
    <item>mytext</item> 
    <item>mytext2</item> 
    ... 
</items> 

我知道葉屬性,但文本我在我的模型試圖映射:

fields: [ 
    { name: 'leaf' , type: 'boolean' , defaultValue: true } , 
    { name: 'text', mapping: 'item'}, 
] 

,並在我的代理儲存讀者:

reader: { type: 'xml',root: 'items' , record: 'item' } 

我所有的節點,但無文字=/

請幫幫我!

-kyrillos

回答

0

我發現一個其他的解決辦法:

fields: [ 
    { name: 'leaf' , type: 'boolean' , defaultValue: true } , 
    { name: 'text', mapping: '/' } 
] 
+0

哇哦,我忘了你能做到這一點與DOM查詢。不錯和優雅,它應該是! – Reimius

1

查看源代碼,問題是,ExtJS的4需要一個模型有子元素,而你的XML在最頂層的模型數據。您將不得不重構返回到這樣的xml:

<items> 
    <item> 
     <text>mytext</text> 
    </item> 
    <item> 
     <text>mytext</text> 
    </item> 
    ... 
</items> 

並使用映射到那裏的文本節點。

另一個選擇是創建一個自定義映射函數,它可以與你的結構一起工作(Extjs不希望你這麼做,但如果你不能改變xml,這是唯一的方法):

fields: [ 
    { name: 'leaf' , type: 'boolean' , defaultValue: true } , 
    { name: 'text', mapping: function(node){ 
     if(node.firstChild && node.firstChild.nodeValue) 
      return node.firstChild.nodeValue; 
     return ""; 
    }}, 
] 
+0

我發現一個其他的解決辦法: –