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

Manual constructor of a polymer element does not properly initialize #12089

Closed
sethladd opened this issue Jul 27, 2013 · 16 comments
Closed

Manual constructor of a polymer element does not properly initialize #12089

sethladd opened this issue Jul 27, 2013 · 16 comments
Labels
P1 A high priority bug; for example, a single project is unusable or has many test failures
Milestone

Comments

@sethladd
Copy link
Contributor

See also public thread: https://groups.google.com/a/dartlang.org/forum/#!topic/web-ui/zd_bPhIj6gI

This now works:

  // Just creating an HTML tag and insert it into the page. Amazing!
  container.children.add(new Element.html('<div is="my-element">'));

However, this is inserting a new <div> but is not activating the shadowDom of the element:

  runAsync(() {
    print('inside run async');
    Element host = new Element.html('<div is="my-element">');
    MyElement myElement = new MyElement()
      ..host = host
      ..created();

    container.children.add(host);
    myElement.inserted();
  });

Previous examples showed an initShadow, but that function is now called by created(). Is there something else I need to call?

[after some research]

I set me some breakpoints, here's what I found:

For the case in runAsync (below), when the code gets to _initShadowRoot(), the _localNames list is empty, and thus nothing happens in _initShadowRoot.

Finding uses of _localNames, looks like only:

void registerPolymerElement(String localName, PolymerElement create()) {
  registerCustomElement(localName, () => create().._initialize(localName));
}

is where _initialize is called (and thus, sets the localName)

So, I'm guessing we need to somehow move _initialize(localName) into created() (or some place a normal user can call for these cases when you're just constructing an element and trying to manually insert)

@jmesserly
Copy link

yeah, I don't think manual creation (as above) will work anymore. What we could do is make new Element.tag("fancy-button") create the element (assuming it is registered). Would that work?

@jmesserly
Copy link

Sorry -- I should've been more clear. Creating an element in code will work, but you should not have to do all those crazy steps! No more .host =, .created, or whatever else :)

Instead it will be just like in the Custom Elements spec: "new Element.tag" (document.createElement in JS), "new Element.html", "innerHtml = ", etc.

@jmesserly
Copy link

https://codereview.chromium.org/20886002/ has some convenience methods for CustomElement (we'll need similar for PolymerElement)

@sethladd
Copy link
Contributor Author

Not going to say no to an easier way to create a custom element :) I'd be very happy to say new Element.tag('fancy-button')

Curious, would I be able to say new FancyButton() ?

@jmesserly
Copy link

Curious, would I be able to say new FancyButton() ?

that is a question for Vijay and Stephen.

Last I heard Dartium is getting really close. Not sure about dart2js side of things.


cc @vsmenon.
cc @rakudrama.

@sethladd
Copy link
Contributor Author

Ah, makes sense.

Can vsm@ and sra@ paste in the issues for dartium and dart2js custom element support? My search didn't find them.

Thanks!

@sethladd
Copy link
Contributor Author

Bumping to High because I believe this will be fixed for 1.0


Removed Priority-Unassigned label.
Added Priority-High label.

@sethladd
Copy link
Contributor Author

Friendly update. I just tried this again with Polymer 0.5.5 and it didn't work. I cleaned up the sample to simplify:

https://github.com/sethladd/dart-polymer-dart-examples/tree/master/web/manually_add_custom_element_to_dom


cc @jmesserly.

@larsbak
Copy link

larsbak commented Aug 27, 2013

Added this to the M7 milestone.

@DartBot
Copy link

DartBot commented Sep 2, 2013

This comment was originally written by yuri...@gmail.com


Just wanted to ask if there is any workaround for creating custom Polymer elements from Dart code? I'm migrating web_ui to Polymer and need some advice. Thank you!

@DartBot
Copy link

DartBot commented Sep 3, 2013

This comment was originally written by yu...@gmail.com


I would be happy to go though all "those crazy steps" at least they worked in web-ui. In polymer nothing works :(

@jmesserly
Copy link

Hi yuriry,

try this:

    import 'package:custom_element/custom_element.dart';
    var myElem = createElement('my-tag-name');

That's all you should need to do now :)


Added Fixed label.

@jmesserly
Copy link

FYI: new Element('tag-name') is covered by:
https://code.google.com/p/dart/issues/detail?id=13009

@sethladd
Copy link
Contributor Author

sethladd commented Sep 4, 2013

Awesome. Confirmed!

@DartBot
Copy link

DartBot commented Sep 5, 2013

This comment was originally written by yurir...@gmail.com


Thanks a lot John, it works!!!

@DartBot
Copy link

DartBot commented Sep 5, 2013

This comment was originally written by yuriry...@gmail.com


Not only dynamic element creation works, lifecycle methods on dynamically created elements also get called as expected. This is great, thank you again!

@sethladd sethladd added Type-Defect P1 A high priority bug; for example, a single project is unusable or has many test failures labels Sep 5, 2013
@sethladd sethladd added this to the M7 milestone Sep 5, 2013
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P1 A high priority bug; for example, a single project is unusable or has many test failures
Projects
None yet
Development

No branches or pull requests

4 participants