1

I'm rendering page using ajax and json. Structure of my json is {"status":"ok","rewards":[{"id":201,"points":500},{"id":202,"points":500}] How do i make ajax loading data only once one if 'points' duplicates in any of hashes?

E.g. i have json with few hashes in which 'points' have same value Here is my code

    $("#page").live('pagecreate', function(e) {
        var request = $.ajax({
            type: "GET",
            url: "example.com/file.json",
            dataType: "json",
            error: function (data, tex

tStatus){
           console.log( status );
          console.log( data );},
        success: function (data, textStatus){
        console.log( "success" );

        console.log( status );
           console.log( data );
         }
        })
        request.success(function(data, textStatus){
            var lis = "";
            var seen = {};
            $.each(data.rewards, function(key, val){
            lis += "<div class = 'reward-ui ui-block-" + String.fromCharCode(97 + key%3) + "'><a href ='#' class ='ui-link-inherit'>" + val.points + "</a></div>";
            });
            $(".ui-grid-b").html(lis);

            });
            //$('.even-odd').listview('refresh');
        })
      });
sanny Sin
  • 1,555
  • 3
  • 17
  • 27
  • That'd be something you do on the server when the data is being generated, or something in your .each loop to remember what's been previously displayed. this has nothing to do with ajax itself. – Marc B Nov 29 '12 at 14:26
  • yes, i tried solution from this question: http://stackoverflow.com/questions/2822962/jquery-remove-duplicate-elements, but that didnt work – sanny Sin Nov 29 '12 at 14:27

1 Answers1

2

Add a local array which will store all the items used. Push into this array in $.each function and before doing lis += " " check if the value already exists in the temp array.

Other than that you could try server side sorting before retrieving data ... like suggested above.

Alex_B
  • 838
  • 7
  • 13