2015-05-23 63 views
0

在0.5,有可能增加一個JS-功能,如:什麼是聚合物0.9「過濾器」的最佳方式?

PolymerExpressions.prototype.timeofdate = function(input) { 
if(input) { 
    return input.substring(11,16) 
} 

(提取小時:分鐘從 「MongoDB的時間戳」 等2014-10-04T12:34:56 + 02:00)

並與管道變量使用,如:

{{starts | timeofdate}} 

當我試圖升級上面0.9的代碼,我不得不創建這個元素來代替:

<script> 
    Polymer({ 
    is: 'x-substr', 
    properties: { 
     start: Number, 
     end: Number, 
    }, 
    attached: function() { 
     this.innerHTML = this.innerHTML.substring(this.start, this.end); 
    } 
    }); 
</script> 

而且使用這樣的:

<x-substr start="11" end="16">{{starts}}</x-substr> 

(使用「附加」的回調,而不是「準備」,如果你要使用任何數據綁定使用此元素)

這是「正道」在Polymer 0.9+中做類似上面的過濾功能?

回答

1

在0.5中最接近過濾器行爲的是computed bindings 0.9+。

爲了您的例子,這會是這樣的:

<dom-module id="..."> 
    <template> 
    ... 
    <span>{{timeofdate(starts)}}</span> 
    ... 
    </template> 
<dom-module> 

Polymer({ 
    ... 
    timeofdate: function (input) { 
    return input.substring(11,16); 
    } 
    ... 
}); 

如果您需要這一次在一個以上的地方,你也可以讓它computed property代替。

<dom-module id="..."> 
    <template> 
    ... 
    <span>{{starttime}}</span> 
    ... 
    </template> 
<dom-module> 

Polymer({ 
    ... 
    properties: { 
    starts: String, 
    starttime: { 
     type: String, 
     computed: 'timeofdate(starts)' 
    } 
    }, 
    timeofdate: function (input) { 
    return input.substring(11,16); 
    } 
    ... 
}); 
+0

我想你建議先的方式,但我並沒有得到它的「DOM重複模板」的數據與來自使用JSON數據的鐵AJAX元件結合的內工作...你測試你的方法在一個「dom-repeat模板」裏面,如: {{item.orginalstring}}'? –

+0

我現在也得到了「計算屬性方式」的工作,輸入字符串作爲參數是這樣的:''(我不'我真的知道,我之前做錯了什麼)。無論如何,使用附加的回調在一個小元素(只有一些JS)作爲「過濾器」也適用,也許沒有「最佳途徑」。也許「PolymerExpressions.prototype」或類似的將來再次出現... –

相關問題