手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表Tag:linux

Yii 官方关于event和behaviors的文章

本文内容来自官方的wiki,一来是做备份,二来自己查询起来也会快一点,毕竟yiiframework官网经常抽风,当然它的抽风不是因为自己,而是因为GFW,说来也可怜,好象很多技术网站都被墙了。
不过想想也正常,如果一个搞技术的,连翻墙也不会,还搞个毛技术啊。
OK,上正菜:http://www.yiiframework.com/wiki/44/behaviors-events/#hh1

These features provide endless possibilities and unbelievable flexibility, but as current documentation does not give more than a few examples, it might be difficult to fully understand their internals and requirements.

It should be noted that they do mostly the same thing. You can attach behaviors and event handlers to components to modify the components' behavior.

Events

It is useful when you want to interrupt the normal application flow without extending base classes.

For example, enabling gzip compression on the output could be done via extending CWebApplication. But because there are entry points for event handlers, one can do this:

PHP代码
  1. Yii::app()->onbeginRequest = create_function('$event''return ob_start("ob_gzhandler");'),  
  2. Yii::app()->onendRequest = create_function('$event''return ob_end_flush();'),  

You can create an event handler -- which is simply a method in some class with a specific signature -- and attach it to the event of an object. You can add as many event handlers as you wish, from as many objects as you wish. If the event handler is, effectively static, then you can create the object as you assign it:

PHP代码
  1. $test_comp->onSomethingGoesOn = array(new SomeClass, 'eventHandler1');  
  2. $test_comp->onSomethingGoesOn = array(new SomeOtherClass, 'eventHandler2');  
  3. $test_comp->onSomethingGoesOn = array(new YetAnotherClass, 'eventHandler3');  

As long as you have a handle on the object, then you can add an event handler to it.

At some point, you can then raise the event with something like one of these:

PHP代码
  1. $test_comp->onSomethingGoesOn(new CEvent($this));  
  2. $test_comp->onSomethingGoesOn(new CEvent());  

So, basically, it allows you to build a list of function calls that can later be executed, in the order they were added. It can save you passing around a lot of object refs and building conditional code, since you can still raise the event, even if it doesn't do anything.

Behaviors

Behaviors are simply a way of adding methods to an object.

Take this scenario: You have 2 classes: MySuperClass1, MySuperClass2. There might be lots of methods from MySuperClass1 & 2 that you want in some new class, say MyBoringClass. Unfortunately, php does not allow for this:

PHP代码
  1. class MyBoringClass extends MySuperClass1, MySuperClass2 {  
  2. }  

This is where behaviors come in. Instead, you can go:

PHP代码
  1. class MyBoringClass extends MySuperClass1 {  
  2. }  
  3.    
  4. $classInstance = new MyBoringClass();  
  5. $classInstance->attachbehavior('uniqueName'new MySuperClass2);  

Now $classInstance has all the methods from MySuperClass1 and MySuperClass2. Since MySuperClass2 is being used as a behavior, it has to extend CBehavior. The only caveat to this is an attached behavior cannot override any class methods of the component it is being attached to. If a method already exists, if it be from the original class or already added by a previously attached behavior, it will not be overwritten.

In an OO language like Ruby, it's quite possible to start with a completely empty object and simply build its behavior as you go along. Yii provides this behavior with a little magic. The key is that the class you wish to add the behavior from must extend Cbehavior.

PHP代码
  1. class SomeClass extends CBehavior  
  2. {  
  3.     public function add($x$y) { return $x + $y; }  
  4. }  

Then use with:

PHP代码
  1. $test_comp = new TestComponent();   
  2. $test_comp->attachbehavior('blah'new SomeClass);  
  3. $test_comp->add(2, 5);  

So, in this case, you are extending the functionality of an object with functionality of another object.

After studying this cookbook page you are encouraged to reread the corresponding guide page as it contains advanced information (for example, if you are familiar with interfaces, you might find it enough to implement IBehavior before extending CBehavior).

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

behavior和Event在model里用的会相对较多一点,不过,其他地方也可以一用。如果你真的不明白这一块的逻辑,那还是用其他方式替代吧。

 

 

 

 

 

 

 

 

Tags: yii, behavior, event

收藏的关于JS的一些文章

这是一些链接,纯收藏,其实我都用firefox的read it later保存下来过,只是我在这里做一点分享罢了。。。

好了。。先贴这么多。。。

 

 

Tags: 收藏, js

随谈

这两天在为neatpic把原来的上传改成使用yUI的那个上传组件,也在准备把上传后的图片统一扔在一个目录下,这样一是为了安全,而是上传后也不用担心目录是否有可执行的问题。然后针对图片进行简单的管理,将它们可以移动到指定的目录下面。。

春节走走亲戚其实也挺痛苦,酒要喝,还得和原来不是很熟悉的人聊天。。。。

不过,这是常事,只是让我觉得郁闷的事,我竟然忘了在初一给老爸老妈打电话,TMD,骂自己了。。。。

表弟要结婚了,这也是一个重要的事情。表弟比我小6岁,结婚啥的却没有比我晚,也是一件好事。而且表弟可能会到上海来工作,好事情呢。。

刚两天,问题还好不是特别多。。。

115快盘

有朋友在sinaapp上开了个项目,是快盘提取文件,支持任意后缀名,因为这样就可以方便用户转贴到任意勃客或者论坛之类的了

所以我也就写了一个类似的。。还没有做rewrite。。

可以尝试一下:

http://labs.neatstudio.com/115.php?code=t99f5a9181&type=

这个type可以是tel,cnc,union,udan,对应了电信,网通,联通,优蛋(优蛋只能生成链接并帮你触发点击)

如果网上啥数据也没有,那就只能生成U蛋链接喽。

想法创意来自:http://kill.sinaapp.com/

 

Tags: 115

vmware 7上5分钟装最新的macos x snow leopard 10.6.3

我一直想要苹果,但是,家里的财政大权不是我能管的。又想买一个黑苹果吧,但财政大臣那边还是不同意,认为买一个假玩意没意思。

虽然我一直想搞台笔记本,但目前好象还没有准备好,上级领导不批啊,所以看到素包子这篇文章就鸡动了一下。。。

素包子找了一些资料,大多不是介绍的最新的vmware7装最新的macos x,搜到了一个不错的安装介绍,参考着做完全没问题,而且很简单,感谢作者刘洪志和电脑报。5分钟搞定配置过程,接下来就是等待自动安装和重启了。

貌似可以升级到10.6.4,做了个snapshot,更新去了,一共700M左右。

(其中使用darwin.iso启动进行安装的步骤可以省略,可以直接使用OSXPCBETA_ArcticFox.10.6.3.iso启动安装。)

想偷懒并且对版本要求不高的同学可以下premade的vmware镜像,直接使用,大小10G,见BT种子,用迅雷离线下载瞬间完成,满水管下载。

Mac OS X 10.6.2

原文在:http://baoz.net/install-macos-x-snow-leopard-10-6-3-in-vmware-7/

好象主要是那两张图:

大小: 354.05 K
尺寸: 288 x 376
浏览: 1959 次
点击打开新窗口浏览全图

大小: 308.41 K
尺寸: 288 x 376
浏览: 1806 次
点击打开新窗口浏览全图

Tags: apple, macos, vmware

Records:351234567