Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.8] Properties observers registered too early #1258

Closed
cedric-marcone opened this issue Mar 5, 2015 · 3 comments
Closed

[0.8] Properties observers registered too early #1258

cedric-marcone opened this issue Mar 5, 2015 · 3 comments

Comments

@cedric-marcone
Copy link
Contributor

Properties observers seem to be set up too early.
If I declare a default value for a property, the property change callback is invoked before the component is ready.

<script>
Polymer({

  is: 'x-component',

  properties: {
    opened: {
      type: Boolean,
      observer: 'openedChanged'
    }
  },

  configure: function() {
    return { opened: false }
  },

  openedChanged: function (newVal, oldVal) {
    console.log('openedChanged', newVal, oldVal);
  },

  ready: function() {
    console.log('ready');
  }
});
</script>

This leads to the following output in the console :

openedChanged false undefined
ready

Do we really want to observe the transition from undefined to default value ?

PS: I updated the code to reflect today's changes in ca0953c but it behaved the same prior to @kevinpschaaf commits.

@robbear
Copy link

robbear commented Apr 16, 2015

I am seeing the same thing, but not in a native browser like Chrome. So it appears the lifecycle path differs between Shadow DOM and Shady DOM. This simple test demonstrates the order of operation, run under a particular browser type:

lifecycle-tester.html:

<link rel="import" href="polymer/polymer.html">
<dom-module id="lifecycle-tester">
  <template>
    <content></content>
  </template>
</dom-module>
<script>
var LifecycleTester = Polymer({
  is: 'lifecycle-tester',

  created: function() {
    console.log('lifecycle-tester: created');
  },

  attached: function() {
    console.log('lifecycle-tester: attached');
  },

  detached: function() {
    console.log('lifecycle-tester: detached');
  },

  ready: function() {
    console.log('lifecycle-tester: ready');
  },

  attributeChanged: function() {
    console.log('lifecycle-tester: attributeChanged');
  },

  properties: {
    fooAttribute: {
      type: Number,
      reflectToAttribute: true,
      value: 1
    }
  }
});
</script>

index.html:

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <script src="webcomponentsjs/webcomponents-lite.js"></script>
  <link rel="import" href="lifecycle-tester.html">
</head>

<body onload='onLoad()'>
  <div id="container"></div>
  <script>
    function onLoad() {
      var container = document.getElementById('container');
      var fixture = document.createElement('lifecycle-tester');
      container.appendChild(fixture);          
    }
  </script>
</body>
</html>

Under Chrome:

lifecycle-tester: created
lifecycle-tester: ready
lifecycle-tester: attributeChanged
lifecycle-tester: attached

Under Firefox:

lifecycle-tester: created
lifecycle-tester: attributeChanged
lifecycle-tester: ready
lifecycle-tester: attached

@arthurevans
Copy link

@robbear What you're talking about (lifecycle callbacks) is unrelated to whether property observers fire when the default value is set.

Some ordering is different on native webcomponents vs. polyfilled webcomponents. However, we make some ordering guarantees that you can rely on (created => ready => attached are always fired in that order).

@sorvell
Copy link
Contributor

sorvell commented May 22, 2015

The design notion here is that the element starts in a pristine state and you are informed of all changes that point on, including default values. Input values are also, by design, set before the ready method is called. By ready time the element's local dom has been created and the element itself has been configured with values.

We've also now normalized attached timing such that it is never called before ready.

@sorvell sorvell closed this as completed May 22, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants