Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding sub-properties with Google Polymer

Is it possible to bind sub-properties automatically with Google Polymer? Something like AngularJS.

Here is a small example:

This is the element that contains the property prop1 and updates it after 2sec:

<dom-module is="test-elem">

    <script>
        TestElem = Polymer({
            is: "test-elem",

            properties: {
                prop1: { type: String, value: "original value", notify: true }
            },

            factoryImpl: function() {
                var that = this;

                setTimeout(function(){
                    that.prop1 = "new value";
                }, 2000);
            }
        });
    </script>

</dom-module>

And this is the main page, which creates an element and show prop1 in the dom:

<template is="dom-bind" id="main-page">

    <span>{{test.prop1}}</span>

</template>

<script>
    var scope = document.getElementById('main-page');

    var t = new TestElem();

    scope.test = t;

</script>

Unfortunately, the page doesn't update with the new value. Is there a way to bind this automatically?

Here is a jsfiddle: http://jsfiddle.net/xkqt00a7/

like image 963
Mathew Avatar asked Sep 28 '22 03:09

Mathew


2 Answers

The easiest way I found was to add an event handler:

t.addEventListener('prop1-changed', function(e){
        scope.notifyPath('test.prop1', e.currentTarget.prop1)
});

http://jsfiddle.net/83hjzoc3/

But it's not automatic like AngularJS.

like image 170
Mathew Avatar answered Oct 20 '22 19:10

Mathew


I can't explain why, but switching your example from setTimeout to this.async makes it work.

like image 28
jojomojo Avatar answered Oct 20 '22 19:10

jojomojo