te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to tell when dom-repeat is done stamping elements - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to tell when dom-repeat is done stamping elements - Stack Overflow

programmeradmin3浏览0评论

In Polymer, the dom-repeat template helper emits a dom-change event whenever it stamps the result of an iteration into the DOM. Is there a way for me to tell when ALL of the iterations are plete?

In Polymer, the dom-repeat template helper emits a dom-change event whenever it stamps the result of an iteration into the DOM. Is there a way for me to tell when ALL of the iterations are plete?

Share Improve this question edited Sep 6, 2015 at 17:33 Luke asked Sep 6, 2015 at 16:39 LukeLuke 5,7085 gold badges39 silver badges68 bronze badges 4
  • Is it possible to determine this by looking at data in the dom change event - knowing what data item should be last and then checking for that? Perhaps not the ideal solution, however. – mmm111mmm Commented Sep 6, 2015 at 19:14
  • 1 github./Polymer/polymer/blob/master/src/lib/template/… @348 it fires an event at the conclusion of its render() – Robert Rowntree Commented Sep 6, 2015 at 20:07
  • 1 as Robert says above the dom-change event is fired when the render of the dom-repeat template is plete. You should then be able to add to your template the following line of code which will execute a function when this event is fired: <template is="dom-repeat" on-dom-change="_functionToCall"> – Ben Thomas Commented Sep 7, 2015 at 8:44
  • How Can I distinguish the final dom-change event from the other ones though? – Luke Commented Sep 7, 2015 at 11:07
Add a ment  | 

2 Answers 2

Reset to default 12

Maybe this simple example can help explain the behaviour and extend on the ments.

<dom-module id="change-tester">
    <template>
    <h1>Change Tester</h1>
        <ul>
        <template id="template" is="dom-repeat" items="{{content}}">
            <li>{{item}}</li>
        </template>
        </ul>
        <button on-click="more">Add more</button>
    </template>
</dom-module>
<script>
    Polymer({

        is: 'change-tester',
        properties: {
            content: {
                type: Array,
                value: function(){ return ["one", "two", "three"]}
            }
        },
        ready: function(){
            this.$.template.addEventListener("dom-change", function(event){
               console.log(event);
            });
        },

        more: function(){
            this.push("content", "four");
            this.push("content", "five");
        }
    });
</script>

Whenever dom-change is fired, I log the event to the console, so open the dev tools and have a look. Initially, the dom-repeat has three iterations and will populate the dom with three elements. Note that only one event will fire, namely when all three elements have been added. If you click the button, two more items are added to the content in the repeat. As the dom-repeat updates asynchronously, these two items are again handled in one go and will only trigger one event.

So the dom-change event is actually that final event you are looking for. It will only fire again, if you manipulate the items that are bound to it.

To access a dynamically created node in Polymer 1.x (like template repeat) use:

this.$$('#id')

For Polymer 2.x use:

this.shadowRoot.getElementById('id');

instead of this.$.id

for example if the template id is template you should use

this.$$('#template')
example
ready: function(){
    this.$$('#template').addEventListener("dom-change", function(event){
        console.log(event);
    });
},
发布评论

评论列表(0)

  1. 暂无评论