views:

163

answers:

2

Basically I was trying to replicate one of the things that xajax gave me with Jquery -> The ability to define what my response modifies server side without setting anything up client side. Works perfectly as is, but each time I want to call a different Jquery function, I have to add it to my if statements.

Each element in the response array contains something to be changed client side.

I should be able to take out almost all of the this.action == 'blah' if statements and replace with some clever javascript, but apparently I'm not clever enough. :)

$.showMessage is simply my replacement for alert, with a timeout and sticky bit.

My client Jquery function:

$.doAjax = function(func,values) {
 $.ajax({ data: values, url: '/time/ajax/' + func, type: 'POST', dataType: 'json', timeout: 5000,
  error: function(xhr, ajaxOptions,thrownError){ $.showMessage('Error processing request: ' +  func,10000);$('#debug').html(xhr.responseText); },
  success: function(data){
    if(data){
  $.each(data, function(){
    if ( this.action == 'attr' ) $(this.target).attr(this.content);
    if ( this.action == 'removeAttr' ) $(this.target).removeAttr(this.content);
    if ( this.action == 'addClass' ) $(this.target).addClass(this.content);
    if ( this.action == 'removeClass' ) $(this.target).removeClass(this.content);
    if ( this.action == 'html' ) $(this.target).html(this.content);
    if ( this.action == 'text' ) $(this.target).text(this.content);
    if ( this.action == 'val' ) $(this.target).val(this.content);
    if ( this.action == 'eval' ) {
          try { 
        eval(this.content); 
      }
      catch(err) {
            $.showMessage(err,5000);
      };
    };
      });
    }
  } 
 });
 return this;
};

My server side code:

header("Content-type: text/plain");   
$response = array();
$response[] = array('action'=>'html','target'=>'#booked_time_group_unbilled','content'=>get_booked_time_group_unbilled());
$response[] = array('action'=>'html','target'=>'#booked_time_my_unbilled','content'=>get_booked_time_my_unbilled());
$response[] = array('action'=>'eval','target'=>'','content'=>"$.showMessage('The selected time has been deleted',5000,true)");
echo json_encode($response);
+2  A: 

This should work:

$(this.target)[this.action](this.content);
pflunk
`if ( this.action == 'eval' ) { eval(this.content); } else { $(this.target)[this.action](this.content);}
Justin Johnson
I tried all sorts of variations on what you just proposed. I'll let you know if it works...
Daren Schwenke
Yep. Justin's version works, but since he was a comment, you get the vote. Thank you.
Daren Schwenke
A: 

I'm not exactly sure what you're asking? Stylistically, I would write it like this:

var handlers = {
    attr: function() { $(this.target).attr(this.content) },
    removeAttr: function() { $(this.target).removeAttr(this.content) },
    // etc.
};

$.each(data,function() {
   handlers[this.action].call(this);
});

If you just want to execute whatever the server sends back, which not just always send back an eval action?

noah
mainly escaping.
Daren Schwenke