Aurelia binds to nested data refresh binding in user element

I want to be notified when an update has occurred in a related object. This plunk http://plnkr.co/edit/7thr0V demonstrates my problem.

In more detail: I pass the data object via bind [data.bind] to the user element. if I now update the property in the data, I would expect that the "dataChanged" hook in the user element is being called. If I show a property from an attached data object in a custom elements template, it is updated, so the binding itself works correctly.

My second reproach uses ObserverLocator, but it also does not run nested updates.

Object in app.js:

this.data = {
  nested: {
    content: "Hello nested world!"
  }
};

Binding to a custom ce element:

<require from="ce"></require>
<ce data.bind="data"></ce>

Part of ce.js:

@bindable data;

constructor(observerLocator) {
  this.observerLocator = observerLocator;

  var subscription = this.observerLocator
        .getObserver(this, 'data')
        //.getObserver(this, 'data["nested"]["content"]') //Doesn't work
        //.getObserver(this, 'data.nested.content') //Doesn't work
        .subscribe(this.onChangeData);
}

onChangeData(newData, oldData) {
  console.log('data changed from ', oldData, newData);
}

dataChanged(d) {
    console.log("Changed", d);
}

ce:

${data.nested.content}

app.js 2 . "" . . , , .

setInterval(() => {
  this.data.nested.content += "!";
}, 1000);


setInterval(() => {
  this.data = {
  nested: {
    content: "Hello nested world No. " + this.counter++  + "!"
  }
};
}, 5000);
+4
1

ObserverLocator API- Aurelia bare metal //.

API , BindingEngine, .

: https://gist.run?id=868a7611952b2e40f350

ce.html

<template>
  ${data.nested.content}


  <!-- debug logging -->
  <h4>Observed Changes:</h4>
  <div repeat.for="change of changes"><pre><code>${change}</code></pre></div>
</template>

ce.js

import {
  bindable,
  BindingEngine,
  inject
} from "aurelia-framework";

@inject(BindingEngine)
export class Ce {
  @bindable data;

  changes = []; // debug logging

  constructor(bindingEngine) {
    this.bindingEngine = bindingEngine;
  }

  expressionChanged(newValue, oldValue) {
    // debug logging:
    this.changes.splice(0, 0, `expressionChanged: "${newValue}"`);
  }

  syncSubscription(subscribe) {
    if (this.subscription) {
      this.subscription.dispose();
      this.subscription = null;
    }
    if (subscribe && this.data) {
      let observer = this.bindingEngine.expressionObserver(this.data, 'nested.content');
      this.subscription = observer.subscribe(::this.expressionChanged);
    }
  }

  dataChanged(newValue, oldValue) {
    // subscribe to new data instance
    this.syncSubscription(true);

    // debug logging:
    this.changes.splice(0, 0, `dataChanged: ${JSON.stringify(newValue, null, 2)}`);
  }

  attached() {
    // subscribe
    this.syncSubscription(true);
  }

  detached() {
    // unsubscribe (avoid memory leaks)
    this.syncSubscription(false);
  }
}

aurelia ?

, . object.observe.

+4

Source: https://habr.com/ru/post/1614162/


All Articles