Showing posts with label Mixin. Show all posts
Showing posts with label Mixin. Show all posts

Friday, August 01, 2008

component mixins 说明

component mixins 是将一个特定的组件(一般位于mixins文件夹中,多数是跟js/ajax相关,用于控制组件行为)跟另一个普通组件(位于components文件夹下)集成在一起,形成一个具有二者全部功能的组件。
component mixins 也跟普通组件相似,可以有自己的参数parameters,当它跟普通组件components中定义的参数发生冲突时,以普通组件定义的参数为准。


components:
public class Area {
@Parameter(defaultPrefix = "literal", value = "300")
private String _height;

@Parameter(defaultPrefix = "literal", value = "300")
private String _width;

@Parameter(defaultPrefix = "literal", value = "px")
private String _unit;
}
mixin:
public class EventMixin
{
@Parameter(required = true, defaultPrefix = "literal")
private String event;

@Parameter(defaultPrefix = "literal", value = "pt")
private String _unit;

public String getEventName()
{
return event;
}
}
using in template:
<t:area t:mixins="eventmixin" t:height="200" t:event="click"/>

例子中mixin和component中都有一个叫unit的参数,以component的参数为准,area组件集成了eventmixin,由于其evnet参数是必须提供的,所以在模板中area组件中要提供此参数。

Thursday, June 28, 2007

Ruby's module mixin

# All you have to do is write an iterator called each, which returns the elements of our collection in turn. Mix in Enumerable, and suddenly your class supports things such as map, include?, and find_all?. If the objects in your collection implement meaningful ordering semantics using the <=> method, you’ll also get methods such as min, max, and sort.


class A
include Comparable
include Enumerable
attr :n

def initialize(n)
@n = n
end

def each
(1..@n).each do |i|
yield i
end
end

def <=>(b)
self.n <=> b.n
end
end

a = A.new(3)
b = A.new(4)

m = a.map {|i| i.to_s + __LINE__.to_s}

p m

n = a > b

p n