易栈网-膘叔

浏览模式: 标准 | 列表 | 图文
三八节快乐

三八节快乐

三八节看起来不象是我们男性过的节日,但事实上,在三八节,我真的要感谢很多人。。。
远的就不说了,以前那些老师啥的很多都是女性的。

就从身边吧,感谢我的母亲,感谢我的岳母,感谢我的妻子。这三个人是我最想表达感谢之意的。

母亲生我养我,这是毋庸置疑的,当然得感谢。

岳母和岳父每天辛苦的帮我们照顾小朋友,我父母又不在上海,岳父母承担的压力就大了,几乎是天天在照顾着小孩(几乎每个小孩都是自己爷爷奶奶或者外公外婆带大的吧。),我和妻子回家又晚,她们辛苦极了。

最后感谢我的妻子,除了因为爱情,还有小朋友啊。虽然我更喜欢小姑娘多一点,但小家伙也是个很可爱的人呢。

mysql.7z

Best MVC Practices

Best MVC Practices

这是一篇来自Yii官方的文章,写在了1.1.6的new fetures里面,值得一看,最起码,你在看完后,下次被人面试的时候,问到什么 是MVC,你可以有回答的东西了。

不多说,上原文,内容也不复杂,所以我就不翻译了(其实是我翻译不来,将就着看看可以,可是要翻译成人人都能看得懂的内容就难了)。。。

原文来自于:http://www.yiiframework.com/doc/guide/1.1/en/basics.best-practices,目前官方也确实没有中文版。

Although Model-View-Controller (MVC) is known by nearly every Web developer, how to properly use MVC in real application development still eludes many people. The central idea behind MVC is code reusability and separation of concerns. In this section, we describe some general guidelines on how to better follow MVC when developing a Yii application.

To better explain these guidelines, we assume a Web application consists of several sub-applications, such as

  • front end: a public-facing website for normal end users;
  • back end: a website that exposes administrative functionality for managing the application. This is usually restricted to administrative staff;
  • console: an application consisting of console commands to be run in a terminal window or as scheduled jobs to support the whole application;
  • Web API: providing interfaces to third parties for integrating with the application.

The sub-applications may be implemented in terms of modules, or as a Yii application that shares some code with other sub-applications.

1. Model

Models represent the underlying data structure of a Web application. Models are often shared among different sub-applications of a Web application. For example, a LoginForm model may be used by both the front end and the back end of an application; a News model may be used by the console commands, Web APIs, and the front/back end of an application. Therefore, models

  • should contain properties to represent specific data;

  • should contain business logic (e.g. validation rules) to ensure the represented data fulfills the design requirement;

  • may contain code for manipulating data. For example, a SearchForm model, besides representing the search input data, may contain a search method to implement the actual search.

Sometimes, following the last rule above may make a model very fat, containing too much code in a single class. It may also make the model hard to maintain if the code it contains serves different purposes. For example, a News model may contain a method named getLatestNews which is only used by the front end; it may also contain a method named getDeletedNews which is only used by the back end. This may be fine for an application of small to medium size. For large applications, the following strategy may be used to make models more maintainable:

  • Define a NewsBase model class which only contains code shared by different sub-applications (e.g. front end, back end);

  • In each sub-application, define a News model by extending from NewsBase. Place all of the code that is specific to the sub-application in this News model.

So, if we were to employ this strategy in our above example, we would add a News model in the front end application that contains only the getLatestNews method, and we would add another News model in the back end application, which contains only the getDeletedNews method.

In general, models should not contain logic that deals directly with end users. More specifically, models

  • should not use $_GET, $_POST, or other similar variables that are directly tied to the end-user request. Remember that a model may be used by a totally different sub-application (e.g. unit test, Web API) that may not use these variables to represent user requests. These variables pertaining to the user request should be handled by the Controller.

  • should avoid embedding HTML or other presentational code. Because presentational code varies according to end user requirements (e.g. front end and back end may show the detail of a news in completely different formats), it is better taken care of by views.

2. View

Views are responsible for presenting models in the format that end users desire. In general, views

  • should mainly contain presentational code, such as HTML, and simple PHP code to traverse, format and render data;

  • should avoid containing code that performs explicit DB queries. Such code is better placed in models.

  • should avoid direct access to $_GET, $_POST, or other similar variables that represent the end user request. This is the controller's job. The view should be focused on the display and layout of the data provided to it by the controller and/or model, but not attempting to access request variables or the database directly.

  • may access properties and methods of controllers and models directly. However, this should be done only for the purpose of presentation.

