[rt-users] Using Ajax with Mason in RT

Paul Tomblin ptomblin at xcski.com
Wed Aug 15 11:44:35 EDT 2012


On Wed, Aug 15, 2012 at 11:37 AM, Martin Drasar <drasar at ics.muni.cz> wrote:

> I have used MakeClicky actions to extend the RT UI and now I would like
> my extensions to do some ajax stuff on click. I would like to know, how
> I can create new Mason component and set it as a valid target for the
> ajax request.
>
> Any help is really appreciated.
>

If you want your AJAX to return JSON, you need to do the following:

1. Do all the work of building the return component in the <%init>.  I make
it a hashref called $ret.
2. In the "web" section of the file (the part that isn't enclosed in
<%init> or other Mason tags:
% $r->content_type('application/json');
% $m->clear_buffer;
<% JSON::to_json( $ret, { utf8 => 1, allow_blessed => 1,
    convert_blessed => 1, } ) |n %>
% $m->abort;
3. Declare as <%args> all the parameters you pass it
4. Turn off any inheritance with
<%flags>
inherit => undef
</%flags>
5. Call it in your Javascript like so:
  jQuery.ajax({
        type:           'POST',
        url:            'assign_ticket.mas',
        data:           {
            'ticketid':         ticketId,
            'assignTo':       rt_user_id,
            'reason':           reason,
            'currentUserId':    <% $session{'CurrentUser'}->id %>
        },
        dataType:       'json',
        traditional:    true,
        success:        changeStatusSuccessCallback,
        error:          changeStatusErrorCallback
  });

function changeStatusSuccessCallback(json)
{
  if (json.status == 'error')
  {
    alert('Error: ' + json.error_msg);
  }
  else
  {
    document.location.reload(true);
  }
}
function changeStatusErrorCallback(jqXHR, textStatus, errorThrown)
{
  alert(textStatus + ": " + errorThrown);
}

I'll leave it to you to figure out something better to do with the return
callbacks.




-- 
http://www.linkedin.com/in/paultomblin
http://careers.stackoverflow.com/ptomblin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.bestpractical.com/pipermail/rt-users/attachments/20120815/84d8da3a/attachment.htm>


More information about the rt-users mailing list