6
我有一個對象列表(例如,人),我動態地添加和從列表中刪除。我想在列表中的任何項目上某個屬性發生更改時在整個列表中運行查詢。如何使用聚合物鏢偵聽列表中物體屬性的變化?
例如,我想知道列表中的任何對象是否已更改其「signedAgreement」屬性。我不想手動將偵聽器連接到每個對象,我只想問清單。我怎樣才能做到這一點?
我的代碼:
library my_element;
import 'package:polymer/polymer.dart';
import 'dart:html';
import 'models.dart';
@CustomTag("my-element")
class MyElement extends PolymerElement with ObservableMixin {
final List people = toObservable([]); // observe adds/removes to the list
final Person newPerson = new Person();
// How can I know when to re-evaluate signedCount?
int get signedCount => people.where((Person p) => p.signedAgreement).length;
void save(Event e, var detail, Node target) {
people.add(new Person.from(newPerson));
newPerson.blank();
}
}
我的模型對象的樣子:
library models;
import 'package:polymer/polymer.dart';
class Person extends Object with ObservableMixin {
@observable String name;
@observable bool signedAgreement = false;
Person();
Person.from(Person other) {
name = other.name;
signedAgreement = other.signedAgreement;
}
blank() {
name = '';
signedAgreement = false;
}
}
'ListPathObserver'被標記爲不推薦 - 這樣做的新方法是什麼? – mpg