Ad

Access Element Scope From Dom-repeat Items Compute Method

- 1 answer

How can one access the element-scope (this) in a method which is called by a dom-repeater to return items?

Take a look at this example:

<dom-module id="demo-element">
    <template>
        <template is="dom-repeat" items="[[getItems()]]">
            <div>[[item]]</div>
        </template>
    </template>
    <script th:inline="javascript">
        Polymer({
            is : "demo-element",
            properties: {
                begin: { type: Number, value: 9 }
            },
            getItems: function() {
                console.log(this.begin); // <- logs "undefined"
                return [0,1,2,3,4];
            }
        });
    </script>
</dom-module>

this.begin can not be accessed from inside getItems().

Ad

Answer

The solution is to list the properties which you need to access like this:

<template is="dom-repeat" items="[[getItems(begin)]]">
Ad
source: stackoverflow.com
Ad