如何构建 Javascript 架构以补充 PHP MVC Web 应用程序? [英] How to structure Javascript architecture to complement a PHP MVC web app?

查看:18
本文介绍了如何构建 Javascript 架构以补充 PHP MVC Web 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为网络应用迭代开发新的 JavaScript 架构.之前的迭代有很多内联代码,分散的包含,没有 .js 文件的目录结构,一切都在全局命名空间中.我的目标是:将脚本包含在页脚中,将所有内容保留在应用程序命名空间/对象中,将组织添加到 .js 文件并将所有特定于应用程序的文件缩小到一个 bundle.js 中

I am working on a new JavaScript architecture for a web app iteration. The previous iteration had lots of inline code, scattered includes, no directory structure for .js files and everything was in the global namespace. I am aiming to: keep the script includes in the footer, keep everything in an application namespace/object, add organization to the .js files and minify all the application specific files in to one bundle.js

我正在尝试采用基于 Nicholas Zakas 的模块化方法:可扩展的 JavaScript 应用程序架构"http://www.yuiblog.com/blog/2009/09/17/video-bayjax-sept-09/

I am trying to take a modular approach based on Nicholas Zakas: "Scalable JavaScript Application Architecture" http://www.yuiblog.com/blog/2009/09/17/video-bayjax-sept-09/

网站目前的结构是这样的

the site is currently structred like so

/app
 /models
 /views
   /home
   /auth
   /meta
     about.tpl
     contact.tpl
     privacy.tpl
 /controllers
   home.php
   auth.php
   meta.php
 /public
  /js
   core.js
   /modules
     module files here
   /jquery
     jqueryplugins here
   /controllers
     home.js
     auth.js
     meta.js

控制器具有对应于我们的 url 路由和视图渲染的方法.例如,http://localhost/meta/contact 将调用meta"控制器上的contact"动作并呈现元/联系人模板.

the controllers have methods which correspond to our url routing and view rendering. For example http://localhost/meta/contact would call the "contact" action on the "meta" controller and render the meta/contact template.

我正在围绕对应用程序全局对象的单个初始化调用规划 js 架构,将控制器和方法作为参数传递给它,即

I am planning the js architecture around a single initialization call to the apps global object passing it the controller and method as arguments i.e.

localwebapp.init(controller, method);

在设计的这一点上,我正在为继承和模块实现而苦苦挣扎.一些模块将是全局的并在整个站点中使用,一些模块将在特定控制器中使用,而一些模块将仅用于控制器操作.模块是独立的,不会相互通信,它们需要被分配到一个沙箱",它们将检查事件触发器

At this point in the design I am struggling on inheritance and module implementation. Some modules will be global and be used throughout all the site, some modules will be used through out specific controllers, and some modules will be on controller actions only. Modules are independent and will not communicate with each other they will need to be assigned to a "sandbox" which they will check with for event triggers

我想我需要沙箱和模块类.控制器脚本基本上是分配给沙箱并初始化的几行模块.

I'm thinking I will need sandbox and module classes. The controller scripts will basically be a few lines of modules being assigned to the sandbox and initialized.

如果我在这里重新发明轮子,请告诉我.任何方向都非常感谢.我已经查看了 javascript MVC 框架的工作原理类似于 JavaScriptMVC,但它看起来不是我需要的

Let me know if I am reinventing a wheel here. Any direction is much appreciated. I have looked in to javascript MVC frame works like JavaScriptMVC but it looks like it is not what I need

推荐答案

我们也使用 JavaScript MVC.您可以在应用程序中同时使用两者.

We use JavaScript MVC as well. You can use both in an application.

在我们的例子中,我们的应用程序更多是由前端驱动的,后端(ZF 与 MVC)是一个 REST API,而 JavaScript MVC 可以很容易地做到这一点.功能集仍然在 PHP 部分,而不是客户端,我们只是利用了 JavaScript MVC 的很多部分来使它看起来更快速等等.

In our case, our application is more frontend driven and the backend side (ZF with MVC) is a REST API and JavaScript MVC makes it easy to do so. The feature set is still in the PHP part, and not client-side, we just utilize a lot of parts of JavaScript MVC to make it look snappier, etc..

最后,我看不出为什么不能反过来.

In the end, I see no reason why it couldn't be the other way around though.

我认为对 MVC 中的视图部分的误解是它可以在浏览器中看到.视图也可以是 XML 或 JSON.我相信你知道这一点,但我想强调这一部分,因为它让大多数人望而却步.

I think the misconception about the view part in MVC is that it's something to see in the browser. The view can be XML or JSON as well. I'm sure you know that, but I wanted to emphasize this part since it is what throws most people off.

如果您通常询问 JavaScript MVC —— 我不知道它是否是最好的 MVC 框架(客户端),但它迫使您定义模型、控制器,并且它带有一个测试框架以确保事情按计划进行.

If you generally ask about JavaScript MVC -- I don't know if it's the best MVC framework (client-side-wise), but it forces you to define models, controllers and it comes with a testing framework to make sure things go according to plan.

让我知道这是否有帮助!

Let me know if this helps!

这篇关于如何构建 Javascript 架构以补充 PHP MVC Web 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