Loading viewModels inside of document.ready

870 views
Skip to first unread message

Stacey Thornton

unread,
Aug 4, 2011, 3:01:43 AM8/4/11
to KnockoutJS
Something I couldn't help but notice in the documentation, samples,
examples, etc about KnockoutJS.

We never seem to use the $(document).ready(function(){ // ... }
scheme. Everything is just put inline after the HTML.

I was wondering if there is an implicit reason for this, and if
wrapping our view models in our document header will hurt performance
or cause problems. In javascript heavy pages, knowing when something
will load can be EXTREMELY important, and I fear leaving free-floating
javascript is not the wisest decision to make. Someone feel free to
correct me if I am wrong, but in a typical jQuery page, it would LOAD
like ..


- CSS files
- JS files
- html content
- knockout as encountered
- dom ready
- (document).ready jQuery code

so then what if my dom-ready code has any relationship to my view
Model?

Does JavaScript just not work in this procedural way? Does Knockout
natively understand this idea of waiting for the DOM to be ready? Am I
just an idiot? Will I ever shut up? All these answers and more in your
hopeful replies, lovely community.

(P.S - of any infrastructure support group I've ever participated in,
Knockouts is by far the best. I think we owe a lot of that to RPN and
Ω Alisson and thelinuxlich and such other top contributors. I don't
think you guys get enough credit for what you do)

rpn

unread,
Aug 4, 2011, 7:53:06 AM8/4/11
to knock...@googlegroups.com
There is no reason that you can't put your code in a ready function or at the very least put ko.applyBindings in there, especially if there is other code that manipulates the DOM first.

I think that most people don't in samples for simplicity, as it is usually not necessary.
Reply all
Reply to author
Forward
0 new messages