Donnerstag, 24. Juli 2008

Grails, AJAX and Client Templates

Grails just gets better by the day for me. Experimenting with AJAX especially with dynamically updating parts of screen, I have come across following set of problems:

  • How to transfer the data?
  • How to render the data?
  • How to add more dynamic functions to dynamically loaded portions of the page?

My initial attempts included rendering HTML on the server and transferring the code to the client, and then replacing innerHTML of the loaded page. This worked  OK, until I also wanted to invoke Javascript functions after loading the HTML. Before I realized what was happeninng I ended up with loads of HTML Snippets on the Server and Javascript Spaghetti Nightmare all over the place.

After some research i came across TrimPath. TrimPath is a template engine written entirely in Javascript and therefore can run on the client. It is quite similar to technologies like Velocity, JSP, or ASP and uses a Javascript object graph as a context for the dynamic parts. 

Folllowing Setup has proven to work for me:

Include Trimpath Library and Templates in the root HTML

Trimpath is loaded from a single Javascript file, I used the GSP tag for this:


Each template (i.e. page parts which will be dynamically updated) I want to use, is embedded in a hidden div tag, and has an ID. To simplify this step, I created a custom Tag for this:
[...]

To further streamline the process I added a number of additional tags with abstract looping, conditions, "inline edit boxes" and the likes. Currently all of this code is built around the Yahoo UI (YUI) library. I plan to abstract this like the other Grails AJAX tags.

Create Base Mechanism to Render Templates and Reload Page Portions

Using the Yahoo UI Scoping and Layout mechanism and Trimpath Library the Javascript is a no brainer:

YAHOO.ST.editor.renderTemplate = function (templateId, contextObject){
   var template = TrimPath.processDOMTemplate(templateId, contextObject)  
   YAHOO.ST.global.layout.getUnitByPosition('center').body.firstChild.innerHTML = template
};

Thats it. 

Invoke the Reload
All which is left on the client side now is to get the data to render (called contextObject in the previously mentioned code) and decide which template to show (called templateId above). In my example it is done in an Yahoo AJAX callback passing the JSON response as the contextObject:

