Where is the state and setState lit-element? - polymer

Coming from a react background, i was wondering where is the state and setState equivalent in lit-element, i couldn't find anything useful in lit-element docs. is it private property? or requestUpdate?

There is no state in LitElement, at least not directly.
What you have is a set of properties that you define in the static get properties() getter.
They act more or less like state does in rect, everytime one of those properties change, the element is updated.

Related

LitElement lifecycle: before first render

Is there a way to execute a method exactly after the component has its properties available but before the first render?
I mean something between the class contructor() and firstUpdated().
It sounds trivial, maybe in fact I'm missing something trivial..
The element's constructor is called when the element is created, either through the HTML parser, or for example through document.createElement
The next callback is connectedCallback which is called when the DOM node is connected to the document. At this point, you have access to the element's light DOM. Make sure to call super.connectedCallback() before doing your own work, as the LitElement instance has some work to do here.
The next callback is shouldUpdate, which is an optional predicate that informs whether or not LitElement should run its render cycle. Useful if for example, you have a single observed data property and destructure deep properties of it in render. I've found that it's best to treat this one as a predicate, and not to add all sorts of lifecycle logic inside.
After that, update and render are called, then updated and firstUpdated. It's generally considered bad practice to perform side effects in render, and the occasions that you really need to override update are rare.
In your case, it sounds very much like you should do your work in connectedCallback, unless you are relying on LitElement's rendered shadow DOM, in which case, you might consider running your code in firstUpdated, then calling this.requestUpdate() to force a second update (or changing some observed property in firstUpdated)
More info: https://lit-element.polymer-project.org/guide/lifecycle

dynamic class in AS3 : listening to property creation?

I'm currently working on a project that involve a re-implementation of the Array class.
This object needs to be an Array for compatibility reasons, while I also need to keep control of what is written in.
I cannot seem to find any way to check property creation inside of a dynamic object in AS3. Something that would work like the Event.ADDED_TO_STAGE but, like, ClassEvent.PROPERTY_ADDED.
I override methods like push, splice etc, but I cannot control direct assignation : MyArray[i] = ...
Is such a thing even possible ?
Of course, I could make some kind of validations elsewhere, but this would involve accessing a part of the code I cannot modify.
Thanks for your time !
I'm not sure I follow you entirely but you may be looking for the Proxy class:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Proxy.html
An example at the bottom shows you how you can override direct assignment:
override flash_proxy function setProperty(name:*, value:*):void {
_item[name] = value;
}
Using this you would be able to dispatch a custom event that would be fired any time an item was added to your ProxyArray

Dynamic Tree Generation in Flex Actionscript

I am using the URL logic for creating a dynamic Tree in Flex using action script. However the output is not properly shown (Object name is shown instead of Label).
Code is available in above mentioned URL.
Please help.
Write a correct toString() implementation of your DataTreeNode, so it would have a proper display in this tree.
An example: Provided the class DataTreeNode has a data:Object field, and this object has a urlToDisplay:String property that you want displayed. Do like this:
override public function toString():String {
if (!data) return '[null]';
return data.urlToDisplay;
}
If you only rely on simple Objects or data classes, you can use the tree's labelField or labelFunction in order to read and/or format data, which is passed to the renderer. There are no new item renderer classes needed.
New renderer should be compatible with these functions!
On a site note: item renderer are not "mostly just simple MXML classes", they are component instances. It doesn't matter how there are implemented. There are best practises like avoiding data bindings in item renderers, that's why it is common to use the markup for drawing, but implementing the views behaviour according to the Flex component live cycle. you might want to read about it in the documentation, because it is a necessary read for a Flex developer.
You will have to write an ItemRenderer that tells your tree how it should display the components. ItemRenderers are mostly just simple MXML classes that access one item each and display the data in any way you want. You will have to assign the ItemRenderer to your component.
See this article:
http://help.adobe.com/en_US/flex/using/WS03d33b8076db57b9-23c04461124bbeca597-8000.html

Binding issue, unable to bind to property * on class *

I have a class named Student, it has several properties along with the property "isSelected:Boolean"and the class is defined Bindable.
[Bindable]
[RemoteClass(alias="portal::Student")]
public class Student
In an mxml application I have a datagrid in which its dataprovider has been set to an ArrayCollection of Students. I have a column of checkboxex for the datagrid along with a headerItemRenderer checkbox which is supposed to select all the students (all checkboxes in rows should be selected or deselected).
I have defined a handler for the click event of the checkbox in the header which sets the isSelected property of each Student object in dataProvider to false or true. But on click of this check box in the header, I get the warning: unable to bind to property 'isSelected' on class 'Student' and therefore check boxes in rows do not get updated.
I don't get why the binding does not work here and don't know what to do to fix this issue. Any help is greatly appreciated.
It depends, i guess you have somewhere in the code a reference to the student instance, you want to bind on. This reference has to be declared [Bindable] as well, in order to get the binding chain to work.
Also, when you not depend on ActionScript to get the binding to work, you don't have to extend EventDispatcher, because MXML will generate that code for you (with regards to Cosma's comment).
Do you define an inline renderer? This is not the optimum in my opinion, since a component is generated and debugging gets a lil fuzzy, since you might run into scope problems. I'd recommend implementing a new component, implement IDataRenderer and propagate the data to the children through the live cycle or implement the component in MXML. Why IDataRenderer? This interface is necessary.
Also, i'm not sure on that one, but shouldn't it be
[Bindable]
[RemoteClass(alias="portal.Student")]
public class Student {}
?
Let your actionscript class extends EventDispatcher (binding is based on event dispatching).
You need to make sure that your ItemRenderer saves the incoming data in a property marked as [Bindable]. You'll want to pass the entire Student in, rather than just "isSelected"...

IComponentActivator Instance

How can I use an IComponentActivator instance for a component, not just specifying a type.
That is, instead of
Component.For<XYZ>.Activator<MyComponentActivator>();
I want to be able say
Component.For<XYZ>.Activator(new MyComponentActivator(someImportantRuntimeInfo));
Also, is there a way I can choose an activator dynamically for a non specifically registered component? That is, I want to have an activator that looks at the type being resolved, decides if it can activate it, and if not, responsibility for activation should be passed on to the default activator.
So basically I want a global IComponentActivator that has the following logic:
Create(Type type){
if (ShouldActivate(type)){
DoActivate(type);
}
else{
// do default activation here somehow
}
}
Thanks!
EDIT
Just use ILazyComponentLoader
Activators are instantiated by the container, and there is no way to provide instances OOTB.
If you really want to do this, I'd say you can extend the container itself.
Put your custom instace activators in ExtendedProperties of your component under a well-known key
inherit from DefaultKernel
Override CreateComponentActivator method
In there return the activator from ExtendedProperties of the component
There's no way to create global activator. You can add IContributeComponentModelCreation implementation that switches the activator to some custom one wen creating your component if you really want to.
But the most important question is this:
What are you trying to achieve by
this? Why do you want to do this in
the first place?