Views can be reused in different ways:

  • Layout: common presentational areas (e.g. page header, footer) can be put in a layout view.

  • Partial views: use partial views (views that are not decorated by layouts) to reuse fragments of presentational code. For example, we use _form.php partial view to render the model input form that is used in both model creation and updating pages.

  • Widgets: if a lot of logic is needed to present a partial view, the partial view can be turned into a widget whose class file is the best place to contain this logic. For widgets that generate a lot of HTML markup, it is best to use view files specific to the widget to contain the markup.

  • Helper classes: in views we often need some code snippets to do tiny tasks such as formatting data or generating HTML tags. Rather than placing this code directly into the view files, a better approach is to place all of these code snippets in a view helper class. Then, just use the helper class in your view files. Yii provides an example of this approach. Yii has a powerful CHtml helper class that can produce commonly used HTML code. Helper classes may be put in an autoloadable directory so that they can be used without explicit class inclusion.

3. Controller

Controllers are the glue that binds models, views and other components together into a runnable application. Controllers are responsible for dealing directly with end user requests. Therefore, controllers

  • may access $_GET, $_POST and other PHP variables that represent user requests;

  • may create model instances and manage their life cycles. For example, in a typical model update action, the controller may first create the model instance; then populate the model with the user input from $_POST; after saving the model successfully, the controller may redirect the user browser to the model detail page. Note that the actual implementation of saving a model should be located in the model instead of the controller.

  • should avoid containing embedded SQL statements, which are better kept in models.

  • should avoid containing any HTML or any other presentational markup. This is better kept in views.

In a well-designed MVC application, controllers are often very thin, containing probably only a few dozen lines of code; while models are very fat, containing most of the code responsible for representing and manipulating the data. This is because the data structure and business logic represented by models is typically very specific to the particular application, and needs to be heavily customized to meet the specific application requirements; while controller logic often follows a similar pattern across applications and therefore may well be simplified by the underlying framework or the base classes.

湿疹2。。。

湿疹2。。。

说起湿疹,实在是让我感觉很烦心,小朋友到现在还是有症状存在着,葯也配过也吃过只是觉得好象还没有恢复好。
这两天正在尝试吃中葯,还好,小朋友居然肯吃中葯,而且没有过多的抗拒,要知道中葯可是很苦的,虽然放了一点糖,但苦是必然的。吃过中葯的都清楚这一点,没吃过的,闻过那个味道也会清楚吧。
小朋友也知道要吃葯才会身体好,虽然吃起来苦一点,但还是肯吃的,好感动。也很伤心,只是现在也不知道他的过敏源到底是什么 ,以后估计还会吃苦头。
希望他早日恢复吧。

jQuery inViewport

jQuery inViewport

做前端的用户都会想过如何做lazyLoad,其实很多时候都有很多想法,比如占位图片,TEXTAREA后处理,根据滚动条的位置显示图片等。
介绍介绍上面的说法,因为这些名词是我自己想出来的。。。和一些标准的前端讲法可能是不一致的。
1、占位图片,这个用的比较多一点,即在页面还没有全部加载完的时候,涉及到图片的地方,先用占位图片(1X1大小的图片进行放大)占好,不影响整个DOM的加载,等全部加载完后,再慢慢加载图片。这样的方式优点在于,不影响DOM加载,代码修改量也最小,缺点是,其实还是加载了所有的图片。。。
2、teatarea后处理,这个,被用的比较明显的应该算是淘宝的trademark的页面上,当时在抢购活动的时候就用上了这个技巧,trademark这个页面全是图片,如果全部读出来并显示,很可能一下子就下载大约3M左右的图片,如果用第一种办法,那么占用的流量就非常夸张。于是淘宝当时的页面源码里就很明显的内容就是第一屏是正常的HTML代码,第二屏开始,把每一层的内容都放在textarea容器里。当滚动条滚动的时候,即当textarea开始出现在页面中时,就把textarea中的HTML代码转化成DOM,显示在页面上。它的优点是,textarea中的内容在初次加载时,不会被当成DOM被页面所加载。因此事实上首次加载的时候,只有第一屏的内容。缺点是需要写很多JS。(和第三点也有关)
3、根据滚动条的位置显示图片,这个其实是现在很多网站采用的技巧了,即滚动条滚动到哪里,图片就开始加载到哪里。和第二点其实有点象,只是处理的结构不一样而已。
介绍完这个,那就好好介绍一下第三点的技术了,原文从这里来:http://www.zhangjingwei.com/archives/jquery-inviewport/。

