A Knockout observable updated by jQuery doesn't trigger change

1,483 views
Skip to first unread message

ja...@jackmcdade.com

unread,
Feb 28, 2013, 4:01:37 PM2/28/13
to knock...@googlegroups.com
I have a textarea like the following:

<textarea data-bind="value: settings, valueUpdate: 'afterkeydown'"></textarea>

The value is populated initally by jQuery via AJAX. Apparently Knockout doesn't see that it's been changed, which prevents the various binding dependencies to be out of sync. I've tried adding .change() on the AJAX success event, but no luck.

Any ideas?

Michael Latta

unread,
Feb 28, 2013, 4:06:35 PM2/28/13
to knock...@googlegroups.com
You need to include more information.  Is settings an observable in the view model you called applyBindings on?  Do you use vm.settings(...new value...) to change the value in the view model.  KO knows nothing about data from AJAX unless it is put into the view model.

Michael


--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jack McDade

unread,
Feb 28, 2013, 4:09:58 PM2/28/13
to knock...@googlegroups.com
Yes, settings is an observable in the vm:

<script>
var koWidget;

$(document).ready(function(){

koWidget = {
settings: ko.observable('')
};

ko.applyBindings(koWidget);
});
</script>

The AJAX stuff is completely external to the vm though. So KO doesn't watch the data for changes then? All change has to all happen encapsulated inside the viewmodel?

Michael Latta

unread,
Feb 28, 2013, 4:13:25 PM2/28/13
to knock...@googlegroups.com
KO watches the view model observables, that is it.  It does not know about any other data.  If you receive data from AJAX it would have no idea where it should go without you putting it into the view model where you want it.

Michael

Charlie Knoll

unread,
Mar 1, 2013, 1:44:18 PM3/1/13
to knock...@googlegroups.com, ja...@jackmcdade.com
Reply all
Reply to author
Forward
0 new messages