Object references in Polymer repeat statements

500 views
Skip to first unread message

Junius Gunaratne

unread,
Jan 16, 2014, 11:29:03 AM1/16/14
to polym...@googlegroups.com
 I can't seem to put a JS object reference as an attribute in a template repeat statement. Anyone have an idea why this doesn't work?

This works:
<polymer-custom-parent>
<template repeat="{{item in items}}">
<polymer-custom-item item="randomstring">
    {{item.title}}
</polymer-custom-item>
</template>
</polymer-custom-parent>

This does not:
<polymer-custom-parent>
<template repeat="{{item in items}}">
<polymer-custom-item item="{{item}}">
    {{item.title}}
</polymer-custom-item>
</template>
</polymer-custom-parent>

Console error:
Exception caught during observer callback: TypeError: Cannot read property 'length' of undefined at Element.bind (http://localhost:8888/bower_components/platform/platform.js:33:5389)

Fernando Hurtado

unread,
Jan 16, 2014, 11:43:58 AM1/16/14
to polym...@googlegroups.com
I had the same issue beginning this week, fiexed with some IFs:

<polymer-custom-parent>
<template if="{{ items }}">
  <template repeat="{{item in items}}">
  <polymer-custom-item item="{{item}}">
      {{item.title}}
  </polymer-custom-item>
  </template>
</template>
</polymer-custom-parent>

Hope that helps

Héctor Fernando

Rafael Weinstein

unread,
Jan 16, 2014, 12:08:40 PM1/16/14
to Fernando Hurtado, polymer-dev
Can one of you guys post a jsbin (or similar) which shows the error? Sounds like a bug.


Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Fernando Hurtado

unread,
Jan 16, 2014, 12:17:30 PM1/16/14
to polym...@googlegroups.com, Fernando Hurtado
Hi Rafael, I tried to documented what happened on my case here: https://groups.google.com/forum/?fromgroups=#!topic/polymer-dev/zpC1xQ-WfMs

Thanks 

Héctor Fernando

Eric Bidelman

unread,
Jan 16, 2014, 1:33:08 PM1/16/14
to Fernando Hurtado, polymer-dev
This repos the issue Raf: http://jsbin.com/UGEHuxO/2/edit


which means the next release should unbreak this :)

Rafael Weinstein

unread,
Jan 16, 2014, 1:39:13 PM1/16/14
to Eric Bidelman, Fernando Hurtado, polymer-dev
I was thinking it was probably that (as Dan also speculated). Eric, thanks for verifying. Guys, sorry for the breakage.

Junius Gunaratne

unread,
Jan 17, 2014, 11:25:14 AM1/17/14
to Rafael Weinstein, Eric Bidelman, Fernando Hurtado, polymer-dev
I updated to the latest Polymer 0.1.3 and also applied Héctor's advice of using an if statement to check for a null object. However, I'm still having data binding issues when using a repeat statement. Polymer still doesn't seem to like me referring to a JS object as an attribute in a repeat statement with a custom element. Am I doing something wrong, or is this a bug? If this is a bug are there work arounds in the interim?

This works:

<link rel="import" href="bower_components/polymer/polymer.html">
<polymer-element name="polymer-list">
  <template if="{{ items }}">
    <template repeat="{{ item in items }}">
      <polymer-custom-elem>{{item}}</polymer-custom-elem>
    </template>
  </template>
<script>
  (function() {
    Polymer('polymer-list', {
      ready: function() { 
        this.super();
        this.items = [1,2,3];
      }
    });
  })();
</script>
</polymer-element>



<link rel="import" href="bower_components/polymer/polymer.html">
<polymer-element name="polymer-custom-elem" attributes="item">
<template>
  <content></content>
</template>
<script>
  (function() {
    Polymer('polymer-custom-elem', {
      item : '',
      ready: function() { 
        this.super();
      },
    });
  })();
</script>
</polymer-element>


Creating an attribute that invokes data binding doesn't work:

<link rel="import" href="bower_components/polymer/polymer.html">
<polymer-element name="polymer-list">
  <template if="{{ items }}">
    <template repeat="{{ item in items }}">
      <polymer-custom-elem item="{{item}}"></polymer-custom-elem>
    </template>
  </template>
<script>
  (function() {
    Polymer('polymer-list', {
      ready: function() { 
        this.super();
        this.items = [1,2,3];
      }
    });
  })();
</script>
</polymer-element>


<link rel="import" href="bower_components/polymer/polymer.html">
<polymer-element name="polymer-custom-elem" attributes="item">
<template>
  {{item}}
  <content></content>
</template>
<script>
  (function() {
    Polymer('polymer-custom-elem', {
      item : '',
      ready: function() { 
        this.super();
      },
    });
  })();
</script>
</polymer-element>



Error:



Eric Bidelman

unread,
Jan 17, 2014, 2:57:12 PM1/17/14
to Junius Gunaratne, Rafael Weinstein, Fernando Hurtado, polymer-dev
You can ditch the null check on the outside <template> in polymer-list. The first <template> in an element is what Polymer generates shadow DOM from. You can also ditch the calls to this.super() since you're not inheriting from other elements.

Updated demo that works: http://jsbin.com/oqotUdE/1/edit

Daniel Freedman

unread,
Jan 17, 2014, 3:31:18 PM1/17/14
to Eric Bidelman, Junius Gunaratne, Rafael Weinstein, Fernando Hurtado, polymer-dev
It seems that the ready function in polymer-custom-elem is to blame. Removing the "this.super" call makes your example work just fine.


Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev...@googlegroups.com.

Yvonne Yip

unread,
Jan 17, 2014, 3:44:19 PM1/17/14
to Daniel Freedman, Eric Bidelman, Junius Gunaratne, Rafael Weinstein, Fernando Hurtado, polymer-dev
It seems like there's a bug with "super" when the element is created by a <template repeat>. bug: https://github.com/Polymer/polymer/issues/397


Reply all
Reply to author
Forward
0 new messages