我在 Breeze 中有一个多对多的关系:
Product *---1 ProductWidgets 1----* Widgets
产品需要知道它的任何小部件何时发生变化。可以随时从产品中添加或删除小部件。
理想情况下,我想做类似的事情:
product.widgets.on('change', function () {});
...但我想我需要类似的东西:
var handleWidgetChange = function (changes) {
console.log("here are the changes", changes);
};
for(var i = 0; i < product.productWidgets.length; i++) {
// make sure we're getting events for the current set of widgets
product.productWidgets[i].widget.entityAspect.propertyChanged.subscribe(handleWidgetChange);
// if any of the current set of product widgets gets pointed elsewhere, catch that
product.productWidgets[i].entityAspect.propertyChanged.subscribe(function (change) {
if (change.propertyName === "widget") {
change.oldValue.entityAspect.propertyChanged.unsubscribe();
change.oldValue.entityAspect.propertyChanged.subscribe(handleWidgetChange);
}
})
}
// handle new product widgets and removal of product widgets
product.productWidgets.arrayChanged.subscribe(function (change) {
if (change.added) {
change.added[0].widget.entityAspect.propertyChanged.subscribe(handleWidgetChange);
} else if (change.removed) {
change.removed[0].widget.entityAspect.propertyChanged.unsubscribe();
}
});
有没有推荐的方法来实现这一目标?
(注意:我正在使用角度,并且很想这样做,$watch('product.productWidgets', function () {}, true)
但这会产生循环引用错误。)