我需要從聚合物(v1.2.4)中的<template is="dom-repeat">
生成的實例中捕獲數據,我不確定最安全的方法是什麼所以考慮到無數的Shadow DOM可用(客戶端瀏覽器可能是polyfilled等)。訪問聚合物中的數據的可靠方法(在dom-repeat/event handler中)
一個簡單的例子:
<template is="dom-repeat" items="[[myItems]]" id="collection"> <paper-card on-tap="handleTap"> (...)
什麼是從事件處理程序訪問模型數據最可靠的方法是什麼?
handleTap: function(e) { var data = e.model.get('item.myData'); }
2.
handleTap: function(e) { var data = this.$.collection .modelForElement(Polymer.dom(e).localTarget) .get('item.myData'); }
我擔心的是最簡單的(#1)選項可能會按預期工作中我環境,但可以得到其他瀏覽器中的越野車。
即使在選項#2中,我不確定是否真的有必要在將事件目標傳遞到modelForElement
之前對事件目標進行標準化(如official Polymer guide on events中的建議)。
關於#1和瀏覽器行爲,如何設置單元測試並在[Sauce Labs]中運行它們(https://www.polymer-project.org/1.0/tools/tests.html#automated-testing-in -雲端)? –