0

JSON object:

{"id":75,"year":2011,"make":"Aston Martin","model":"V12 Vanquish"}

jQuery:

function post() {
  $("form").on("ajax:success", function(e, data, status, xhr) {
    var json = JSON.parse(xhr.responseText);
    $.each(json, function(k, v) {
      $("#vehicle_record").append('<li class="'+k+'">'+v+'</li>');
    });
  });
}

HTML page output:

75
2011
Aston Martin
V12 Vanquish
75
2011
Aston Martin
V12 Vanquish

Questions:

  1. Why is the output rendered twice? (I only expect it once)
  2. Is there a better way to render JSON to the page? (please advise)

--UPDATE--

The following a walk through of the UX:
FORM ONE: name > click next > post > FORM TWO > Choose Vehicle > click next > post > see vehicle picked: add another vehicle? Y (BACK to FORM TWO) / N (continue...)

I need to be able to use post for each model independently without interference. So how do I get for example all of the JSON objects submitted to the vehicle model? (I need this post function to be modular)

Perhaps I can do something like this?:

function post() {
  $("form").on("ajax:success", function(e, data, status, xhr) {
    var json = JSON.parse(xhr.responseText);
    var model = e.currentTarget.action;
    renderOnPage(json, model);
  });
}
function renderOnPage(data, model) {
  if ( model.match(/vehicles/) ) {
  $.each(data, function(k, v) {
    $("#vehicle_record").append('<li class="'+k+'">'+v+'</li>');
  });
  }
}
9
  • 3
    looks like the method post was called twice!!! Commented Mar 1, 2014 at 0:53
  • 1
    What's the ajax:success event? That's not a standard jQuery event, AFAIK. Commented Mar 1, 2014 at 0:54
  • 1
    can you add a console logging in the post method as the first line and see how many times it is called Commented Mar 1, 2014 at 0:54
  • Also put it in the callback function, to see if something is triggering the event twice. Commented Mar 1, 2014 at 0:55
  • re rendering json ( client side templating ) if you don't want to make something, I like ejs ( simple ) and linkedins fork of dust ( if you need logic templating ) Commented Mar 1, 2014 at 0:58

2 Answers 2

1

Better or maybe best way to render JSON on HTML (without using third party libraries) is to come up with something like this:

<html>
<body>
<span>ID</span>: <span json-path="id"></span>
<span>Year</span>: <span json-path="year"></span>
<span>Make</span>: <span json-path="make"></span>
<span>Model</span>: <span json-path="model"></span>
</body>
</html>

where json-path is just any attribute name that you want to come up with. Now on success of AJAX you should do something like this:

function refreshPage(data) {
   $("[json-path]", $(document)).each(function() {
    $(this).text(data[$(this).attr("json-path")]);
   });
}

Now if you need more complex features like to display an array of items ins JSON in a table or LIST then just add a switch case statement in above refreshPage

 switch((this).prop('tagName')) {
        case "INPUT":
           $(this).val(data[$(this).attr("json-path")])

Using this approach you can even map your json-path to an array on JSON path for a TABLE or SELECT tag and then use switch case to identify and render each type of elements differently.

Sign up to request clarification or add additional context in comments.

1 Comment

Hey thanks for your suggestion could you perhaps share a link with a thorough example of your suggested implementation. Thanks again.
0

Your post function actually binds a handler twice to success event, therefore its obvious it's been executed twice. You don't actually need to call function explicitly I suppose. Simply

$("form").on("ajax:success", function(e, data, status, xhr) {
    var json = JSON.parse(xhr.responseText);
    $.each(json, function(k, v) {
      $("#vehicle_record").append('<li class="'+k+'">'+v+'</li>');
    });
});

would work. Don't call post on clicks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.