Polymer: How to watch for change in <content> properties

tschie

I'm just starting to learn Polymer. Here is a generic version of my polymer element:

<polymer-element name="my-element">
    <template>
        <style>
        :host {
            position:absolute;
            width: 200px;
            height: 100px;
            background-color: green;
        }
        </style>
        <p>my element</p>
        <content id="first-in"></content>
        <content id="second-in"></content>
    </template>
    <script>
    Polymer('my-element', {
        domReady: function() {
            alert(this.children[0].getAttribute('title')); 
            //this returns the value I want to observe
        }
    });
    </script>
<polymer-element>

The content tags are both being filled with another custom element (again slightly simplified):

<polymer-element name="in-element" attributes="title">
    <template>
        <style>
        ...
        </style>
        <p>{{title}}</p>
    </template>
    <script>
    Polymer('in-element', {
        title: 'me'
    });
    </script>
<polymer-element>

What I want to be able to do is call a function in my-element when the title attribute in (any) in-element (put in the content tag) is changed (by a click event or whatever). I'm not sure how to access this using observe or if I need to use mutationObserver, etc. How is this done/Is it possible?

ebidel

Native properties like title are not observable by Polymer's data-binding system (e.g. Object.observe() [info]). It's generally a good idea to avoid them.

In your example, I've changed title to mytitle and published it with reflect: true so the property value reflects back to the attribute. This way you can completely avoid .getAttribute() and just check .mytitle on in-elements. You can also use {{mytitle}} in bindings.

You can do this through mutation observers [1]. Polymer provides onMutation to monitor children but you want to monitor attributes of children. For that, you need a pure MO:

ready: function() {
  var observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(m) {
      if (m.target.localName == 'in-element') { // only care about in-element
        console.log(m.attributeName, m.oldValue, m.target.mytitle);
      }
    });  
  });
  // Observe attribute changes to child elements
  observer.observe(this, {
    attributes: true,
    subtree: true,
    attributeOldValue: true
  }); 
}

Demo: http://jsbin.com/doxafewo/1/edit

In domReady(), I also changed your alert of this.children[0] to this.$.firstin.getDistributedNodes()[0].mytitle. Using getDistributedNodes() is better because you're guaranteed to have the nodes that are actually passing through the <content> insertion point [2].

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to change item properties programmatically (Content Processor XNA 4.0 )?

From Dev

AngularJS: Watch transcluded content change with interpolation?

From Dev

How to watch file for new content and retrieve that content

From Dev

How to watch file for new content and retrieve that content

From Dev

Polymer: How to access elements within the 'content' tag

From Dev

How to render content with multiple AJAX calls with Polymer?

From Dev

Google Polymer - how to add content to Neon elements

From Dev

Google Polymer - how to add content to Neon elements

From Dev

How to edit the CSS of a <content> selector in Polymer 1.0

From Dev

How to watch on change of select with ng-change?

From Dev

how to watch specified `properties` in `router` and react accordinlgy?

From Dev

How to change the properties of a button

From Dev

polymer 1.0 - how to change font size programmatically?

From Dev

How to reflect change from child to parent in Polymer

From Dev

How to change underline border in paper input Polymer

From Dev

How to change content on hover

From Dev

How to change content flow?

From Dev

How to change content on hover

From Dev

how to change SpannableString content?

From Dev

How to change font size of label Apple Watch

From Dev

How to prevent value change inside $watch

From Dev

How to $watch state change of $stateProvider in AngularJS?

From Dev

How to change Apple Watch app icon?

From Dev

How to change Apple watch table cell background?

From Dev

How to change $watch value in unit test?

From Dev

How to watch array or object change in service?

From Dev

How to watch a directory and `cp` file on change?

From Dev

Adding properties for a Polymer element to observe based on content or a better way to handle forms

From Dev

Polymer defining properties in element

Related Related

  1. 1

    How to change item properties programmatically (Content Processor XNA 4.0 )?

  2. 2

    AngularJS: Watch transcluded content change with interpolation?

  3. 3

    How to watch file for new content and retrieve that content

  4. 4

    How to watch file for new content and retrieve that content

  5. 5

    Polymer: How to access elements within the 'content' tag

  6. 6

    How to render content with multiple AJAX calls with Polymer?

  7. 7

    Google Polymer - how to add content to Neon elements

  8. 8

    Google Polymer - how to add content to Neon elements

  9. 9

    How to edit the CSS of a <content> selector in Polymer 1.0

  10. 10

    How to watch on change of select with ng-change?

  11. 11

    how to watch specified `properties` in `router` and react accordinlgy?

  12. 12

    How to change the properties of a button

  13. 13

    polymer 1.0 - how to change font size programmatically?

  14. 14

    How to reflect change from child to parent in Polymer

  15. 15

    How to change underline border in paper input Polymer

  16. 16

    How to change content on hover

  17. 17

    How to change content flow?

  18. 18

    How to change content on hover

  19. 19

    how to change SpannableString content?

  20. 20

    How to change font size of label Apple Watch

  21. 21

    How to prevent value change inside $watch

  22. 22

    How to $watch state change of $stateProvider in AngularJS?

  23. 23

    How to change Apple Watch app icon?

  24. 24

    How to change Apple watch table cell background?

  25. 25

    How to change $watch value in unit test?

  26. 26

    How to watch array or object change in service?

  27. 27

    How to watch a directory and `cp` file on change?

  28. 28

    Adding properties for a Polymer element to observe based on content or a better way to handle forms

  29. 29

    Polymer defining properties in element

HotTag

Archive