晚上在微博上“拔赤”写了个lazyload插件,发现其中用到一个很有意思的方法”div.inViewportRegion()”,字面意思就是在可视区域内。

在网上找了找,发现这是YUI提供的一个组件,觉得很有意思。
http://gillserver.com/yui/api/dom-region.js.html

如果jQ里面也包含类似的方法,可以做很多事,于是GG了一下,发现国外的同学写过这种选择器,代码表现上更直观。

代码如下:

(function($) {

$.belowthefold = function(element, settings) {
var fold = $(window).height() + $(window).scrollTop();
return fold < = $(element).offset().top - settings.threshold;
};

$.abovethetop = function(element, settings) {
var top = $(window).scrollTop();
return top >= $(element).offset().top + $(element).height() - settings.threshold;
};

$.rightofscreen = function(element, settings) {
var fold = $(window).width() + $(window).scrollLeft();
return fold < = $(element).offset().left - settings.threshold;
};

$.leftofscreen = function(element, settings) {
var left = $(window).scrollLeft();
return left >= $(element).offset().left + $(element).width() - settings.threshold;
};

$.inviewport = function(element, settings) {
return !$.rightofscreen(element, settings) && !$.leftofscreen(element, settings) && !$.belowthefold(element, settings) && !$.abovethetop(element, settings);
};

$.extend($.expr[':'], {
"below-the-fold": function(a, i, m) {
return $.belowthefold(a, {threshold : 0});
},
"above-the-top": function(a, i, m) {
return $.abovethetop(a, {threshold : 0});
},
"left-of-screen": function(a, i, m) {
return $.leftofscreen(a, {threshold : 0});
},
"right-of-screen": function(a, i, m) {
return $.rightofscreen(a, {threshold : 0});
},
"in-viewport": function(a, i, m) {
return $.inviewport(a, {threshold : 0});
}
});

})(jQuery);

来源:http://www.appelsiini.net/projects/viewport

简单测了下,不错,呵呵~

Demo:http://www.zhangjingwei.com/inviewport.zip

------EOF--
事后测试了一下,加载了这段的JS后,剩下对代码的处理就其实不多了,只需要BIND一个in-viewport就OK了,也算是比较方便。值得推荐,不知道啥时候类似功能可以直接进jQuery类库

杂谈吧

杂谈吧

或许很多人在看到我博客的上面微勃图片的时候肯定很吃惊:求财付通0.5元支付宝返还。

