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

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

问题描述

我正在为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应用程序体系结构"

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将在元"控制器上调用联系人"操作并呈现元/联系人模板.

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.

在我们的案例中,我们的应用程序更多地是前端驱动的,而后端(带有MVC的ZF)是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天全站免登陆