Hello World example

65 views
Skip to first unread message

Greg Kh

unread,
Sep 22, 2015, 12:30:19 PM9/22/15
to KnockoutJS
At the beginning of knockout.. looking into Hello World example.. trying to figure out the following :
It tracks changes on the View, i.e when first name is changed it goes to 'fullName' , but I can not figure out how to make changes in the model so it update 'fullName'.. i.e. if in console I would type ; ViewModel('One', 'Two') then is executes function updates this.firstName and this.lastname  but fullName unchanged and no changes on the UI.. so how to apply changes from the ViewModel side .. Thanks.

Gunnar Liljas

unread,
Sep 22, 2015, 4:02:13 PM9/22/15
to knock...@googlegroups.com
var vm=new ViewModel("Planet", "Earth");
ko.applyBindings(vm);
vm.firstName("Something else")

2015-09-22 18:30 GMT+02:00 Greg Kh <greg.k...@gmail.com>:
At the beginning of knockout.. looking into Hello World example.. trying to figure out the following :
It tracks changes on the View, i.e when first name is changed it goes to 'fullName' , but I can not figure out how to make changes in the model so it update 'fullName'.. i.e. if in console I would type ; ViewModel('One', 'Two') then is executes function updates this.firstName and this.lastname  but fullName unchanged and no changes on the UI.. so how to apply changes from the ViewModel side .. Thanks.

--
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/d/optout.

Greg Kh

unread,
Sep 22, 2015, 4:43:35 PM9/22/15
to KnockoutJS
this code engages knockout, but how exactly change values of observables in the ViewModel ( firstName  or lastName)  from console   
 var ViewModel = function(first, last) {
        this.firstName = ko.observable(first);
        this.lastName = ko.observable(last);
 
        this.fullName = ko.computed(function() {
            
            return this.firstName() + " " + this.lastName();
        }, this);
    };
   $(function () {
       
       ko.applyBindings(new ViewModel("Planet", "Earth")); 
     
    });

Gunnar Liljas

unread,
Sep 22, 2015, 5:06:00 PM9/22/15
to knock...@googlegroups.com
You must maintain a reference to the viewmodel instance. Otherwise you can't access the properties.

vm=new ViewModel("Planet", "Earth");
ko.applyBindings(vm);


Access vm...


Greg Kh

unread,
Sep 22, 2015, 5:37:30 PM9/22/15
to KnockoutJS
Thanks.. I just needed to get it out of function scope.. it works now the way I need:
   vm = new ViewModel("Planet", "Earth");
   $(function () {
         ko.applyBindings(vm); 
     
    });
Reply all
Reply to author
Forward
0 new messages