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

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

editplus 支持svn了

刚才在看sae的文档时,发现他在介绍如何部署代码时提到了editplus,并说,editplus可以用TSVN来部署文件。
这时候我觉得纳闷,什么时候editplus支持SVN了?
然后找到了editplus的官网,找到了what's new:

原来,从3.30开始就支持了。
不过TSVN必须要先安装小乌龟才OK,HOHO,有兴趣的朋友是可以尝试了。
反正我现在用PHPstorm,什么都带了。
自从用了phpstorm,腰不酸腿不疼了。

Tags: edit, svn, sae

OS X Tiger: Enable/Disable Spotlight

很幸运,看完这篇文章后,我根据文中的内容,终于恢复了spotlight,所以我现在对邮件什么的也能够搜索了。
OK,这篇文章在这里:http://www.technipages.com/os-x-leopard-enabledisable-spotlight.html
嗯,其实我当时是认为spotlight没什么用啊。但真没想到,原来spotlight接管了这么多功能。NND,我还仅仅以为spotlight是类似第三方插件似以的。没文化啊,真是太可怕了。。。。
上原文:

You may want to turn off Spotlight to increase system performance or to prevent it from indexing personal data. Here’s how to do it under Mac OS X.

Launch Terminal from the Utilites folder.

To disable Spotlight, type the following and press Return:

% sudo launchctl unload /System/Library/LaunchDaemons/com.apple.metadata.mds.plist

To disable it permanently, type this instead:
% sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist

To enable Spotlight type the following and press Return:
% sudo launchctl load /System/Library/LaunchDaemons/com.apple.metadata.mds.plist

To enable it permanently, type this instead:
% sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist

For instructions on how to do this in Mac OS X Leopard, see How to completely disable Spotlight.

---------
HOHO又能够搜索邮件是一件很开心的事情。开始怀念windows下面的搜索了。
很多朋友说你不会直接用find啊。是,如果我搜索文件,用find当然无所谓,可是邮件没办法啊。。

Tags: spotlight

转:Tiger: Disabling Spotlight

嗯,到现在为止,我也没有办法搜索我的电脑、mail、甚至 spotlight,真是纠结。
于是这篇disabing spotlight就成了我搜索中的目标。我想,既然能够disable spotlight,那肯定能够enable吧?
所以,上原文吧:http://www.theconsultant.net/2005/06/tiger-disabling-spotlight/

Spotlight introduces a fairely large performance hit on to the system, especially if the files you are working with are both large and have the Spotlight plugin, and thus can be indexed. Performance hit might be less noticable on the desktop system with fast drives, however on my laptop with 4200 rpm drive, and constantly dealing with megabytes of source code and compilations spotlight introduced less of a benefit and more of a hindrance.

So, without further ado, in order to disable spotlight, one has to edit /private/etc/hostconfig, find the line that reads SPOTLIGHT=-YES-, change it to SPOTLIGHT=-NO-, and rebooot.

This will prevent MetaData Service, / System / Library / Frameworks / CoreServices.framework / Versions / A / Frameworks / Metadata.framework / Versions / A / Support / mds from starting on boot time.

Note that this will not disable file change notifications in the kernel, as can be checked using Amit Singh’s fslogger. On the same page there is some more in depth information on the kernel notification service that Spotlight (and fslogger) subscribe to.

A perty GUI called Spotless was written by someone, but I am not sure I’d trust a GUI to parse and edit a text file.

If you want to get rid of the looking glass icon in the top right hand corner as well, you might want to either remove (perferably just move out of place) or chmod -R 0000 /System/Library/CoreServices/Search.bundle (Key file. Actual parts of Spotlight are: /Library/Spotlight /System/Library/Spotlight /System/Library/CoreServices/Search.bundle /System/Library/PreferencePanes/Spotlight.prefPane /System/Library/Services/Spotlight.service /System/Library/Contextual Menu Items/SpotlightCM.plugin /System/Library/StartupItems/Metadata plus /usr/bin/md*, although I’d argue that metadata tools in /usr/bin/md* are actually useful.)
Changing permissions means that if at some point you want to undo the changes, you can always repair permissions. In any case, little looking glass in the corner doesn’t bother me much.

Technically one can probably selectively start and stop Spotlight by killing or startng mds and mdimport, however a way Apple recommends is using mdutil -i off / to turn off indexing of the boot volume (ie existing databases would be preserved and accessible through spotlight).

If you ever want to blow away your Spotlight database, and force reindexing (assuming mds/mdimport run), you can do mdutil -i off /, mdutil -E / , mdutil -i on /

Note: Apprently killing spotlight interferes with find in Finder and in Mail.app. As I never use either (locate or find . -name “*foo*” -print on the command line is much more powerful, plus gives me an -exec stuff {} \; option), it doesn’t bother me, however ocdinsomniac has some nice additional information and a script that purports reverting Finder’s find to the Panther style behavior.

Tags: spotlight

纠结:mac居然不能使用搜索功能了

不知道从什么时候起,我的macosx系统居然不能搜索了。
Finder中无法找文件
邮件中无法搜索邮件(只能根据发件人的详细地址来筛选)
Spotlight中也无法搜索了。

真纠结啊,不知道是哪里的设置出了问题。NND,望知情者给条生路啊

Tags: macosx, 搜索

Records:43123456789