Javascript 在指定物件為變數時是傳「reference」,若不知道這個特性很容易搞不清楚你的值為啥被改掉了,jQuery的extend函式可以做deep Clone,也就是遞迴將物件裡頭的任一成員都做clone,但針對array好像有點問題,因此我寫了一支小function來呼叫,以後要做cloneObject時可以直接服用。
/*
* cloneObject 會完整clone一個全新的Object
* 若傳入的object是array, 則會回傳array
*/
(function($) {
$.extend($, {
cloneObject : function(obj){
var newObj;
if($.isArray(obj)){
newObj = [];
for(var i=0; i<obj.length; i++){
newObj.push(arguments.callee(obj[i]));
}
return newObj;
}else{
newObj = {};
return $.extend(true, newObj, obj);
}
}
});
})(jQuery)
Closure Template 是google release 的工具Closure Tools其中之一,我會注意到這個工具是因為前端產生動態layout的需求愈來愈多,雖然已經很盡力要將邏輯與template分開,但前端跟layout的結合實在是太密切,很難說分就分,雖然要把layout獨立出來要花更多時間,但至少會更好維護,往後要換template也不用再重寫程式,本篇文章稍微紀錄一下使用心得與過程。
if (typeof examples == 'undefined') { var examples = {}; }
{namespace examples}
/**
* Says hello to the world.
*/
{template .helloWorld}
Hello world!
{/template}
if (typeof examples == 'undefined') { var examples = {}; }
if (typeof examples.helloworld == 'undefined') { examples.helloworld = {}; }
{template .helloName}
blablabla
{/template}
java -jar SoyToJsSrcCompiler.jar --outputPathFormat simple.js simple.soy

Categories
Tag Cloud
Blog RSS
Comments RSS
Last 50 Posts
Back
Void « Default
Life
Earth
Wind
Water
Fire
Light 