aurelia-contrib / aurelia-getting-started

Basically a queue of topics and examples to cover
MIT License
20 stars 5 forks source link

Array binding and observing #18

Open Alexander-Taran opened 6 years ago

Alexander-Taran commented 6 years ago

one of the biggest frustrations for developers is array binding and observing.. It all comes down to javascript

That should be covered in detail with examples

if you you want to ... than you can.. the result will be.. side effects..

https://github.com/aurelia/binding/issues/670

discussion from gitter:

mariaantony-gnanasekaran @mariaantony-gnanasekaran 10:27 Hi @Vheissu ,

We tried your suggestion. but unfortunately it doesn't work. firstNameChanged callback will be triggered only when we do assignment operation with array.

That's why we used collection observer to observe the array changes.

When we do push,pop, slice operation in our array dataSource, dataChanged callback function is getting triggered.

But when we change the array with index like the below code snippet

let arr = [ a:1]; arr.a =2; dataChanged callback function is not getting trigger but view is getting updated.

Is there any way to observe the array changes, when we do array operation with index?

Alexander-Taran @Alexander-Taran 10:29 @mariaantony-gnanasekaran yeah use .splice () to replace the item that will trigger array change handlers .splice(i/ index of item to change /, 1 , newValue)

mariaantony-gnanasekaran @mariaantony-gnanasekaran 11:53 Hi @Alexander-Taran ,

Thanks for your suggestion.

I need to know is this an issue from Aurelia core? because whenever the property gets changed, we expect callback method to get triggered for notifying the change.

Alexander-Taran @Alexander-Taran 12:28 @mariaantony-gnanasekaran It is that it is not magic.. got the same thing with react.. atm it all ties down to how objects are observed. properties get converted to getters and setters.. and observing code is injected in to setters.. so whenever you assign something to a property - framework is notified.. for arrays there is another strategy.. I don't know the details but it somehow observes changes to array structure.. that's why splice works, but assigning by index does not.. because there is no way to covert indexer to getter setter.. I guess.. And what makes it confusing that in your view - it works.. because obervers are for properties of the objects in array.. We have to document it in understandable way..

But that is all how javascript works.. not specific to aurelia