1
(function()
{
2
var rRoute, rFormat;
3
$.route = function(obj, path)
{
4
obj = obj ||
{};
5
var m;
6
(rRoute || (rRoute = /([\d\w_]+)/g)).lastIndex = 0;
7
while ((m = rRoute.exec(path)) !== null)
{
8
obj = obj[m[0]];
9
if (obj == undefined)
{
10
break
11
}
12
}
13
return obj
14
};
15
$.format = function()
{
16
var args = $.makeArray(arguments), str = String(args.shift() || ""), ar = [], first = args[0];
17
args = $.isPlainObject(first) ? args : $.isArray(first) ? first : [args];
18
$.each(args, function(i, o)
{
19
ar.push(str.replace(rFormat || (rFormat = /\
{([\d\w\.]+)\}/g), function(m, n, v)
{
20
v = n === 'INDEX' ? i : n.indexOf(".") < 0 ? o[n] : $.route(o, n);
21
return v === undefined ? m : ($.isFunction(v) ? v(n) : v)
22
}));
23
});
24
return ar.join('');
25
};
26
})();
posted on 2011-10-24 12:32
梅雪香 阅读(385)
评论(0) 编辑 收藏