After overriding three lifecycle methods of WebComponent: created(), inserted(), and removed(), I can see that the first two are called consistently but removed() is never called. Is there anything special that needs to be done so that removed() is called? Or is it simply never called?
The removed() method is called when a custom element is removed from the DOM. Here is a small program that demonstrates the use of the created(), inserted(), and removed() lifecycle events.
Create a Dart web application with an index.html file that looks like this:
<!DOCTYPE html>
<html>
<head>
<title>index</title>
<link rel="import" href="my_element.html">
<script src="packages/polymer/boot.js"></script>
</head>
<body>
<div id='container'><my-element></my-element></div>
<script type="application/dart">
import 'dart:html';
void main() {
query('#container').onClick.listen((event) {
event.target.remove();
});
}
</script>
</body>
</html>
This file imports and displays a custom element, <my-element>.
Define the following file that defines <my-element>:
<!DOCTYPE html>
<html>
<body>
<polymer-element name="my-element">
<template>
<p>The name is {{person.name}}</p>
</template>
<script type="application/dart" src="my_element.dart"></script>
</polymer-element>
</body>
</html>
And define the accompanying Dart file that demonstrates the lifecycle methods getting called:
import 'package:polymer/polymer.dart';
class Person extends Object with ObservableMixin {
#observable String name;
Person(this.name);
}
#CustomTag("my-element")
class MyElement extends PolymerElement {
#observable Person person = new Person('Shailen');
void created() {
super.created();
print('created');
}
void inserted() {
print('inserted');
}
void removed() {
print('removed');
}
}
When you run index.html, you will see a paragraph with some text in it. The created() and inserted() lifecycle methods are called, and 'created' and 'inserted' messages print in the console. When you click on the div that contains the custom element, the element is removed, the removed() lifecycle method is called, and 'removed' prints in the console.
Hope this helps.
Related
I want to detect attribute change, then to refresh the element.
I do
main.dart
library main;
import "package:polymer/polymer.dart";
import "dart:html";
void main() {
initPolymer().run(() {
Polymer.onReady.then((e) {
querySelector("nav-bar").setAttribute("mode", "ok");
});
});
}
navbar.html
<link rel="import" href="packages/polymer/polymer.html">
<polymer-element name="nav-bar" attributes="mode">
<template if="{{mode == null}}">
<p>Hello</p>
</template>
<template if="{{mode == 'ok'}}">
<p>Youpie</p>
</template>
<script type="application/dart" src="navbar.dart"></script>
</polymer-element>
navbar.dart
import 'package:polymer/polymer.dart';
#CustomTag('nav-bar')
class NavBar extends PolymerElement {
#published String mode;
NavBar.created() : super.created();
}
When I put my element in index.html, whatever "mode" value, I have always Hello.
When I change the mode using query, nothing happen. The text should be changed...
I did something wrong ?
There are two ways that should solve the problem
querySelector("nav-bar").mode = "ok");
or
#PublishedProperty(reflect: true) String mode;
or both (in my opinion it would be best to apply both)
I have a component that performs a task, but then needs to refresh the data (a method callable from the controller).
Is it possible to access the controller methods and properties from a component?
Is it available through Scope? Parent/root scope?
(Also noticed I'm using a directive as well which would love to access the parent scope)
I used $broadcast ... Not sure if that's the proper way to do it or not. I imagine trying to access the controller from component/directive is circular? So maybe $broadcast is what I want?
...For methods it makes sense I guess...But I do feel like I should be able to get a property from the controller. I must be doing something wrong if I can't see the parent scope...
You have to set the visibility on the parent then you can inject the parent to the child. (I'm only on my phone and limited possibilities) AFAIK SO has a question+answer already.
It sounds like you have one controller and one component which is a child element of the controller. In this case you can inject the controller in the component's constructor:
#NgController(...)
class MyController {
refresh() {
...
}
}
#NgComponent(...)
class MyComponent {
MyController _myController;
MyComponent(MyController this._myController);
doSomething() {
_myController.refresh();
}
}
Components should basically be reusable and therefore shouldn't have any reference to the calling controller. Here is one solution where two separate controllers use one component to modify controller's property. I'm not sure have I used watch-function properly or is there better way to do this.
Idea comes from this question and answers:
AngularDart components and model binding
edit: ops, sorry: One controller and two different properties.
ang_testi.dart:
import 'package:angular/angular.dart';
import 'package:di/di.dart';
#NgComponent(
selector: 'my-component'
)
class MyComponent {
NgModel _ngModel;
Scope _scope;
MyComponent(this._ngModel, this._scope){
_scope.$watch(() => _ngModel.modelValue, (value) => onC(value));
}
onC(value){
if(value!=null) _ngModel.modelValue=value.toUpperCase();
return _ngModel.modelValue;
}
}
#NgController(
selector: '[main-test]',
publishAs: 'ctrl')
class MainTestController {
String msg;
String msg2;
Scope _scope;
MainTestController(this._scope){
_scope.$watch(() => msg, (value) => onMsgC(value));
}
onMsgC(value){
print("msg change:$value");
}
}
class MyAppModule extends Module {
MyAppModule() {
type(MainTestController);
type(MyComponent);
}
}
void main() {
ngBootstrap(module: new MyAppModule());
}
ang_testi.html:
<!DOCTYPE html>
<html ng-app>
<head>
<meta charset="utf-8">
<title>ng-model test</title>
<link rel="stylesheet" href="ang_testi.css">
</head>
<body main-test>
<my-component ng-model="ctrl.msg"></my-component>
<my-component ng-model="ctrl.msg2"></my-component>
<p><input type="text" ng-model="ctrl.msg"></p>
<p>Hello world from {{ctrl.msg}}!</p>
<p><input type="text" ng-model="ctrl.msg2"></p>
<p>Hello world from {{ctrl.msg2}}!</p>
<script src="packages/shadow_dom/shadow_dom.min.js"></script>
<script type="application/dart" src="ang_testi.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
I'm sorry if I'm polluting this thread.
I found a much simpler way to create a component, which can manipulate data in the calling controller. In the following example the #NgComponent transforms the string from the calling controller to uppercase and preserves a twoway binding.
The key to preserve binding is to use the variable of type: List, Map or Class (I believe also Set and Queue will work). With these types Dart uses "pointers", not values (sorry if the terms are not correct).
The correct type of variable makes also following code work (if you use for example type: String, bindings are not working): https://github.com/angular/angular.dart/issues/264
ang_testi.dart
import 'package:angular/angular.dart';
import 'package:di/di.dart';
#NgComponent(
selector: 'my-component'
)
class MyComponent {
List _test;
#NgTwoWay('test')
set test(List list){
_test=list;
}
List get test{
_test[0]=_test[0].toUpperCase();
return _test;
}
}
#NgController(
selector: '[main-test]',
publishAs: 'ctrl')
class MainTestController {
List msg=[""];
List msg2=[""];
}
class MyAppModule extends Module {
MyAppModule() {
type(MainTestController);
type(MyComponent);
}
}
void main() {
ngBootstrap(module: new MyAppModule());
}
ang_testi.html
<!DOCTYPE html>
<html ng-app>
<head>
<meta charset="utf-8">
<title>ng-model test</title>
<link rel="stylesheet" href="ang_testi.css">
</head>
<body main-test>
<my-component test="ctrl.msg"></my-component>
<my-component test="ctrl.msg2"></my-component>
<p><input type="text" ng-model="ctrl.msg[0]"></p>
<p>Hello world from {{ctrl.msg[0]}}!</p>
<p><input type="text" ng-model="ctrl.msg2[0]"></p>
<p>Hello world from {{ctrl.msg2[0]}}!</p>
<script src="packages/shadow_dom/shadow_dom.min.js"></script>
<script type="application/dart" src="ang_testi.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
I'm trying to nesting in Dart PolymerElements in another PolymerElement like this.
#CustomTag('test-box')
class Box extends PolymerElement{
#observable List<Child> childs = new List<Child>();
Box.created() : super.created() { }
}
#CustomTag('test-child')
class Child extends PolymerElement{
Child.created() : super.created() { }
}
and then in testbox.html
<link rel="import" href="testchild.html">
<polymer-element name="test-box">
<template>
<div>
<ol name="child-list">
<template repeat="{{child in childs}}">
{{child}}
</template>
</ol>
</div>
</template>
<script type="application/dart" src="testbox.dart"></script>
</polymer-element>
Is that possible with Dart/Polymer? All my tries are failed.
I want to handle html nodes like classes.
Thanks in advance
You can use a model object to pass data to a child element via published property.
Check this example: https://github.com/sethladd/dart-polymer-dart-examples/tree/master/web/observable_objects_inside_list_changes
You can add the children nodes to the box, for example:
#CustomTag('test-box')
class Box extends PolymerElement{
#observable List<Child> childs = new List<Child>();
Box.created() : super.created() {
}
void _addChildren(List<Child> children) {
children.forEach((Child c) {
this.children.add(c);
}
}
#override void attached() { super.attached(); _addChildren(childs); }
}
Then you can monitor changes on childs using observable API to reflect the changes on the array.
Beware that Child object should be created with new Element.tag("test-child").
But IMHO the best solution is the one offered by #Leksat using a more pure MVC approach.
I had pretty much the same problem and solved it using some kind of proxy element.
ProxyElement Dart code:
library ProxyElement;
import 'package:polymer/polymer.dart';
#CustomTag('proxy-element')
class ProxyElement extends PolymerElement {
#published PolymerElement target;
ProxyElement.created() : super.created();
attached() {
shadowRoot.querySelector('#proxy').append(target);
}
}
And its HTML code:
<link rel="import" href="../packages/polymer/polymer.html">
<polymer-element name="proxy-element">
<template>
<style>
:host {
display: inline;
}
</style>
<template if="{{target == null}}">
No target element defined.
</template>
<template if="{{target != null}}">
<div id="proxy"></div>
</template>
</template>
<script type="application/dart" src="proxy_element.dart"></script>
</polymer-element>
Usage:
...
<template repeat="{{child in children}}">
<proxy-element target="{{child}}"></proxy-element>
</template>
...
In the following example, when the Update Class button is clicked, {{testClass.someProperty}} is updated with the new class' someProperty, but updating testClass.someProperty does not update {{testClass.someProperty}} in the template.
I thought making the TestClass #observable would make its properties observable and when updating the property, the property in the template would change.
What would be the correct way to bind TestElement.testClass.someProperty to {{testClass.someProperty}}, and as an extension, any class' properties to a Polymer Element template?
test_class.dart
library test;
import "package:polymer/polymer.dart";
#observable
class TestClass extends ObservableBase {
String someProperty;
TestClass(this.someProperty);
}
test_element.dart:
library test_element;
import "package:polymer/polymer.dart";
import "test_class.dart";
#CustomTag("test-element")
class TestElement extends PolymerElement with ObservableMixin {
#observable
TestClass testClass = new TestClass("original");
void updateClass() {
testClass = new TestClass("xyz");
}
void updateProperty() {
testClass.someProperty = "foobar";
}
}
test_element.html
<polymer-element name="test-element">
<template>
{{testClass.someProperty}}
<button on-click="updateProperty">Update Property</button>
<button on-click="updateClass">Update Class</button>
</template>
<script type="application/dart" src="test_element.dart"></script>
</polymer-element>
test.dart
library test;
int main() {
}
test.html:
<!DOCTYPE html>
<html>
<head>
<link rel="import" href="test_element.html" />
<script src="packages/polymer/boot.js"></script>
</head>
<body>
<test-element></test-element>
<script type="application/dart" src="test.dart"></script>
</body>
</html>
I got it to work by moving #observable from the class to the property. I thought annotating the class with #observable was supposed to be the same as annotating all the properties as observable, but that didn't work:
test.dart
library test;
import "package:polymer/polymer.dart";
class TestClass extends ObservableBase {
#observable
String someProperty;
TestClass(this.someProperty);
}
I have a class field that is a String and I want to use a text input to modify that field. How can I use #observable with polymer.dart to do this?
Here is the class field I would like to sync-up with the UI:
class Person {
#observable String name;
Person(this.name);
}
Import the polymer.dart file and mix in the ObservableMixin into Person. Extend PolymerElement, and also use a #CustomTag annotation.
Here is what the dart file using #observable with a custom element could look like:
import 'package:polymer/polymer.dart';
class Person extends Object with ObservableMixin {
#observable String name;
Person(this.name);
}
#CustomTag("custom-element")
class CustomElement extends PolymerElement {
#observable Person person = new Person('John');
}
In the associated .html file, use {{}} syntax to create the binding with the #observable field:
<!DOCTYPE html>
<html>
<body>
<polymer-element name="custom-element">
<template>
<label> Name: <input value="{{person.name}}"></label>
<p>The name is {{person.name}}</p>
</template>
<script type="application/dart" src="element.dart"></script>
</polymer-element>
</body>
</html>
This element can be used in the following manner (note the link to boot.js):
<!DOCTYPE html>
<html>
<head>
<title>index</title>
<link rel="import" href="element.html">
<script src="packages/polymer/boot.js"></script>
</head>
<body>
<custom-element></custom-element>
<script type="application/dart">
void main() {}
</script>
</body>
</html>