2016-11-29 182 views
2

如果我有以下dom-repeat模板:计算属性

<template is="dom-repeat" items="{{myFiles}}" as="file"> 
    <span> 
    {{file.createDate}} <br/> 
    </span> 
</template> 

,我想格式化file.createDate,有没有使用computed property做到这一点的方法吗?

回答

3

没有,你就需要在项目(或在这种情况下,其子属性)使用computed binding

// template 
<template is="dom-repeat" items="{{_myFiles}}" as="file"> 
    <span>{{_formatDate(file.createDate)}}</span> 
</template> 

// script 
Polymer({ 
    _formatDate: function(createDate) { 
    return /* format createDate */; 
    } 
}); 

或者,你可以使用一个计算的属性(如命名为_myFiles)在myFiles阵列,这将处理dom-repeat迭代之前的所有项目:

// template 
<template is="dom-repeat" items="{{_myFiles}}" as="file"> 
    <span>[[file.createDate]]</span> 
</template> 

// script 
Polymer({ 
    properties: { 
    myFiles: Array, 
    _myFiles: { 
     computed: '_preprocessFiles(myFiles)' 
    } 
    }, 
    _preprocessFiles: function(files) { 
    return files.map(x => { 
     x.createDate = /* format x.createDate */; 
     return x; 
    }); 
    } 
}); 
+0

我不得不定义我的计算性能为这样: MYFILES:{ type:Array, computed:'_preprocessFiles(myFiles)', notify:true } –