Reputation: 758
I am new to polymer.
Currently I am stuck at a step which I don't know how to solve. Suppose I have an index.html in which I have used two components: <table></table>
and <search></search>
. The table uses a parameter items
that holds the content of the table. And <search></search>
basically searches from DB based on some search criteria. Now how to update the item
parameter of the from inside the <search></search>
element.
I hope that I have made myself clear. If not, then please ask me .
Thank you
Upvotes: 0
Views: 64
Reputation: 348
There's different paradigms in which you could take this problem. One common paradigm is to keep 1-way data flow (known as data down, actions up), which is idiomatic to frameworks like React. Polymer can handle two-way binding, so you could also handle it this way.
I would
This is an example of the data down/actions up paradigm using a higher-order-component. that is your parent component in this case. it is coordinating the updating/flow of the other 2 components.
I added a plunkr so you can also see an example of polymer's 2-way binding. The main difference between this and the instructions above is that we are not passing any methods down from the parent to the children for the children to call. Instead we are enabling the data bindings to be bi-directional, which means that if the child updates a passed down property, the change will automatically propagate up without you having to intervene. You make a prop double bound by using the curly braces and not the bracket braces and in the property definitions of the child, set the notify
property to true.
Which way you choose to go is up to you and your team. Some teams find that 2-way binding is difficult to reason about because it could be hard with deeply nested components to discover who changed a property. Passing down an action to deeply nested components in my opinion can also be confusing, so I am not necessarily against 2-way binding, which also got a bad name because early implementations of it in other frameworks had performance issues, but Polymer's in my experience does not.
Upvotes: 2
Reputation: 1609
You can alternatively make use of events to pass the data, answer given by Trey is also correct and his suggestion is apt. You should use parent component do all that hard work. However if you still wish to continue in the manner you have gone so far I suggest you use events to pass the data,
handleIncomingData(e) {
this.dispatchEvent(new CustomEvent('kick', {detail: {ajaxData: yourAjaxData}}));
}
this snippet is adopted from the polymer site itself, make sure you pass the data using the detail property of the event.
Refer this link,
https://www.polymer-project.org/2.0/docs/devguide/events
Upvotes: 1