Knockout ES5 - subscribe

124 views
Skip to first unread message

Richard Griffiths

unread,
Jan 18, 2018, 11:14:59 AM1/18/18
to KnockoutJS
I have just become aware of the plugin Knockout-ES5.

1) What are peoples thoughts on using this plugin - advisable?

2) I'm having an issue with subscribing to a property....

I use a dto class in my main VM to hold data items.

Can't seem to be able to subscribe using code below (error - can't find property)


 ko.getObservable(vm, 'dto.LabelFrom').subscribe(function (newValue) {
        alert('The new value is ' + newValue);
    }); / /fails


 ko.getObservable(vm.dto, 'LabelFrom').subscribe(function (newValue) {
        alert('The new value is ' + newValue);
    }); / / also fails

Can anyone advise - thanks.

Gunnar Liljas

unread,
Jan 18, 2018, 12:21:03 PM1/18/18
to knock...@googlegroups.com
What is vm.dto? Could you show us more code?

/G

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Richard Griffiths

unread,
Jan 18, 2018, 12:34:40 PM1/18/18
to KnockoutJS


On Thursday, 18 January 2018 16:14:59 UTC, Richard Griffiths wrote:
I have just become aware of the plugin Knockout-ES5.

1) What are peoples thoughts on using this plugin - advisable?

2) I'm having an issue with subscribing to a property....

I use a dto class in my main VM to hold data items.

Can't seem to be able to subscribe using code below (error - can't find property)




/// <reference path="../knockout-3.4.2.debug.js" />
var vm;

function OrderLabelsVM() {
    var self = this;
    self.dto = new OrderLabelsDto();
    self.test = "123";
        ko.track(this);
}

var OrderLabelsDto = function (data) {
    var self = this;
    self.LabelFrom ="1234";// data ? data.LabelFrom : '0001';
    self.LabelTo = data ? data.LabelTo : '2000';
    self.TextOnLabel = data ? data.TextOnLabel : '.net';
    self.Message = data ? data.Message : 'Ready'; 
}

$(document).ready(function () {
    vm = new OrderLabelsVM();
    ko.applyBindings(vm);
    ko.getObservable(vm, 'test').subscribe(function (newValue) {
        alert('The new value is ' + newValue);
    });
    //ko.getObservable(vm, 'dto.LabelFrom').subscribe(function (newValue) {
    //    alert('The new value is ' + newValue);
    //});
    
}); 

Richard Griffiths

unread,
Jan 19, 2018, 6:40:25 AM1/19/18
to KnockoutJS
I have resolved this by adding 

 ko.track(this);

to my dto class


On Thursday, 18 January 2018 16:14:59 UTC, Richard Griffiths wrote:
Reply all
Reply to author
Forward
0 new messages