views:

35

answers:

2

I'm just getting my feet wet with jQuery after beginning to read Sitepoint's "Novice to Ninja" but, as always, I'm left wondering if there's a better way to write the code I've come up with. As it turns out, the answer is almost always and emphatic "yes."

All these "if" statements seem ridiculous. How can I do this better? What functions should I look at to clean this up. Thanks for the help.

$('#user').change(function(){   
var user_id = $('#user').val();
$.ajax({
    type: 'POST',
    url: '../admin/billing/' + user_id,
    dataType: 'json',
    success: function(billing){
        //alert(billing.id);
        var name = '<a href="../user/view/' + user_id +'">' + billing.fname + ' ' + billing.lname + '</a><br />';
        if(billing.company_name != ''){
            var company_name = billing.company_name + '<br />';
        }else{
            var company_name = '';
        };
        if(billing.address_one != ''){
            var address_one = billing.address_one + '<br />';
        }else{
            var address_one = '';
        };
        if(billing.address_two != ''){
            var address_two = billing.address_two + '<br />';
        }else{
            var address_two = '';
        };
        var csz = billing.city + ', ' + billing.state + ' ' + billing.zip + '<br />';
        if(billing.phone != ''){
            var phone = billing.phone + '<br />';
        }else{
            var phone = '';
        };
        var data = name + company_name + address_one + address_two + csz + phone;
        $('#billing').empty().append(data);
        $('input:text').val('');
        $('#same-as-billing').attr('checked', false);
    }
});

});

A: 

Not a huge improvement but will help John Resig's Javascript Micro Templating

Generally you want to try to render everything server side but if you have no choice (data coming from an external domain) it's helpful.

Ken Struys
That actually is a great lead in to a question I've been struggling with Ken. Thanks. I'll be using the same JSON data for a few different jQuery actions but all will be formatted different ways. Do you mean to imply it's better to run a few different ajax calls and have the server do the work than it is to run 1 ajax call and have jQuery do the work? Thanks!
Ryan
@Ryan generally I would say you want to keep your templates on the server side. I've been in a lot of situations where I do templating in javascript and then I need the same template server-side in php (or vise-versa). I don't like have multiple copies of the same template obviously, so it ends up being refactored to a server-side template. I do understand your point, you're just making more ajax calls that you might not need but you get the advantage of cached rendering and template consistency. It's really up to you but if you're doing it client side use some form of a template.
Ken Struys
Ryan
A: 

You can loop through json object and create sting:

var data = [];
$.each(billing, function(key, value){
  if(value != "") data.push(value);
});
data = data.join("<br />");

But this wont work for you if you need more than just output object values (just an example). There is nothing much that you can optimize here. You can only skip some elses

var data = '<a href="../user/view/' + user_id +'">' + billing.fname + ' ' + billing.lname + '</a><br />';
if(billing.company_name != "") data+= billing.company_name+"<br />";
if(billing.address_one != "") data+= billing.address_one+"<br />";
data+= billing.city+", "+billing.state+" "+billing.zip+"<br />";
// and so on
Anpher
I'm planning to use the same set of JSON data for a few different jQuery actions, Anpher, so I don't think the loop will work for me. However, the condensed version (without the else's) looks great. Thanks!
Ryan