简体   繁体   中英

Creating a listener on a list for changes

I have a dart object which contains a list. Simply put:

class Test extends PolymerElement{
  List list = [];

  addTask(item){
    list.add(item);
  }
}

and I wanted to implement a listener on that list in another object:

class listenerClass extends PolymerElement {
  Test get _data => $['Object'];
}

So the object is retrieved from the dom withthe getter.

Is there a way to observe a change in the parent? I tried the following:

@Observe("_data.list")
dataListChanged(_,__){
  var item = _data.list.last;
  //do magic with item.
}

I have the underscore because it is private and isnt exposed to Dom or anything... but the list itself isnt a property so it doesnt notify or anything.

I was hoping there was a way to do some sort of listener for it though.

My desired endstate is that I want to fire a function in the parent whenever an item is added to the list, with only the reference to the child object as defined above. Even though this _data is populated by way of Polymer, Since this isnt touching properties at all, the answer may likely just be Pure dart.

This is not how polymer works. Polymer framework has no way to know you have defined a getter in your class and emit the right notification.

You should add notify to the list property of Test and bind that to a property in listenerClass . Then observe that variable.

But probably you will have better luck with ObservableList and using autonotify for a simpler way to use observability with polymer-dart projects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM