2010-06-07 19 views
4

一個約RJS最方便的事情之一是其呈現的部分,所以你把所有的視圖代碼在一個地方的能力:的Javascript模板沒有RJS,用JSON

# task/index.html.erb 
<ul id="task_list"> 
    <%= render :partial => 'task', :collection => @tasks %> 
</ul> 

# task/_task.html.erb 
<li> 
    <% if task.is_completed %> 
    <%= task.name %> - <%= task.completed_date %> 
    <% else %> 
    <%= task.name %> - UNCOMPLETED 
    <% end %> 
    ... 
</li> 

現在我試圖移動遠離RJS,並讓服務器以小巧,格式良好的JSON響應,而不是大量的JS + HTML。

有沒有辦法讓保持我的部分文件和代碼沒有重複並且能夠通過JS添加新項目到任務列表而不使用RJS?我研究過一些JavaScript模板引擎,但它們都需要我維護一個單獨的ruby部分和一個javacript模板。

+0

因此,對我而言,您看起來像是在尋找一種客戶端Javascript模板引擎,它可以理解您的RJS模板語法。這是否準確? – Pointy 2010-10-13 16:15:37

+0

你可能想看看TrimPath – Dapeng 2010-10-16 04:41:12

回答

8

的jQuery 1.4.3將包括TMPL插件(直接intergrated到jQuery的) 見http://api.jquery.com/jquery.tmpl/

<!DOCTYPE html> 
<html> 
<head> 
    <script src="http://code.jquery.com/jquery-latest.min.js"></script> 
    <script src="http://nje.github.com/jquery-tmpl/jquery.tmpl.js"></script> 
</head> 
<body> 

<ul id="movieList"></ul> 

<script> 
    var movies = [ 
    { Name: "The Red Violin", ReleaseYear: "1998" }, 
    { Name: "Eyes Wide Shut", ReleaseYear: "1999" }, 
    { Name: "The Inheritance", ReleaseYear: "1976" } 
    ]; 

var markup = "<li><b>${Name}</b> (${ReleaseYear})</li>"; 

/* Compile the markup as a named template */ 
$.template("movieTemplate", markup); 

/* Render the template with the movies data and insert 
    the rendered HTML under the "movieList" element */ 
$.tmpl("movieTemplate", movies) 
    .appendTo("#movieList"); 
</script> 

</body> 
</html> 
0

鬍鬚(http://mustache.github.com/)是沒有一個超級簡單的模板語言邏輯什麼的。它在Javascript和Ruby中都有實現,因此模板可以在任何環境中呈現。

使用比RJS(或類似的模板語言)有點棘手。由於模板中沒有邏輯,所以JSON對象必須擴展以提供更復雜的行爲。

下面是一些例子標記:

<div id="person_image"><img src="{{avatar_url}}"></img></div> 

<div class="info"> 
    <div id="person_location">{{location}}</div> 
    <h2 class="name">{{name}}</h2> 


    <div id="person_positions"> 
    <ul class="positions"> 
    {{#positions}} 
     <li>{{company_name}} — {{title}}</li> 
    {{/positions}} 
    </ul> 
    </div> 


    <div id="person_social_links"> 
    <div class="social-profiles research-links"> 
    {{#links}} 
     <a href="{{url}}" class="branded" target="_blank" style="background-image:url(https://www.google.com/s2/favicons?domain={{name}})"></a> 
    {{/links}} 
    </div> 
    </div> 
</div> 

這會使這個對象:

{ 
    "id":"4c0578d940cfd305ff00011c", 
    "name":"Steve Someguy", 
    "location":"Austin, Texas, United States", 
    "positions":[ 
    {"title":"CEO", "company_name":"ACME, Inc.", "company_id":null}, 
    {"title":"Director", "company_name":"Capsasin, Inc.", "company_id":"4c0578d940cfd305ff00011c"} 
    ], 
    "links":[ 
    {"name":"twitter.com","url":"http://twitter.com/spicyjs","image_url":"http://a3.twimg.com/profile_images/439463427/Picture_7_bigger.png"}, 
    {"name":"twitter.com","url":"http://twitter.com/shadr","image_url":"http://a1.twimg.com/profile_images/20072132/me.jpg"}, 
    {"name":"facebook.com","url":"http://facebook.com/shad.reynolds","image_url":"http://www.facebook.com/profile/pic.php?uid=AAAAAQAQm2JvEZLOpW8bCG-rToD8VQAAAAlFjZG9cIKwaX2wuA_Nspjn"} 
    ] 
} 

Handlebars.js是一個相關的項目,該項目還獲得了一些牽引力,雖然Ruby實現不似乎是完整的(http://yehudakatz.com/2010/09/09/announcing-handlebars-js/)。

+0

我不認爲提問者想要改變他當前的模板文件*。 – Pointy 2010-10-16 14:54:38

0

您要搜索的內容可能是JsonMLPure,我在JSON.org頁面的底部找到了很久以前的那些人,但我沒有親自去測試它們。

如果你使用它們,請告訴我它是怎麼回事。

祝你好運!