Pregunta

Given any knockout example, I want to be able to identify the source of an observable update.

Consider for example this simple code:

HTML

<input type='text' data-bind='value: someValue' />
<span data-bind='text: someValue'></span>
<button data-bind='click: updateValue'>Update</button>

js

var vm = function () {
    var self = this;
    this.someValue = ko.observable('random value');
    this.updateValue = function () {
        self.someValue('random value ' + Math.round(Math.random()*10));
    }
}

var vmi = new vm();

vmi.someValue('other random value');

ko.applyBindings(vmi);

Fiddle

I want to know if someValuewas last updated via the input tag, the button, or via code.

What do you think would be the best way to do so? (extender, custom binding handler, ...)

¿Fue útil?

Solución

Create separate "writable computed observables" for each of those things to modify. See http://knockoutjs.com/documentation/computedObservables.html#writeable_computed_observables. In your write methods for each of these, you can handle differentiation/coordination/syncing between them as appropriate. There might be a better solution though if you described your actual scenario.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top