简体   繁体   English

路由如何在javascript MVC框架中工作

[英]How does routing work in javascript MVC frameworks

In most of the JavaScript MVC frameworks there is a requirement to separate your application files in to diretories like so: 在大多数JavaScript MVC框架中,需要将您的应用程序文件分成如下所示:

-App
    -Controllers
    -Models
    -Views

I'm building my own MVC framework in the hope that it will help me better understand the concepts before I go on to use one of the well established frameworks. 我正在构建自己的MVC框架,希望在我继续使用其中一个完善的框架之前,它将帮助我更好地理解这些概念。

My question is how do the existing frameworks make this directory structure work? 我的问题是现有的框架如何使这个目录结构工作? How does a controller know that the view is up a directory and in to the views folder for instance? 例如,控制器如何知道视图在目录中并且在视图文件夹中? How is this communication made possible? 这种沟通如何成为可能?

I've been googling for routing in MVC Javascript. 我一直在谷歌搜索MVC Javascript中的路由。 I can see that routing url's specify the controller to be called and parameters to be sent to this controller but this doesn't explain what I need to know. 我可以看到路由url指定要调用的控制器和要发送到该控制器的参数,但这并不能解释我需要知道的内容。

JS MVC frameworks has no directory structure. JS MVC框架没有目录结构。 You can load your MVC components dynamically from directories, but this way is pretty complicated. 您可以从目录动态加载MVC组件,但这种方式非常复杂。

Now I have the same problem with a big RIA. 现在我对一个大的RIA也有同样的问题。 I use Backbone framework and I organised directories structure by myself (my structure looks exactly like yours). 我使用Backbone框架 ,我自己组织目录结构(我的结构看起来与你的完全一样)。 Then I included all JS files in my HTML template. 然后我在我的HTML模板中包含了所有JS文件。 But for production mode I merge all my JS files and I put all MVC parts into one file. 但是对于生产模式,我合并了所有的JS文件,并将所有MVC部分放入一个文件中。

At first I tried to load MVC part dynamically from router, but I had a lot problems with loader for dependencies between models, controllers and views. 起初我尝试从路由器动态加载MVC部分,但是我在加载器中遇到了很多关于模型,控制器和视图之间依赖关系的问题。 Also it can slow down your code. 它也会减慢您的代码速度。

So best way it's load at once all js files that you need or use Require.js . 因此,最好的方法是立即加载您需要的所有js文件或使用Require.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM