Knockout en Rails 3

if I'm using rails 3 which uses asset pipeline to compile all Javascripts, does that mean I can have only one Knockout view model for my entire application? If not, how do I specify which view model is binded with which view? In the tutorial code, it looks like 1 view model is bound per page, but that doesnt work in rails since all JS are loaded upon first page load.

preguntado el 31 de julio de 12 a las 14:07

1 Respuestas

No, you do not need to include all javascript on every page! This is a very bad idea.

There are many methods for limiting javascript to a single page, you should pick one:
Método 1
Método 2
Método 3

Please, please, please do not try to load all your javascript on every single page.

Noticias (after your comment below):

I think you are confusing a few different things here.

First, even if you compile all your javascript into a single gzipped/uglified file, that still doesn't force you to use one knockout viewmodel for your entire application. That file can contain multiple viewmodels. They don't even need to know about each other.

Second, the way the rails pipeline works is by concatenating related or dependant javascript files together. It does this to reduce the number of solicitudes the browser has to make to get the javascript it needs for each page. It doesn't necesariamente mean all your javascript becomes one file. Just that the javascript para cada página become one file. For more information, check out the Rails Asset Pipeline Documenation, it has a great explanation of how it works and how to use it properly.

Third, neither of these things mean you need to write all your javascript as if it were one file. In fact, this is a bad idea. You should seperate your javascript into relevant files by functionality. This allows them to be reusable, as well as eases development work.

contestado el 23 de mayo de 17 a las 12:05

If all JS is uglified and compressed via Gzip, is that better than serving JS that's controller specific, such as using method 3? However, if I want to use one big file, that won't work with Knockout? - red

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.