dart-archive / observe

Support for marking objects as observable, and getting notifications when those objects are mutated
https://pub.dartlang.org/packages/observe
BSD 3-Clause "New" or "Revised" License
13 stars 6 forks source link

ObservableList not updated in template #72

Open DartBot opened 9 years ago

DartBot commented 9 years ago

Originally opened as dart-lang/sdk#21568

This issue was originally filed by defav...@gmail.com


What steps will reproduce the problem?

  1. create a custom element with an ObservableList field (mylist)
  2. put the list in the element template ({{mylist}})
  3. add a new element to the list

What is the expected output? What do you see instead? I should see the list updated (["new element"]), instead the list is not updated ("[]").

What version of the product are you using? Dart 1.7.2, polymer-expression 0.13.0

Please provide any additional information below.

DartBot commented 9 years ago

Comment by zoechi


I encountered this very recently with a Map. When you have a field

@observable List myList = []; // or even // List myList = toObservable([]);

and add it to the template of your Polymer element like

{{myList}}

Id doesn't get updated when you add/remove/modify items in the List. This is mostly because myList hasn't changed and this is what {{myList}} refers to. myList still refers the same list.

{{myList}} implicitely uses toString() but it seems toString() is not observed for changes. {{myList.toString()}} doesn't change this (at least this was the case for my Map example).

If you do something like

void addItem(Event e) {   myList.add('xxx');   var tmp = myList;   myList = null;   new Future(() => myList = tmp); }

then the view is updated.

Using a filter didn't help either

String asString(List val) {  return val.toString() }

{{myList | asString}}

What should work is

String _oldMyListString; @observable String get myListString => myList.toString();

myList.changes.listen((recs) {   _oldMyListString = notifyPropertyChange(#myListString, _oldMyListString, myListString); });

{{myListString}}

caution: code not tested

Maybe someone from the Dart team knows a better workaround...

DartBot commented 9 years ago

Comment by kasperl


Added Area-Pkg, Pkg-Observe, Triaged labels.

DartBot commented 9 years ago

Comment by jakemac53


This has caused a lot of confusion and come up a few times, I might spend a bit of time today seeing if I can get it working correctly. This is really only useful as a debugging feature but I think that even if its just for that its worth getting right.