在聚合物飞镖如何计数重复模板内 [英] In polymer-dart how do I count inside a repeating template

查看:157
本文介绍了在聚合物飞镖如何计数重复模板内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个重复的模板:

Say I've got a repeating template:

<template bind repeat id='my-template'>
    This is the bound value: <span id="#myid[x]"> {{}} </span>
</template>

我可以用什么替换[x]

what can I replace [x] with that will be unique? Access to the loop counter would do the trick, but I'm open to suggestions.

推荐答案

我添加了一些实用程序Fancy语法(Polymer.dart的默认绑定语法)来帮助这一点,但基本的大纲是运行你的集合通过一个过滤器,将添加索引并返回一个新的Iterable。

I'm adding some utilities to Fancy Syntax (Polymer.dart's default binding syntax now) to help with this, but the basic outline is to run your collection though a filter that will add indices and return a new Iterable.

这里有一些代码,现在可以做到:

Here's some code that will do it now though:

import 'package:fancy_syntax/fancy_syntax.dart';
import 'package:mdv/mdv.dart';

Iterable<IndexedValue> enumerate(Iterable iterable) {
  int i = 0;
  return iterable.map((e) => new IndexedValue(i++, e));
}

class IndexedValue<V> {
  final int index;
  final V value;

  IndexedValue(this.index, this.value);
}

main() {
  query('#my-template')
    ..bindingDelegate = new FancySyntax(globals: {
      'enumerate': enumerate,
    })
    ..model = ['A', 'B', 'C'];
}



<template bind id='my-template'>
  <template repeat="{{ item in this | enumerate }}">
    This is the bound value: <span id="#myid-{{ item.index }}">{{ item.value }}</span>
  </template>
</template>

我试图得到一堆实用程序,如Python的itertools到库中使用像这样。我会在可用时更新。

I'm trying to get a bunch of utilities like Python's itertools into a library for uses like this. I'll update when they're available.

这篇关于在聚合物飞镖如何计数重复模板内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