Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

this way I handle ajax response:

success: function (json) {                  
                $.each(json, function(index,item){              
                    if (item.field == "article_id") {
                        $("#article_id").val(item.value);
                    } else if (item.field == "naslov") {
                        $("#naslov").val(stripslashes(item.value));
                    }else if (item.field == "slug_naslov") {
                        $("#slug_naslov").val(item.value);
                    }else if (item.field == "datum") {
                        $("#datum").val(item.value);
                    }else if (item.field == "url") {
                        $("#url").val(item.value);
                    }else if (item.field == "tekst") {
                        $("#tekst").val(stripslashes(item.value));
                    }else if  (item.field == "tag_title") { //handle tags - tokeninput                          
                        var tagoviArray = item.value;                       
                        var tagoviInput = $("#txtTags");    
                            $(tagoviInput).tokenInput("clear");
                            $.each(tagoviArray, function (index, value) {                           
                                var arr = value.split(','); 
                                    $.each(arr, function (i, v) {   
                                        if (!(v=="")) { 
                                            $(tagoviInput).tokenInput("add", {id: "", name: v}); tag
                                        }
                                    });                         
                            });

                    }else if (item.field == "love") {
                        $("#love").val(item.value);
                    }          
                });
                return false;
            }

I am sure it can be improved :)

share|improve this question

1 Answer 1

up vote 4 down vote accepted

Maybe something like this?

success: function (json) {
  var decorators = {},
      handlers = {};

  // decorators prepare content for insertion
  decorators.naslov = stripslashes;
  decorators.tekst  = stripslashes;

  // specialized handlers
  handlers.tag_title = function (value) {
    // do the tokenization-stuff here
  };

  // Go through the response
  $.each(json, function (index, item) {
    var value = item.value;
    // if the field has its own handler, use that
    if( typeof handlers[item.field] === "function" ) {
      handlers[item.field](value);
    } else {
      // Otherwise, send the value through the decorator
      // (if there is one for the given field), and
      // insert the value in the corresponding input
      if( typeof decorators[item.field] === 'function' ) {
        value = decorators[item.field](value);
      }
      $("#" + item.field).val(value);
    }
  });
}
share|improve this answer
    
thank you, this look much "readable" I will give a try... I got the point – InTry Oct 7 '12 at 14:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.