Chorn Sokun's Weblog

Conquer inner fear, push it to the limit!

Archive for March 2009

MonoRail & jQuery

leave a comment »

Last year I blog about using jQuery.ajaxForm() to send http post request to MonoRail action and finally it depend on JSGenerator to produce javascript and send it back to client.

But overtime I find it pretty scary using that approach:

  • It give too much control to the server – the server can generate a bunch of javascript and send off to the client sort of invade client responsibility.
  • In some scenario I even specify from the server which div element I want to render my data table in
  page.ReplaceHtml('#divId', "content_to_be_replace_from_server")
  • using approach in previous post I was not be able to ensure 100% that if I send piece of jquery script that it combine into the main DOM properly for instance I did have a form & a jQuery to confirm the form into ajaxForm but that behavior is not always reliable.

So what the deal now? well, I happen to change the strategy a while ago just haven’t got time to blog about it the solution is pretty simple “Don’t make JSGenerator your new hammer.”, Let see my simplify approach:

<div id='dlist'>
  <!-- I am going to render list of data in this div --></div>
<input type="button" value="Show data" onclick="showData();" />
<input type="button" value="Add more" onclick="addData();" />
<div id='dform' style='display: none;'>
  <!-- I am going to load form into this div --></div>

That pretty normal HTML markup right? 2 div one for display list of data second one for display form for adding more data and I also have 2 buttons to invoke client script (jQuery).

function showData(){
   $('#dlist').load( '/task/list.rails', {'status': 'incomplete'} );

function addData(){
   $('#dform').load( '/task/add.rails', {}, function(){

Now that instead of using $.ajax I use $(‘#elementId’).load() instead because load() will inject html render by MonoRail action in the DOM and it just as native element.

Note : line 07 dform had style apply so that without having form inside it don’t occupy space in the page however one content loaded we need to set visibility for it.

For your reference the final piece of the puzzle is purely view code like this (View\task\add.brail)

<form method='post' id='taskForm' action='/task/create.rail'>
  Name: <input type='text' name='t.Name' />
  <input type='submit' value='Create' />

<!-- not inject some jQuery script along make the form submit via ajax -->
       $('#taskForm').ajaxForm ({
          success: function(response){
          error: function (xhr, status, error) {} // handle error if you wish

Notice on line #09 : need the form to submit using ajax and line #11 the client take control where it want to place the result (list of task).

It seem like we have to write a bit more code (jQuery) but after all that the fun part of being a programmer write code that :) Hope this would help.

Written by Chorn Sokun

March 26, 2009 at 9:13 pm

Posted in Castle, MonoRail, Tips & Tricks

Tagged with ,

MonoRail vs ASP.NET MVC Take #1 HTTP verbs

with 2 comments

Along came ASP.NET MVC although I am not the fan of ASP.NET MVC but it doens’t heard reading ASP.NET free chapter (nerddinner) and I did learn a few tricks, I will try my best to map concept between the two framework now let see

Why differentiate via HTTP verbs? read the book and find out, but here the deal

  • ASP.NET MVC [AcceptVerbs(HttpVerbs.Post)]
  • and [AccessibleThrough(Verb.Post)] in MonoRail
// GET: /Payroll/Delete/2
public void Delete([ARFetch("id"}] Payroll payroll){
   if (payroll == null)
       PropertyBag["payroll"] = payroll;

// POST: /Payroll/Delete/2
public void Delete([ARFetch("id"}] Payroll payroll, bool confirm){
   if (payroll == null)
     ..... // drop the record off the database
     ..... // do whatever you want.
          RedirectToAction("Delete", "id"  + payroll.Id);

MonoRail Rock!

Written by Chorn Sokun

March 26, 2009 at 10:17 am

Posted in Castle, MonoRail, Tips & Tricks

Tagged with

Game? I’m suck! Help

with 24 comments

Last night while I went out to TnC for a reason I saw some friends Vannak & Ratha surprisingly they was having a good time in JXII world ;)

Talking about game when I was young (g! am I old) nah that time I was probably 12-15 years old and I love playing video game. My favorite was(is) “Street Fighter” but dear I hardly play those game anymore after my 2nd big brother punished me for always leaving home for game :( kids you know. Alright so what the deal? I moved my focus from game to computer and it end up with me being a computer programmer.

Suddenly in the late 2008 a friend who is a graphic designer Sambath persuaded me to play the game (JXII) well since I got a copy of the game during BarCamppp and consider the level of stress I had during working day I decided I should try something fun and there it started. But man, I may have been “‘smarter” figure out how code work, but it seem like all the time when I try to be a gamer (computer game) I feel suck.

If you are my dear reader a JXer willing to help me becoming a better player please drop your suggestion base on the following spec:

Characters & skills Learnt

I am WuDang Sword sect, current lv 59, learn almost all available skill elements.
No school book went to HS a few times with deadly result :D even wounded.
Any tips, tricks … :) let play the game.

Written by Chorn Sokun

March 25, 2009 at 11:21 am

Posted in Geek, Socialize

Brail Daily Use Macros #2

with 2 comments

Imagine you had to associate multiple skill to your “Profile”

Multiple skill

Suppose we have the following Profile & Skill class

class Profile {
  public virtual int Id { get; set; }
  public virtual string Name { get; set; }
  //.... other properties as necessary
  public virtual IList<Skill> Skills { get;set; }

// a lookup class per say
class Skill {
  public virtual int Id { get;set; }
  public virtual Profile { get; set; }
  public virtual string Title { get; set; }

Assume that we know how to apply AR attribute on these two class make it persist able to a database. The important thing is Profile has many Skills that all you have to concentrate on right now.

Ok ! today recipe is to generate UI with checkbox which allow user to tick and save the choices without any hassle. With the help of FormHelper method YES, WE CAN

list = Form.CreateCheckboxFieldList("p.Skills", skills, {@text:'Title', @value:'Id'})
// we need a loop
for item in list:
  ${list.Item()}  ${item.ToString()}

Don’t panic let me explain a bit ${list.Item()} will generate individual html checkbox where ${item.ToString()} will generate a label for each one.

Written by Chorn Sokun

March 16, 2009 at 5:00 pm

Brail Daily Use Macros

with one comment

If you are DRY then try these macros in your *.brail

<% OutputSubView("_list") %>

Passing some variable to sub-view

<% OutputSubView("_list", {@students: studentList}) %>

but if you work with *.brailjs then you would love

Page.ReplaceHtml("#domElementId", {@Partial: '_list'})

Why do I choose brail?

  • it compile => performance gain
  • easy to create additional template processing macros
  • it just Boo !

until next time.

Written by Chorn Sokun

March 10, 2009 at 10:22 am

Posted in Castle, Tips & Tricks

Tagged with