2016年4月17日 星期日

ActiveRecord::Observer


callback, but not very related to model
we can use observer

class AuditObserver < ActiveRecord::Observer
  observe :account:balance

  def after_update(record)
    AuditTrail.new(record"UPDATED")
  end
end


Storing Observers in Rails

If you’re using Active Record within Rails, observer classes are usually stored in app/models with the naming convention of app/models/audit_observer.rb.

Configuration

In order to activate an observer, list it in the config.active_record.observers configuration setting in yourconfig/application.rb file.
config.active_record.observers = :comment_observer, :signup_observer

Observers will not be invoked unless you define these in your application configuration. 

2016年4月16日 星期六

how to build a virtual dom tree

如何實現一個Virtual DOM算法

  1. use javascript object to present the structure of dom, and use it to build a real dom tree, then insert to document
  2. when state updated, recreate a object tree, and then compare old tree and new tree, and record the diff
  3. use 2.) diff to 1.) real dom tree

2.1. how to compare:
1.Depth-first search for new and old tree, and add unique mark
2.diff type: REPLACE, REORDER, PROPS, TEXT

3.compare list: ex => abcdefghi =>abchdfgij 

2016年4月10日 星期日

css 原理

 http://kb.cnblogs.com/page/125663/


from right to left effect:
DIV#divBox p span.red{color:red;} => find class ‘red’, then find span, then find p ….
in order to filter some unrelated 元素 quickly

how to optimize css

  1. 不要在ID选择器前使用标签名
    一般写法:DIV#divBox

      更好写法:#divBox
      解释:因为ID选择器是唯一的,加上div反而增加不必要的 CSS 匹配。
  2. 不要在 class 选择器前使用标签名
    一般写法:span.red
    更好写法:.red
    解释:同第一条,但如果你定义了多个.red,而且在不同的元素下是样式不一样,则不能去掉,比如你css文件中定义如下:

      p.red{color:red;}
      span.red{color:#ff00ff}
    
    
      如果是这样定义的就不要去掉,去掉后就会混淆,不过建议最好不要这样写
  3. 尽量少使用层级关系
    一般写法:#divBoxp.red{color:red;}
    更好写法:.red{..}
  4. 使用 class 代替层级关系
    一般写法:#divBox ul li a{display:block;}
    更好写法:.block{display:block;}
  5. 在 CSS 渲染效率中 id 和 class 的效率是基本相当的
    class 会在第一次载入中被缓存,在层叠中会有更加好的效果,在根部元素采用id会具有更加好(id有微妙的速度优势)。

how browser work








ruby object


  • ruby中,一切皆對象
  • 理解ruby對像模型
  • 了解ruby查找方法的方式

描述了Ruby中方法調用的過程,“向左一步進入該對象的類,然後沿著祖先鏈一直查找方法,找到方法之後,根據自身的綁定執行該方法”。 因此,對象本身只有一組綁定,而方法定義都是在類中。那麼上面說到的單件方法和類宏應該在什麼地方定義呢?單件方法肯定不能定義在類中,否則將會影響該類的所有實例對象。類本身也是對象,類的方法不能定義在自身,因為對象的方法必須定義在對象的類中,而類對象的類是Class,如果把類方法定義到Class上,那麼所有的類對像都會擁有該方法。這一切迷思的答案都來源於一個Ruby中的高級概念,Eigenclass Eigenclass在Ruby中,當調用obj.class向一個對象索要它的類的時候,編譯器並沒有告訴大家全部的真相,你得到的類並不是你看到的類,你得到的是一個對象特有的隱藏類,這就是該對象的Eigenclass,雖然Object#class方法想把關於Eigenclass的信息隱藏起來,但是,存在即存在,總會被人挖出來的。 



Eigenclass是一個類,但是是一個很特殊的類,它只能有一個實例,且不能被繼承,但是其自身可以繼承其它類。因此,所有的對像都有一個自己的Eigenclass,單件方法就定義在該對象的Eigenclass中,類宏定義在該類對象的Eigenclass中

為了區分普通類和Eigenclass,Ruby會使用“#"表明該類是一個Eigenclass。 
*一個實例對象的Eigenclass的父類是該對象的類 
*一個類對象的Eigenclass的父類是該類對象的父類的EigenClass。 


singleton_method

singleton_method means object’s specific method

when we define method on specific object, will insert the singleton class between object and his class.
  1. puts example2. class  
  2.   
  3. class  << example2  
  4.   puts  self  
  5. end  

輸出結果:
  1. ExampleClass  
  2. #<Class:#<ExampleClass:0x28305d>>  


And if we add class method on class just like add singleton method on class 

instance_eval 與class_eval

instance_eval’s receiver must be an instance. and if an instance call instance_eval => mean you can define this instance’s singleton_method.
and because class is a instance of Class => so class also can call instance_eval => define this class’s singleton_method => class method

class_eval’s receiver must be an class. and if class call class_eval => you can define this class’s instance_method

instance_eval must be call by instance, use to define singleton_methods

class_eval must be call by class, use to define instance_methods