事实上,在发出这条消息的时候,虫少侠(http://www.enjoyphp.com/)就在财付通上给我了,当然,我立马从支付宝返还了,还的当然不止0.5元。

这是上周日我在测试财付通支付的时候因为没钱也不愿意绑定号码的时候遇到的问题,所幸还是有朋友愿意提供了钱,而不是让别人误会我在诈骗。。。

------------------------------------------------------------------

第二个就是http://codeany.com/,这是一个基于PHPRPC的内容分析的接口,用网站上的介绍来说就是:在Codeany在线接口的帮助下,引入站内检索,正文提取,热词提取和SEO优化,智能垃圾评论判定等服务,就跟在饮料机上接一杯饮料一样简单.立即可用,无需开发!

当然我不能这样见解,准备好好看看htmlSQL这个玩意,感觉这个东西有点象YQL,但又不太一样,毕竟YQL是属于在线版本而且调用是有次数限制的,htmlSQL已经比较老了,现在不更新了,用laoliu.me站长来说,如果和phpQuery配合使用那是相当的HIGH呀,准备抽空看看。

-----------------------------------------------------------------

又在看读取excel文件的class了,确实需要这样的工具,所幸现在已经成型的Class非常多,很方便就能解决问题。比如phpexcel,黑黑。当然也有非常小的工具,只有几十行,但照样能够读取,只是功能差一点而已。不过,对于我只需要读取sheet内容的人来说就非常适合了。

----------------------------------------------------------------

听说DZX2快出来了,内置整合QQ Connect,这让很多站长很兴奋认为这样是会简化用户注册会带来大量的流量。我是觉得这玩意没什么大用处。。。。

Javascript1.6数组新特性和jquery的几个工具方法

前两天看的文章里刚刚说ECMA 262中的一些String特性,又正好看到一篇博客介绍一些其他操作的实现,比如foreach,这个对于PHP用户来说就太常用了,而以前在javascript中只能用for(i in arr)来实现,虽然都没有问题,但毕竟看起来怪怪的。

上次的文章在这里:Javascript中各种trim的实现,这次的文章引用自Javascript1.6数组新特性和jquery的几个工具方法。

原文如下,其中会插入自己的内容,会特别标记

JavaScript 1.6 引入了几个新的Array 方法,具体的介绍见:New in JavaScript 1.6 。这些方法已经被写进了ECMA262 V5。现代浏览器(IE9/Firefox/Safari/Chrome/Opera)都已经支持,但IE6/7/8不支持。jquery的工具方法中提供了类似的功能。【膘叔:上次在讲trim的时候自己也测试过,确实IE8不支持,低于IE8的就更不用想了】

1、Array.forEach()和jquery的$().each()。在数组中的每个项上运行一个函数。类似java5 增强的for循环。

var ary = [2,4,6,8];

// js1.6 Array.forEach方法
ary.forEach(function(i){alert(i);});

// jquery的写法
$(ary).each(function(){alert(this);});
//还可以写成这样
$(ary).each(function(index,item){alert(item);});//index是元素的索引,item是该元素
//膘叔:这个相当于PHP中如下代码。。
while (list($key, $val) = each($fruit)) { echo "$key => $val\n"; }

2、Array.filter()和jquery的$.grep()。在数组中的每个项上运行一个函数,并将函数返回真值的项作为数组返回。简单的说就是用一个条件过滤掉不符合的数组元素,剩下的符合条件的元素组合成新的数组返回。

var ary = [2,4,6,8];

// js1.6 Array.filter()方法
var otherAry1 = ary.filter(function(item){return item>4;});
alert(otherAry1);//输出6,8

// jquery写法(注意和$.each的区别)
// 此处你们函数中第一个参数是数组元素自身,第二个参数是数组元素索引
// 而$().each方法刚好相反,作者应该统一下。
var otherAry2 = $.grep(ary,function(item,index){return item>4;});
alert(otherAry2);//输出6,8
//膘叔:PHP中有类似函数,比如array_filter,也是可以通过callback来处理

3、Array.map()和jquery的$.map()。在数组中的每个项上运行一个函数,并将全部结果作为数组返回。这个方法非常强大,尤其是作用于DOM数组时(在abcc项目上用过,对每个查询模块DOM生成查询字符串)。简单说就是把每个数组元素运算的结果作为新数组元素(还是很拗口)【膘叔:php中有array_map,array_walk等】

var ary = [2,4,6,8];

// js1.6 Array.map()方法
var newAry1 = ary.map(function(item){return item+1;});//每个元素加1
alert(newAry1);//输出3,5,7,9

// jquery写法
var newAry2 = $.map(ary,function(item,index){return item+1;});
alert(newAry2);//输出3,5,7,9

4、Array.every()方法。检查数组元素是否都符合某个条件,只要有一个不符合返回false,否则返回true

var ary = [2,4,6,8,10];

alert(ary.every(function(item){return item>1}));//true
alert(ary.every(function(item){return item>2}));//false

5、Array.some()方法。检查数组中元素是否符合某个条件,只要有一个符合返回true,否则返回false

var ary = [2,4,,6,8,10];

alert(ary.some(function(item){return item>9;}));//true
alert(ary.some(function(item){return item>10;}));//false

最后给出 IE6/7/8的解决方案,让这些浏览器完美支持JS1.6的Array新方法。

-function(){

function applyIf(o, c) {
if(o) {
for(var p in c) {
if(o[p]===undefined) {
o[p] = c[p];
}
}
}
return o;
}
applyIf(Array.prototype, {
indexOf : function(obj, idx) {
var from = idx == null ? 0 : (idx < 0 ? Math.max(0, arr.length + idx) : idx);
for(var i = from, l = this.length; i < l; i++) {
if(i in this && this[i] === obj) {
return i;
}
}
return -1;
},
lastIndexOf : function(obj, idx) {
var len = this.length, from = idx == null ? len - 1 : idx;
if(from < 0) {
from = Math.max(0, len + from);
}
for(var i = from; i >= 0; i--) {
if (i in this && this[i] === obj) {
return i;
}
}
return -1;
},
every : function(fn, thisObj) {
var l = this.length;
for(var i = 0; i < l; i++) {
if(i in this && !fn.call(thisObj, this[i], i, this)) {
return false;
}
}
return true;
},
some : function(fn, thisObj) {
var l = this.length;
for(var i = 0; i < l; i++) {
if(i in this && fn.call(thisObj, this[i], i, this)) {
return true;
}
}
return false;
},
filter : function(fn, thisObj) {
var l = this.length, res = [], resLength = 0;
for(var i = 0; i < l; i++) {
if(i in this) {
var val = this[i];
if(fn.call(thisObj, val, i, this)) {
res[resLength++] = val;
}
}
}
return res;
},
map : function(fn, thisObj) {
var l = this.length, res = [];
for(var i = 0; i < l; i++) {
if(i in this) {
res[i] = fn.call(thisObj, this[i], i, this);
}
}
return res;
},
forEach : function(fn, thisObj) {
var l = this.length;
for(var i = 0; i < l; i++) {
if(i in this) {
fn.call(thisObj, this[i], i, this);
}
}
}
});
}();

------EOF----

最后这一段有事没事可以参考一下哦,毕竟这些都是可以拿来做参考的,比如象最后一个foreach,可以好好学学这些代码哦。

最后,var evens = [i for each (i in numbers) if (i % 2 == 0)];,看到这个,有没有感觉很象python的写法???这个是javascript 1.7中的写法了,看看这里的介绍:https://developer.mozilla.org/en/JavaScript/Guide/Predefined_Core_Objects#Working_with_Array-like_objects

表弟结婚

表弟结婚

时间果然是无情的,过去了就永远过去了。表弟比我小六岁,如今也成家了。祝福他永远幸福。愿天下有情人都不是同父异母的兄妹,还是终成眷属吧

自 WAP 发表
alipay和tenpay等的接口操作记录学习

alipay和tenpay等的接口操作记录学习

昨天加班折腾了一下商铺的支付流程,也算是测试了这些接口,不过还是以alipay和tenpay接口居多(即支付宝和财务通接口)

说实话,财务通的接口有点乱,但有一点好处,每个接口的名称都不一样,不同的接口可以很容易认出来,而支付宝就不一样,名称是一样的,但代码和结构不太一样。累了一点。

不过,细分起来这些接口无非就是那几样东西

1、参数,这些都是固定的,有顺序,所以,其实是很方便的,参考一下文档即可。然后做一下MD5进行sign,这些是用来做数据校验的。(有一些接口是对GET参数按照key进行sort而处理的,这种最方便,直接ksort就OK了,如果不是就更方便按顺序录入参数就可以了。)

2、几个固定的URL,接口的URL,notify_url,return_url,了解了这些东西之后就好处理了。

无非就是这些东西,因为都是转向到支付页面进行处理的,所以也不用过多的担心,根据接口来就行了,当然为了要在支付宝或者财务通里看到订单的详细内容,需要那些Desc里写详细,当然你自己平台上的订单号也要传递过去,否则日后无法对帐。

由于我测试 的都是即时到账接口,所以没有什么特别多的经验可以谈,只是我自己了解后做的小记录而已。

Javascript中各种trim的实现

Javascript中各种trim的实现

说到trim,其实这真的让无数前端郁闷。比如在处理input框里内容的时候,都会需要处理input内容的左右空格。但让人郁闷的是,String里居然没有原生方法,而每个人的实现方法都会不一样,效率也各有不同。

但是,新版的ECMA-262里已经表示有此方法了:

15.5.4.20 String.prototype.trim ( )

The following steps are taken:

1. Call CheckObjectCoercible passing the this value as its argument.
2. Let S be the result of calling ToString, giving it the this value as its argument.
3. Let T be a String value that is a copy of S with both leading and trailing white space removed. The definition
of white space is the union of WhiteSpace and LineTerminator .
4. Return T.

NOTE The trim function is intentionally generic; it does not require that its this value be a String object. Therefore, it
can be transferred to other kinds of objects for use as a method.

本文摘自http://www.cnblogs.com/snandy/archive/2011/02/26/1965866.html,只是经过我自己的整理了啦。

第一种:这种是大多数人都会写的,也是流传最多的代码了吧?

String.prototype.trim = function() {
//return this.replace(/[(^\s+)(\s+$)]/g,"");//會把字符串中間的空白符也去掉
//return this.replace(/^\s+|\s+$/g,""); //
return this.replace(/^\s+/g,"").replace(/\s+$/g,"");
}

第二种:来自motools:

function trim(str){
return str.replace(/^(\s|\xA0)+|(\s|\xA0)+$/g, '');
}

第三种:这是jQuery的,jquery的方法类似于第一种:

function trim(str){
return str.replace(/^(\s|\u00A0)+/,'').replace(/(\s|\u00A0)+$/,'');
}

第四种是来自所摘博客中最写的:Steven Levithan 在进行性能测试后提出了在JS中执行速度最快的裁剪字符串方式,在处理长字符串时性能较好:

function trim(str){
str = str.replace(/^(\s|\u00A0)+/,'');
for(var i=str.length-1; i>=0; i--){
if(/\S/.test(str.charAt(i))){
str = str.substring(0, i+1);
break;
}
}
return str;
}

博客中还写了这么一点,那就是Molliza Gecko 1.9.1引擎中还给String添加了trimLeft ,trimRight 方法。

这让我想起了PHP的代码,比如ltrim,rtrim,trim之类的

杂记

杂记

一点小记录吧
第一:jQuery 升级到1.5.1了,前两天在群里的时候还有人在说,1.5不支持IE9,因为在使用的时候居然出错了。报错信息居然是document.getElementsByTagName,但是直接写这样的document.getE....是正常的。所以,这明显就是1.5的BUG。

当然这里不是说这个,这里是一篇翻译,http://www.cnblogs.com/sanshi/archive/2011/02/25/1964441.html。我觉得主要内容是这一段:

“jQuery 目前把 IE9作为一个重要的浏览器来支持。所有已知的BUG都已经得到修复或者已经提交给IE的开发团队以便在最终版本中得到解决。(All known bugs have been fixed and/or been reported to the IE team for resolution in the final release.)

三个新的参数被添加到jQuery.ajax():

isLocal: 允许当前的执行环境被识别为“local”(例如文件系统),jQuery默认情况下是不会识别这种环境。如下的协议被识别为本地环境:file, *-entension, widget. 如果需要修改isLocal,推荐使用$.ajaxSetup()方法来修改。

mimeType: 可以用来覆盖XHR的mime type.

xhrFields: 一个可以添加到原生XHR对象上的键值对(fieldName-fieldValue)。比如,你可以在跨域调用中设置withCredentials为true。”

第二:第二件嘛就是果粉比较开心的事了,那就是前两天刚刚苹果刚刚更新了商店里商品的信息,所有的mac pro现在都更新CPU为i5了,而且价格没变。这实在太让人激动开心了。黑黑,看来我去年没有买成mac pro也是属于塞翁失马焉知非福吧。

第三:yahoo的邮箱更新了。其实这并不算是什么 新闻吧。不过自从我用yahoo的邮箱以来,这是第三次更新了。虽然每一次打开邮箱都感觉比较慢(应该是邮箱服务器在国外的关系吧),但是我想说的是用户体验。用过很多WEB邮箱,几乎到现在都没有更新过,几年前什么 界面现在还是。。。当然QQ邮箱变化是较大的,以前最喜欢用网易的邮箱,SINA以前是最烂的,现在好很多。139邮箱原来也不行,现在也好很多了。gmail的变化也比较小,只是一些小功能的更新,只是这些小功能的更新会让用户使用起来更流畅。

第四:Google JavaScript Style Guide。这个嘛,啥时候有空的时候翻译翻译吧。还是算比较好的,虽然只是google的js style,但是对我们开发人员来说也可以学习学习。原文在这里http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml?showone=Tips_and_Tricks