grootJs 属性过滤器
index10.html
<html>
<head>
<title>属性过滤器</title>
<script src="jquery-1.11.2.min.js"></script>
<script src="groot.js"></script>
</head>
<body>
<div gt-view="myview">
<span gt-text="_.d({time},'yyyy-MM-dd')"></span>
</div>
</body>
</html>
<script>
var view = groot.view("myview", function (vm, ve) {
vm.time = "2013/10/20";
});
</script>
属性过滤器定义
groot.filter(
{
"d": function (value, format) {
if (!value) return;
if (!format) format = "yyyy-MM-dd";
switch (typeof value) {
case "string":
value = new Date(value.replace(/-/, "/"));
break;
case "number":
value = new Date(value);
break;
}
if (!value instanceof Date) return;
var dict = {
"yyyy": value.getFullYear(),
"M": value.getMonth() + 1,
"d": value.getDate(),
"H": value.getHours(),
"m": value.getMinutes(),
"s": value.getSeconds(),
"MM": ("" + (value.getMonth() + 101)).substr(1),
"dd": ("" + (value.getDate() + 100)).substr(1),
"HH": ("" + (value.getHours() + 100)).substr(1),
"mm": ("" + (value.getMinutes() + 100)).substr(1),
"ss": ("" + (value.getSeconds() + 100)).substr(1)
};
return format.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g, function () {
return dict[arguments[0]];
});
}
}
)
上面是内置的时间过滤器的定义;groot.filter 为属性过滤器扩展接口