简体   繁体   English

PHP MVC与纯Javascript视图:良好的做法?

[英]PHP MVC with pure Javascript View : good practice?

My question might not be understandable enough, so let me explain the situation : 我的问题可能不够明白,所以让我解释一下情况:

I'm working on a big ajax webApp built, server side, with PHP using CodeIgniter. 我正在使用CodeIgniter开发一个使用PHP的大型ajax webApp构建服务器端。 This framework saperates clearly models, controllers and views. 该框架清楚地表明了模型,控制器和视图。 The view files are rendered in HTML and then sent to the client that does some js treatments on it (like attaching events). 视图文件以HTML格式呈现,然后发送到客户端,对其执行一些js处理(如附加事件)。

This way of working seems strange to me, as it separates the view between the server side and the client side. 这种工作方式对我来说似乎很奇怪,因为它将服务器端和客户端之间的视图分开。

I was thinking about moving all the View treatments to the client part that will build dynamically its html in js. 我正在考虑将所有View处理移动到客户端部分,它将在js中动态构建其html。 The server side will then send only raw data. 然后,服务器端将仅发送原始数据。

I worked this way on smaller projects, and I was really happy with the result (easily understandable, portable and reusable). 我以这种方式在较小的项目上工作,我对结果非常满意(易于理解,便携和可重复使用)。

Is it a right way to implement an MVC app ? 这是实现MVC应用程序的正确方法吗? Any advices around that reflexion ? 关于那种反思的任何建议?

I've done pretty much what you are describing on a fairly large data services app as an internal application. 我已经完成了你在一个相当大的数据服务应用程序上作为内部应用程序所描述的内容。 In my case I was using ExtJS for the client-side rendering/views, and was communicating to a C# WCF endpoint exposed on the web server. 就我而言,我使用ExtJS进行客户端渲染/视图,并与Web服务器上公开的C#WCF端点进行通信。 Essentially requests were made/submitted and responses were serialized to/from JSON. 基本上是请求被提交/提交,并且响应被序列化到JSON或从JSON序列化。 It ran very smooth, once some kinks were worked out. 一旦解决了一些问题,它运行得非常顺利。 The original author had written a custom serializer to do direct results from their data layer directly... this leads to a lot of extra data going down the pipe. 原作者编写了一个自定义序列化程序来直接从他们的数据层直接得到结果......这导致了很多额外的数据流入管道。 As long as you are judicious with your payload data it can be very effective. 只要您了解有效载荷数据,它就会非常有效。

