2015-06-15 64 views
1

我無法理解數據綁定現在如何工作。聚合物1.0對象發生變化時的數據綁定

在我的索引頁我有一個對象(從RESTful服務得到JSON),當像應用到定製元件,它工作得很好:

<main-menu class="tiles-container ofvertical flex layout horizontal start" 
    menuitems="{{menuitems}}"> 
</main-menu> 

var maintemplate = document.querySelector('#fulltemplate'); 
maintemplate.menuitems = JSON.parse(data.GetDesktopResult); 

可正常工作,當我用不同的用戶加載我的頁面,更改主菜單以顯示每個用戶的桌面配置。 (這個menuitems對象反映每個用戶的每個桌面模塊的位置和大小)。

現在,用戶以前可以在旅途中改變自己的配置,而在Polymer 0.5上我沒有問題,只是改變了我的maintemplate.menuitems對象,就是這樣,它立即反映在模板上。

正如我遷移到聚合物1.0,我意識到在對象上的變化不會改變任何可見的,它比這複雜得多,但只是這樣做行不通:

<paper-icon-button id="iconback" icon="favorite" onClick="testing()"></paper-icon-button> 

function testing(){ 
    debugger; 
    maintemplate = document.querySelector('#fulltemplate'); 
    maintemplate.menuitems[0][0].ModuleSize = 'smamodule'; 
} 

對象的變化但屏幕上沒有任何反應,直到我將它保存到數據庫並重新加載頁面。

我是否缺少某些東西/當我更改作爲屬性傳遞的對象時,是否需要在Polymer 1.0上做其他操作來更新元素?

你問之前,我有設置好的爲notify: true這些屬性,這是我發現的不同精心的事情,但仍然無法讀取工作

謝謝!

編輯:

<template is="dom-repeat" items="{{menuitems}}" as="poscol"> 
    <div class="positioncolum horizontal layout wrap flex"> 
     <template is="dom-repeat" items="{{poscol}}" as="mitem" index-as="j"> 
      <main-menu-item class$="{{setitemclass(mitem)}}" 
       mitem="{{mitem}}" 
       index="{{mitem.TotalOrder}}" 
       on-click="itemclick" 
       id$="{{setitemid(index, j)}}"> 
      </main-menu-item> 
     </template> 
    </div> 
</template> 

main-menu-item正好被設置在此基礎上對象屬性而改變大小和顏色的div

回答

2

您需要使用:

這是代碼menuitems在使用如果要修改對象或數組內的元素,則會使用突變輔助函數,否則dom-repeat將不會收到有關更改的通知(請檢查docs):

function testing(){ 
    debugger; 
    maintemplate = document.querySelector('#fulltemplate'); 
    this.set('maintemplate.0.0.ModuleSize', 'smamodule'); 
} 
+0

剛剛嘗試過,該對象發生了預期的變化,但仍然沒有做其他任何事情...... – Iskalla

+0

難道這是計算類的問題嗎?當我閱讀文檔時,這應該起作用,但它永遠不會進入setitemclass(),這是我更改項目類的地方,並使其更改大小和外觀。 – Iskalla

+0

是'poscol'數組的基本數組(只有字符串?)。如果是這樣,你可能會遇到這個問題(https://github.com/Polymer/polymer/issues/1839)。 –