success: function(oResponse) {
   var oResults = eval("("+oResponse.responseText+")")
   YAHOO.ST.editor.renderTemplate('template_edit", oResults)
}        

Provide JSON data 

Grails provides the easiest possible way to provide JSON formatted results. Import the JSON format:

import grails.converters.JSON  

and then in the controller render the result like follows:

render returnMap as JSON    

Done!

I am still tinkering on how to distribute logic: Should the client only pull data and decide what to do with it, or should the server send code and invoke methods on the client? If the latter is the case, I would think that a RJS like extension for GRAILS would be desirable.

Grails, Javascript Templating and YahooUI make it so easy to create Rich Internet Applications by "pure programing", that it never ceases to amaze me. Keep up the good work!

36 Kommentare:

很悶 hat gesagt…

ut女同聊天室38ga成人尼克成人百分百貼圖85cc免費影片觀看108無碼av艾利絲寫真集自拍露點熟女人專賣店線上無碼人妻熟女自拍動物性交正妹被強姦3p換妻熟女交友洪爺色情網站自拍走光愛愛愛鋼管鋼管秀ms5 hinet net 性愛85cc街影城a圖貼大奶正妹可愛愛心圖片tt 同志交友網免費色情小說a片dvdsexinsex666 貼圖區素人王國人氣女優激情無碼yes 777歐美貼圖wii777遊戲備份站lion travel素人大全集85cc 嘟嘟影城情趣用品線上直購網 國外進口情趣商品辣妹神算走光影片裸體彩繪比基尼車隊裸體遊戲無名正妹牆qvod成人tt1069男同志論壇天天看正妹瑤瑤寫真

簡單 hat gesagt…

Knowledge is power...................................................

心臟 hat gesagt…

想像是什麼並不重要,想像能做什麼才重要..............................

春天來嚕 hat gesagt…

Birthdays are good for you. The more you have, the longer you live.............................................

念強 hat gesagt…

正妹日報成人版免費視訊聊天室美女貼圖518人力銀行性感美女線上av免費影片台灣a片王-avdvd免費視訊辣妹聊天室成人 影片明星露點照明星色情文學明星色情寫真明星色情貼圖真真明星色情照片明倫高中正妹明道中學口交日本漫畫卡通排行榜日本漫畫卡通 h 線上觀看日本漫畫色情視訊 辣妹sexdiy影城米克綜合論壇85cc免費聊天室露點girl5320av一葉情貼影色網性愛影片77p2p影片網性愛貼圖sogo論壇xvideos日本sex888免費a片卡通一夜成人聊天室免費影片線上直播ut同志聊天室sm聊天室高雄視訊淫美論壇免費18影片85st免費影片觀看

駱治男 hat gesagt…

堅決的信心,能使平凡的人,成就不平凡的事。......................................................

ZenaT_Pinter2284 hat gesagt…

Nice Post~!!!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

美岑 hat gesagt…

自拍走光貼走光自拍自拍性照片台灣性愛自拍影片歐美免費自拍日本妹av女優日本妹圖日本性姿勢日本性感熱辣寫真日本性影片日本真情寫真日本做愛姿勢日本無碼免費影片日本免費a短片日本免費a片線上看日本免費a片線上日本女優觀賞日本片免費試看日本片商日本卡通免費a片日本卡通圖庫日本正妹av女優日本成人片卡通日本成人影城日本色小遊戲日本色情按摩影片日本色情區日本色情漫話日本免費av片日本無碼貼圖金瓶梅成人漫畫金瓶梅情色小說文學成人無碼影片

上宇軒興 hat gesagt…

haha~ funny! thank you for your share~ ........................................

GriselJuhasz hat gesagt…

在莫非定律中有項笨蛋定律:「一個組織中的笨蛋,恆大於等於三分之二。」...............................................................

EdwardG_Burrell1016 hat gesagt…

責人之心責己,恕己之心恕人。.............................................

志文 hat gesagt…

希望是風雨之夜所現之曉霞........................................

柏勳 hat gesagt…

人不能像動物一樣活著,而應該追求知識和美德..................................................

07_TeddyF_Silvey0 hat gesagt…

快樂,是享受工作過程的結果...........................................................................

思姿穎穎 hat gesagt…

成熟,就是有能力適應生活中的模糊。..................................................

怡如怡如 hat gesagt…

生命是一頓豐富的宴席,有人卻寧可挨餓 ..................................................

黃佳伸 hat gesagt…

你不能左右天氣,但你可以改變心情.............................................................

宜欣 hat gesagt…

寂寞又無聊 看到你的BLOG 加油喔!!......................................................................

姜志銘 hat gesagt…

河水永遠是相同的,可是每一剎那又都是新的。......................................................................

asdas hat gesagt…

成熟,就是有能力適應生活中的模糊。.................................................................

育財育財 hat gesagt…

成熟,就是有能力適應生活中的模糊。.................................................................

王名仁 hat gesagt…

當一個人內心能容納兩樣相互衝突的東西,這個人便開始變得有價值了。............................................................

珮陽 hat gesagt…

馬丁路德:「即使知道明天世界即將毀滅,我仍願在今天種下一棵小樹。」............................................................

雅俊芬凱陳許 hat gesagt…

寫文章需要心情~~期待你再一次的好文章............................................................

淑蔡承均dgd周真淑ii淑 hat gesagt…

生存乃是不斷地在內心與靈魂交戰;寫作是坐著審判自己。..................................................

蔡靜芳蔡靜芳 hat gesagt…

所有的資產,在不被諒解時,都成了負債............................................................

洪瑋婷洪瑋婷 hat gesagt…

融會貫通的智慧,永遠不會被遺忘。..................................................

李志吉 hat gesagt…

人類的聰明,並非以經驗為依歸,而是以接受經驗的行程為依歸。..................................................

小袁璇 hat gesagt…

大家隨便說,說什麽都好,就是請不要惦惦............................................................

恭v秋昀嘉 hat gesagt…

Learning makes a good man better and ill man worse.............................................................

建邱勳 hat gesagt…

幸運是機會的影子............................................................

偉曹琬 hat gesagt…

No pains, no gains.......................................................................

于庭吳 hat gesagt…

看看blog調整心情,又要來繼續工作,大家加油............................................................

峻胡邦慧v帆 hat gesagt…

you got it!i like it!!!..................................................................

孟謝謝謝凱謝謝謝謝 hat gesagt…

心平氣和~祝你也快樂~~..................................................

佳張張張張燕張張張張張 hat gesagt…

變天了~~注意身體,別感冒囉!............................................................