Some caveats though... 但有些警告......

  • You should probably avoid this if you expect users without javascript enabled to be able to access the site (anything involving money transactions from external users). 如果您希望没有启用JavaScript的用户能够访问该站点(任何涉及外部用户的金钱交易),您应该避免这种情况。
  • You will want to document your methodology as clearly as possible. 您需要尽可能清楚地记录您的方法。
  • Finding developers for maintenance tasks after you have implemented your application will be very difficult. 在实施应用程序后找到开发人员进行维护任务将非常困难。 (many server-side devs are shy of, scared of, or just plain inneffective with JS skills. (许多服务器端开发人员都害羞,害怕,或者只是对JS技能无效。

For the most part it's a toss up, I find that most people at least have JS enabled, but may have other things blocked off. 在大多数情况下,这是一个折腾,我发现大多数人至少启用了JS,但可能有其他东西被阻止。 AJAX/XmlHttpRequest supports is nearly universal at this point. 此时AJAX / XmlHttpRequest支持几乎是通用的。

As to templating for client-side display, there are a few options there (but that's a separate discussion). 至于客户端显示的模板,那里有一些选项(但这是一个单独的讨论)。

I started using the same approach: JavaScript for the user interface layer and PHP for the database access layer. 我开始使用相同的方法:用户界面层的JavaScript和数据库访问层的PHP。 I'm using AJAX to pass all the data back and forth between the 2 layers. 我正在使用AJAX在2层之间来回传递所有数据。 So far, AJAX has occasionally frozen on me, but it has been speedy enough most of the time. 到目前为止,AJAX偶尔会冻结我,但它大部分时间都足够快。 So I guess it'll work well enough. 所以我猜它会运作得很好。

(The result is that my code has gone from 90% PHP with 10% JavaScript...to 65% JavaScript with 35% PHP.) (结果是我的代码已从90%PHP,10%JavaScript ...到65%JavaScript,35%PHP。)

I've also separated the code for my page views from the code for my triggered event action functions. 我还将页面视图的代码与触发事件操作函数的代码分开。 So I like to think that I have an MVC arrangement now (even though I'm not using an off-the-shelf MVC framework like Backbone.js). 所以我觉得我现在有一个MVC安排(即使我没有使用像Backbone.js这样的现成的MVC框架)。

I'm not using HTML templates, though. 不过,我不是在使用HTML模板。 I don't think it feels natural to have 100% separation between HTML and programming. 我认为在HTML和编程之间实现100%的分离并不自然。 I think simple programming loops, conditional statements, and JavaScript triggers all go nicely with HTML. 我认为简单的编程循环,条件语句和JavaScript触发器都可以很好地与HTML结合使用。

Building JavaScript views works fine within the MVC pattern, since your view is not mixed in with your business logic or model. 构建JavaScript视图在MVC模式中工作正常,因为您的视图不会与业务逻辑或模型混合在一起。

However, there are a couple drawbacks to using full javascript views. 但是,使用完整的JavaScript视图有一些缺点。 Mainly it eliminates the ability for graceful degradation if the client has javascript turned off. 如果客户端关闭了javascript,它主要消除了优雅降级的能力。 Also, some browsers (IE) don't have a very fast javascript engine, which will make your page load more slowly. 此外,某些浏览器(IE)没有非常快速的JavaScript引擎,这会使您的页面加载速度变慢。 It is true that some of the view is separated between the client and the server, but it kind of makes sense when you think about it. 确实,某些视图在客户端和服务器之间是分开的,但是当你考虑它时它是有意义的。

In most cases the HTML that you send to clients is the same for everyone (unless you are doing browser detection on the server side). 在大多数情况下,发送给客户端的HTML对每个人都是相同的(除非您在服务器端进行浏览器检测)。 However the JavaScript routines are different. 但是JavaScript例程是不同的。 If you are using a library like JQuery, this will be hidden from you, but the code that is running on each client may differ greatly. 如果您使用的是像JQuery这样的库,这将对您隐藏,但每个客户端上运行的代码可能会有很大差异。 One example of this would be the XMLHttpRequest that is used by firefox/webkit etc browser and the active x control that is used by IE. 其中一个例子是firefox / webkit等浏览器使用的XMLHttpRequest和IE使用的活动x控件。 Since the html portion of the content is the same for everyone it makes sense to build on the server, and since the JavaScript portion of the view may differ, it make sense that it is built on the client side. 由于内容的html部分对于每个人都是相同的,因此在服务器上构建是有意义的,并且由于视图的JavaScript部分可能不同,因此它构建在客户端侧是有意义的。

HTH HTH

如果你认为它有主视图创建html / js引擎和几个ajax视图与数据流的方式 - 它将在MVC术语imo中很好。

Is there anything more dynamic going on on the site itself? 网站本身还有更多动态吗? Does it do more AJAXy things, dynamically refresh parts of the site etc.? 它是否做更多的AJAXy事情,动态刷新网站的部分等? If so, it may be reasonable to have a Javascript-only site. 如果是这样,拥有一个仅限Javascript的网站可能是合理的。

Since this is not how the web traditionally works though, sending HTML from the server is still the baseline. 由于这不是网络传统的工作方式,因此从服务器发送HTML仍然是基线。 If your pages are basically static, if you want to serve older clients, an audience that may have disabled Javascript, an audience that may have accessibility problems with Javascript-only pages, alternative clients that cannot understand Javascript or search engines, you should serve HTML pages from the server. 如果您的网页基本上是静态的,如果您想为旧客户提供服务,可能已禁用Javascript的受众群体,可能存在仅限Javascript页面的辅助功能问题的受众群体,无法理解Javascript或搜索引擎的替代客户端,则应提供HTML来自服务器的页面。 There's nothing wrong with it, it's straight forward, simple and foolproof. 它没有任何问题,它是直接,简单和万无一失的。 There are many things to consider when reinventing the wheel in Javascript on the client side. 在客户端重新发明Javascript时,需要考虑很多事情。 Unless you have a good use for the potential this offers (see for example the highly dynamic Facebook or Twitter pages), it may be more trouble than it's worth to your users. 除非您充分利用此优惠的潜力(例如参见高度动态的Facebook或Twitter页面),否则可能比您的用户更麻烦。

It sounds like you are one step away from going from the MVC pattern to the MVVM pattern. 听起来你离MVC模式到MVVM模式只有一步之遥。

MVVM is ideal for complex user interfaces (which is exactly what you would be creating with all the AJAX and JavaScript and whatnot) because in this case your HTML view will be able to act as the controller via JavaScript. MVVM非常适合复杂的用户界面(这正是您使用所有AJAX和JavaScript以及诸如此类的东西创建的),因为在这种情况下,您的HTML视图将能够通过JavaScript充当控制器。 There is a library (warning: I've never used it but it looks promising) for this called Knockout JS . 这个名为Knockout JS的库有一个库(警告:我从未使用它,但看起来很有希望)。

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

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