Questions for Knockout users: using an observable view model to do a global update

5,621 views
Skip to first unread message

Michael Best

unread,
Jan 30, 2012, 4:51:39 PM1/30/12
to KnockoutJS
I'm working on some improvements to the binding process that I hope
will be included in the next version of Knockout. I discovered while
browsing the code that Knockout will accept an observable view model.
I've created a fiddle showing that can update all the bindings by
updating the observable:
http://jsfiddle.net/mbest/gdQFK/

Here are my questions:

Have you used this feature?
Now that I've told you about it, do you think you'll use this feature?
Do you like this feature but would want it implemented differently?
Would you care if this feature was removed?

Thanks.

Michael Latta

unread,
Jan 30, 2012, 5:01:41 PM1/30/12
to knock...@googlegroups.com
Very cool. Rather than having to call applyBindings the model is just updated at all times. I like that. Yes, I expect I will use this now that it is pointed out to me. On the other hand As I think about it more, I would tend to use the ko.mapping plugin that just updates the CONTENTS of the view model, not the model itself. So, I am not so sure this one really matters.

Michael

EntitySpaces

unread,
Jan 30, 2012, 6:08:54 PM1/30/12
to KnockoutJS
Hmmm, that is sneaky. I wonder if all the DOM subscriptions and all
that are undone and re-hooked up when the entire VM is switched out?

Michael Best

unread,
Jan 30, 2012, 8:15:08 PM1/30/12
to KnockoutJS
No, they are not. All the binding are re-evaluated in the context of
the new view model object, but only the update functions for each
binding are called (the init functions are not called again).

James Ong

unread,
Jan 30, 2012, 11:58:16 PM1/30/12
to knock...@googlegroups.com
This is cool and is useful for use in any situation?

rpn

unread,
Jan 31, 2012, 12:24:20 AM1/31/12
to knock...@googlegroups.com
It is useful in a case where you want to swap out your entire view model for a new one with the same structure.  The individual properties on the view model would still need to be observable, if you wanted your UI to respond to changes to them (unless you are always swapping in an entirely new view model).

James Ong

unread,
Jan 31, 2012, 12:39:17 AM1/31/12
to knock...@googlegroups.com
I see, good for contact database using the same template.

Nerdstalker

unread,
Jan 31, 2012, 1:30:27 AM1/31/12
to KnockoutJS
Very cool.

Vuyiswa Maseko

unread,
Jul 19, 2012, 3:48:30 AM7/19/12
to knock...@googlegroups.com
hi Guys 


i need some help here , i had a problem trying to bind the ViewModel twice ,so i need your help , i still have the problem here is my Binding  


            $.getJSON("/Cars/SearchCars", data, function (result)
            {
                //Autocomplete binding
         
                var viewModel =    ko.observableArray(result) // These are the initial options 
                  
                viewModel1 = 
                {
                    SearchOptions: ko.observableArray(result),
                    switchModel: function () { viewModel(viewModel2);}
                };

                viewModel2 =
                {
                    SearchOptions: ko.observableArray(result),
                    switchModel: function () { viewModel(viewModel1); }

                };
                viewModel(viewModel1);
                ko.applyBindings(viewModel);
                alert("Done Retrieving Data");
                 
            });


Thanks 

Vuyiswa Maseko

unread,
Jul 19, 2012, 5:05:33 AM7/19/12
to knock...@googlegroups.com
The Problem happens when Knockout tries to Bind the ViewModel to the same node multiple times. seting the viewmodel to null will not work , so to resolve this i explicidly binded the tablelement, and after that i clear the node before i bind a Viewmodel

 ko.cleanNode(document.getElementById("tblsearchresults")); 


Thanks for everything
--
Wise sayings are too deep for a fool to understand , And when important matters are being discussed they are not invited.

szabolc...@gmail.com

unread,
Jul 19, 2012, 6:01:34 AM7/19/12
to knock...@googlegroups.com
Hi Michael!

Actually I tend to replace the data/content part of the vm (mostly because of performance considerations), while a colleauge of mine with strong silverlight background tends to lazily create viewmodels, like in your example:

js:

self.createTechnicianViewModel ko.observable(null);
self.createTechnician function ({
    if (+groupId !== 0{
        self.createTechnicianViewModel(
            new OrgPageCreateTechnician(groupIddsupshot.dataSources.dsChangePasswordfinishTechnicianCreating)
        );
    }
};

vm part:

....
<class="button" data-bind="click: createTechnician">Create Technician</a>
....

<div data-bind="with: createTechnicianViewModel">
    @Html.Partial("_OrgPageCreateTechnician")
</div>


I'm still torn if this latter mode is superior or not, or if it is way too resource hungry or not - it nullifies the withLight bindinghandler and also confers with my lazyBind which I'm not so happy about, but with a large single page application this seems to alleviate some of our pains. What's your opinion? Thank you!

Szabolcs

Michael Best

unread,
Jul 19, 2012, 3:55:56 PM7/19/12
to knock...@googlegroups.com, szabolc...@gmail.com
I personally use both approaches depending on the context. Of course, the 'with' binding isn't the best for performance, but we're planning to work on that soon. This version of 'with' will, like 'withlight', just update the child bindings when the view model changes, instead of replacing all the DOM nodes. Thus it's just as fast as replacing individual data values.

-- Michael

szabolc...@gmail.com

unread,
Jul 20, 2012, 3:45:14 AM7/20/12
to knock...@googlegroups.com, szabolc...@gmail.com
Thank you! I'm really looking forward to performance enhancements, right now KO with IE8 is unbearably slow :(

Abhijit

unread,
Aug 18, 2014, 7:12:19 AM8/18/14
to knock...@googlegroups.com

very cool ..... it reduce memory leak issue .

Abhijit

unread,
Feb 11, 2015, 2:07:08 PM2/11/15
to knock...@googlegroups.com
Hi,
I am facing page rendering time issue .. I have a large viewmodel
and it is bind to multiple underscores template in my page ..

now the problem is if any of element in viewmodel got update , my template re render again .. is it ok ? if not then how can I solve this issue . I am using knockout 3.0

thanks
Reply all
Reply to author
Forward
0 new messages