2012-04-25 8 views
0

次のシナリオ:彼らは単に彼らは私たちを置きたい場所にnode.js用のJavascript発光テンプレートエンジン?

<script language="Javascript" src="https://our.server/customer-name/entry-point.js"/> 

を書き込むことによって、自分のページ上の私たちの製品を含めることができるしているように、私たちは、私たちの顧客にいくつかのエントリポイントURL(https://our.server/customer-name/entry-point.jsのようなもの)を得ました製品(はい、私は知っている、これは醜い解決策ですが、私が変更できるものではありません)。

ここでは、私たちは問題に直面しています:私たちのentry-point.jsは何とかどこから(https://our.server/customer-name/)それは他のファイルを読み込む必要があります知っている必要があります。その答えはentry-point.jsを動的に生成して、それが例えば、

var ourcompany_ourproduct_basepath = "https://our.server/customer-name/"; 

これを行うための明白な方法は、手動でこのような何かentry-point.jsを構築することである:あなたが見ることができるように

res.write("var ourprefix_basepath = \"" + basepath.escape() + "\";"); 
res.write("function ourprefix_entryPoint() { /*do something*/ }"); 
res.write("ourprefix_entryPoint();"); 

を、それはあまりにも悪いだけです。

たとえば、次のようなテンプレートエンジンがありますか。次のために:

var ourprefix_basepath = "https://our.server/customer-name/"; 
function ourprefix_entrypoint() { /* do something */ }; 
ourprefix_entrypoint(); 

var basepath = "https://our.server/customer-name/"; 
var export = { 
    ourprefix_basepath: basepath.escape(), 
    ourprefix_entrypoint: function() { /* do something */ } 
}; 
templateEngine.render(export); 

または

view.vw: 
    ourprefix_basepath = rewrite("{#basepath}"); 
    function ourprefix_entrypoint() { /* do something */ 
    ourprefix_entrypoint(); 

App.js: 
    templateEngine.render("view.vw", { basepath: "https://our.server/customer-name/" }); 

またはこのような何か応答ストリームに次のように記述します、(あなたのアイデアを持っていますか)?

答えて

0

Node.jsがリフレクションをサポートしているようですが、明示的にどこかに記述されているかどうかはわかりません。

したがって、JSONは、JSのサブセットであるという事実を利用することによって、テンプレートエンジンを使用せずに次のクリーンなコードが可能である:

var entrypoint = function(data) { 
    /* do something with data.basepath and data.otherparam here... */ 
} 

exports.processRequest = function(request, response) { 
    var data = { 
     basepath: computeBasepath(request), 
     otherparam: "somevalue" 
    }; 
    response.send("(" + entrypoint.toString() + ")(" + JSON.stringify(data) + ")"); 
} 
関連する問題