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

If I set element property before component registered I cannot change it anymore #1882

Closed
cwayfinder opened this issue Jun 16, 2015 · 3 comments

Comments

@cwayfinder
Copy link

If we create an element and set some properties previously to being initialized like in the following example:

<script>
  var selector = document.createElement("iron-selector");
  selector.selected = 1;
  selector.setAttribute('id', 'selector');
  selector.innerHTML = "<div>Item 1</div><div>Item 2</div><div>Item 3</div>";
  document.body.appendChild(selector);

  Polymer.Base.importHref('bower_components/iron-selector/iron-selector.html');

  console.log('hasOwnProperty', selector.hasOwnProperty('selected'));
</script>

then in console line selector.selected = 2;does nothing.

Cause: own property in the selector overrides the property with setter and getter in behavior (IronSelectableBehavior).

The same issue happens with many other elements as well.

@cwayfinder
Copy link
Author

Polymer creates setter and getter in the accessors.html line #154

@sorvell
Copy link
Contributor

sorvell commented Jun 16, 2015

This behavior is by design. When element declarations are loaded lazily via HTML imports, it's necessary to wait for the declarations to register and for existing elements to upgrade. In general, the callback method in Polymer.Base.importHref can be used as a ready signal. For example, here's a working version of the code posted above:

Polymer.Base.importHref('bower_components/iron-selector/iron-selector.html', function() {
  var selector = document.createElement("iron-selector");
  selector.selected = 1;
  selector.setAttribute('id', 'selector');
  selector.innerHTML = "<div>Item 1</div><div>Item 2</div><div>Item 3</div>";
  document.body.appendChild(selector);
});

While it's true that Polymer could attempt to save and re-apply property values that were set prior to element registration/upgrade, this could not be done for methods, and we've chosen instead to avoid trying to sugar over the general issue that elements can exist in 2 states: un-upgraded and upgraded. User code should be written such that it deals with this fact.

@sorvell sorvell closed this as completed Jun 16, 2015
@manolo
Copy link

manolo commented Jun 17, 2015

@sorvell Although I might agree about not running accessors with previous property values, I think it should not prevent the accessor not working at all if the property was set, because the component remains unusable if the user has set some property previously. Thus imho, when accessors are configured, if not reapplied it should delete previous property values, so as new accessors are usable.

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

3 participants