In polymer-dart how do I count inside a repeating template

泄露秘密 提交于 2019-12-05 19:23:52

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>

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!