Quantcast
Channel: Terrysco's Blog » Book Remarks
Viewing all articles
Browse latest Browse all 9

jQuery, working with PHP

0
0

5年前,我刚刚学习PHP这们编程语言的时候,我就放弃了学习很多前端技术,比如javascript和css。总是把所有精力放在服务器端的配置,开发和数据库的设计以及优化上。五年时间过去后,唯一的收获是对服务器端技术比较熟悉,但是开发出来的应用总是感觉缺少点什么。再后来发现了前端技术的重要性,因为前端技术的可优化余地要远远大于后端。于是恶补了一大堆js和css的优化和浏览器的工作行为理论。但越来越发觉自己做出来的应用“难以使用”了,我相信很多服务端技术开发的朋友跟我有同样的感觉。

首先,由于我们缺乏对javascript的熟练编写,导致一个应用很多时候都是借助服务器端完成(以下都以PHP为例),也就是说用户的浏览器只是在GET和POST数据,然后渲染HTML,很少用客户端来进行计算和交互,最多只是用来检测下表单的合法性。记得四年前开发一个CMS的时候,用javascript写了大量的代码,至今想起来犹如梦魇。那时对javascript很不了解,用PHP的思维去使用它,造成代码冗长,难以维护,加上自己调试能力不好,每次做bug修复的时候都想告别这个行业。08年初,在使用Drupal这个CMS的时候,意外看到了jQuery这个框架,是内置在Drupal中的,默认自动加载。为了编写好很多ajax应用,啃了几天jquery cookbook,算是入门了,后来也闲置了。最多就是写一些简单的ajax应用和前台效果。前几天看了一本书《jQuery with PHP》,09年10月份出版的,面向的主要读者就是做服务端开发,对前端知识比较少了解的人。200多页比较精简,语言通俗易懂,看完后收获颇多,从书中摘取一些比较有意思的东西分享给大家。(附上个人翻译,有误的地方欢迎拍转)我想这本书是对一个PHP工程师来说,平衡前后端能力,提高产品的用户体验,节省网络带宽,提高应用性能的重要一课。

jquery-with-php

It would be silly if a 3D screen was generated on the server and was sent to the client
24 times per second. And it would be awkward if you tried to do something like
walk through a wall, and had to wait for the server to decide if it was allowed or not.
However, in a pure PHP environment, that’s exactly what has been happening—the
server generates absolutely everything that the client displays. If an element needs
to be removed from a table, for example, then the server needs to be told, so that it
can give the client the new view. If you try to do something illegal, such as submit a
blank form where an email address is required, then pure PHP applications will let
you do that, wasting time and server resources.
However, using JavaScript, and especially using Ajax, the client side is no longer a
“dumb terminal” for the server. We can write applications fully on the client side, if
we wish, using the server purely as a database. This book will demonstrate ways to
work towards that.

It would be silly if a 3D screen was generated on the server and was sent to the client 24 times per second. And it would be awkward if you tried to do something like walk through a wall, and had to wait for the server to decide if it was allowed or not.

(这里用网游来比喻前端和后端的关系,很多程序员都玩网游吧!)

如果3D效果的显示数据是在服务端生成后以每秒24次的频率发送给客户端,可想这种设计有多傻。同样,如果你试着做一些很常见的操作,比如准备穿越一道墙,如果这个时候还要等待服务器来判断是否允许的话同样很傻,客户端做这个事情是最合适的。

However, in a pure PHP environment, that’s exactly what has been happening—the server generates absolutely everything that the client displays. If an element needs to be removed from a table, for example, then the server needs to be told, so that it can give the client the new view. If you try to do something illegal, such as submit a blank form where an email address is required, then pure PHP applications will let you do that, wasting time and server resources.

然而,在单独的PHP环境中,服务端产生几乎所有客户端要显示的东西。比如要从一个HTML表格中删除一个元素,必须通知服务器,这样服务端才能生成新的页面发送给客户端。如果你做了一些非法操作,比如没有填写重要的邮件地址就提交了一个空的表单,只有提交后PHP才能知道,这样大大浪费了时间和宝贵的服务器资源。

However, using JavaScript, and especially using Ajax, the client side is no longer a ”dumb terminal” for the server. We can write applications fully on the client side, if we wish, using the server purely as a database. This book will demonstrate ways to work towards that.

然而,使用Javascript,特别是Ajax,对服务器来说客户端不再是一个“哑终端”。我们完全可以在客户端编写一些应用程序,如果我们愿意,服务端完全可以作为一个数据库来使用。

Viewing all articles
Browse latest Browse all 9

Latest Images

Trending Articles





Latest Images