KnockoutJS organization

178 views
Skip to first unread message

kirua.z...@gmail.com

unread,
Apr 26, 2016, 1:45:08 PM4/26/16
to KnockoutJS
Hi

I start an application with asp5 mvc6 and knockout, and i want to know how to oragnize my knockout file
I want to do 1 knockout viewmodel = 1 view

 exemple : operaion.js == > operation.cshtml/html

is there a special way to do it ?

i try to do it but i saw that we can't do multiple ko.applybindings ..
can we import all js file in one file and use this last one like <script src="main.js"></script>

the best way to declare view model is

function viewModel() {

}

kirua.z...@gmail.com

unread,
Apr 26, 2016, 1:47:26 PM4/26/16
to KnockoutJS
    self....
}
ko.applybindings(new viewModel())

or 

var vm = {
  p1 = "r"
}


thanks all

Gunnar Liljas

unread,
Apr 26, 2016, 4:16:23 PM4/26/16
to knock...@googlegroups.com
"is there a special way to do it ?"

No. Knockout doesn't deal with application structure. Just try to implement a structure and stock with it.

"the best way to declare view model is"

It's mostly a matter of taste. I usually prefer the "function" way.

--
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.

kirua.z...@gmail.com

unread,
Apr 27, 2016, 2:25:03 AM4/27/16
to KnockoutJS
Ok
But how can i organize my files ?
i have file1.js and file2.js, both have ko.applybindings, and i have an error said multiple bindings is not authorize.
And should i declare all js file in main html or the right one in the right html ?

Bryan Smith

unread,
Apr 28, 2016, 11:18:04 AM4/28/16
to KnockoutJS
Multiple Bindings - You can only bind one knockout model to a portion of the DOM. There are ways to divide this up (exclude portions, bind to a specific portion of the DOM, etc). 

Declaration - We bind per page. e.g. file1.js to file1.html. For extensions and utility js classes we use RequireJS. We put the RequireJS right along the Knockout model (e.g. same file). This way we have our model, whatever else we needed for that model all handled nicely for us via Require.

Organization -  We keep all of our models in /js/app/viewDirStructure for ease of matching. 

kirua.z...@gmail.com

unread,
Apr 29, 2016, 2:54:19 AM4/29/16
to KnockoutJS
It means in every html page, i need to load jquery then knockout then myviewmodel.js ?
is the applybindings of myviewmodel1.js is clear when i load page2.html then myviewmodel2.js with another myviewmodel2.js ?

Le mardi 26 avril 2016 19:45:08 UTC+2, kirua.z...@gmail.com a écrit :

Tien Quang Phan

unread,
May 5, 2016, 11:55:01 PM5/5/16
to KnockoutJS
men, using bundles (Script.Render) in asp.net mvc to load shared scripts (jquery, bootstrap, ko...) and section (RenderSection) to include viewmodel into each html page.
Reply all
Reply to author
Forward
0 new messages