With Polymer, I have implemented a custom form, which is using polymer_signals.html for listening to changes from arbitrary other polymer elements.
It is implemented like this:
<link rel="import" href="packages/polymer_elements/polymer_signals/polymer_signals.html">
<polymer-element name="user-settings" extends="form">
<template>
<polymer-signals on-polymer-signalauth-changed="{{onAuth}}"></polymer-signals>
...
This element is dynamically instantiated in a parent polymer element like this:
var userSettings = new Element.tag('form', 'user-settings');
$['main'].children.add(userSettings);
This raises the exception:
Exception: Concurrent modification during iteration: Instance(length:4) of '_GrowableList'._notify#0x1c4cf589 (http://127.0.0.1:3030/buddy/web/packages/polymer_elements/polymer_signals/polymer_signals.dart:39:12)
<anonymous closure> (http://127.0.0.1:3030/buddy/web/packages/polymer_elements/polymer_signals/polymer_signals.dart:49:12)
If I statically instantiate the polymer-form, I don't get this error. How can I prevent this?
I can't reproduce your problem.
There are two common things that are often missed
if you have a custom main method
see the answer to Polymer querySelector working on DartVM but not in Chrome after compile
call of super.polymerCreated() in the constructor of elements that extend DOM elements.
see the answer to Custom Polymer element extending AElement in Dart
If you still can't solve the problem please add more code to your question (for example how your user-settings element looks like).
Related
In AngularDart I used to extend ShadowRootAware and AttachAware to query the document with shadowRoot.querySelector(...).
Now it seems that those classes are gone. How can I query the DOM, and how do I execute some code in the "attach" phase?
I found
var shadowRoot = DOM.getShadowRoot(host);
shadowRoot should allow you to call querySelector()
Not tried myself yet though.
I have a custom polymer-element, "my-app" which uses template if="..." constructs to conditionally include other polymer elements.
All of these polymer elements are defined in one file, "views.html". This file includes the Dart code to implement all these custom elements, "views.dart".
I have a separate "index.html" that does nothing more than import the "views.html" file along with the standard polymer and dart initialization files, and then place a single my-app declaration tag in the body.
I am finding that the ".created" constructors for 1) the various subelements and 2) the my-app element itself (that conditionally includes subelements) are being called multiple times - even though a my-app element is declared only once in the simple "index.html" file.
I can see it right in the debugger.
Particularly regarding the calling of "MyApp.created", I thought this might be some garbage collection issue. But I set a global variable the first time MyApp is created to the MyApp instance to preclude this.
What is even stranger to me is that when MyApp is created repeatedly again, I can see that the global variable (to prevent gc of a MyApp instance) has been reset to "null", as if the entire file of Dart code had been reloaded/reevaluated somehow.
Very strange to me.
Are the PolymerElement subclasses for custom elements instantiated merely for the definition of custom elements? If so, that would be odd. I would think such custom polymer element subclasses would only be instantiated during the declaration of polymer elements.
Also, would a template conditional inside a polymer element definition somehow be able to force reconstruction of a the defined polymer element? This is hard to imagine, but then anything is possible with Dart Polymer, LOL!!!
I was looking for an authoritative document on when constructors for custom Dart PolymerElements might be called, but I could find no such document.
Any and all help greatly appreciated.
I have a custom #NgComponent in my project and it works if I place it within the static HTML of the application. What I'm trying to figure out is how to add one to the DOM dynamically? If I construct an instance of my Component it does not appear to be of type Element and so it cannot be added directly to the children of an element in the DOM. Is there an alternate way to construct my component or wrap it for injection into the DOM?
e.g. I naively expected to be able to do something like:
dom.Element holderEl = dom.document.querySelector("#my-holder");
holderEl.children.add( new MyComponent() );
But I have also tried simply appending HTML containing my custom element to an element using innerHTML
holder.innerHtml="<my-component></my-component>"
and creating the element using document.createElement()
dom.Element el = dom.document.createElement("my-component");
dom.document.body.append(el);
But the component does not seem to be realized when added.
thanks,
Pat
You can add components dynamically, but you must manually invoke the Angular compiler so it notices that the innerHTML has a component embedded in it.
However, that is not the "Angular way".
Instead, write your template as
<div id="my-holder">
<my-component ng-if="should_component_be_displayed"></my-component>
</div>
Here, my-component will be created and included in the DOM only if should_component_be_displayed is true.
The my-holder div can be removed which leads to a cleaner DOM structure.
Is there a procedure to call an "init" method on a polymer element in Dart in order to populate it?
I have a polymer template (still not sure it's correct) and I want to populate it with the results of an HttpRequest. I can populate with a static list, but not sure how to populate it with a dynamic list made via an http call.
Are there examples anywhere?
I'm still trying to come up to speed on Dart and Polymer ...
My hacks are at https://gist.github.com/fils/6270699
Have you considered putting the init code inside a created() lifecycle method? You can see an example of that at https://github.com/dart-lang/web-ui/blob/polymer/example/todomvc/web/todo_row.dart
I would like to pass a reference to WebComponent instances created in markup to the WebComponent dart class. For instance:
html:
<element name="x-container>
<template iterate="x in xs">
<x-content-elem>
<x-item item="{{x}}" top-container="{{lexical-scoped-ref-to-container}}">
</x-content-elem>
</template>
...
I'm looking for a way to get a reference to x-container to the x-item.top-container property. The main thing is x-item might be nested in some complicated way so doing dynamic lookup could be difficult or not very robust.
You can use the DOM to find the parent element. Something like this should work.
From inside of x-item:
Container container = this.parent.xtag;
A custom element acts like a node on the page. The elem.xtag getter returns the Dart object that backs the node on the page.
If there are other elements in-between, you can can still use CSS queries to find elements that you are looking for.