Open Side Menu Go to the Top

05-08-2013 , 05:52 PM
hmm, i tried this and same result:

Code:
  $('#rankings_link').click(function(){
    $.ajax({
      dataType: 'json',
      url: '/rankings',
      type: 'get'
    }).done(function(received_data){
      var jsonObj = $.parseJSON('[' + received_data + ']');
      for (var m = 0; m < jsonObj.length; m++) {
        $('#rankings ol').append('<li>' + jsonObj[m] + '</li>');
      }
    });
  });
Also I just realized, I'm doing an AJAX request but it's triggered by clicking on a link which sends the user to /rankings...that's an issue isn't it?

It should be staying on the same page...

But if that's the case, how can I have this link in my nav bar? Seems like I should leave the link to /rankings as is and then, on that page, bind the ajax call to a button click (I'll have to create this).

Does that sound right?
Learning Javascript Thread Quote
Learning Javascript Thread
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Learning Javascript Thread
05-08-2013 , 06:36 PM
try this,

Code:
  $('#rankings_link').click(function(e){
    e.preventDefault();
    $.ajax({
      dataType: 'json',
      url: '/rankings',
      type: 'get'
    }).done(function(received_data){
      var jsonObj = $.parseJSON('[' + received_data + ']');
      for (var m = 0; m < jsonObj.length; m++) {
        $('#rankings ol').append('<li>' + jsonObj[m] + '</li>');
      }
    });
  });
you can stop the browser from following links by calling the preventDefault method on the event.
Learning Javascript Thread Quote
05-08-2013 , 07:32 PM
parseJSON won't work like that I don't believe. What I was trying to say is instead of having a JSON object that looks like :

Code:
{
    "foo": "bar",
    "foo2": "bar2",
    "foo3": "bar3"
}
change the format of it to look like this, as you don't need your "extra" keys at all:

Code:
{
    "foo": ["bar, "bar2", "bar3"]
}
then you can easily iterate through the array in your ajax callback, like:

Code:
for (var m = 0; m < received_data.foo.length; m++) {
    $('#rankings ol').append('<li>' + received_data.foo[m] + '</li>');
 }
Learning Javascript Thread Quote
05-08-2013 , 09:41 PM
but that's not what my json object looks like

mine is just

Code:
["Farag, Ali", "Harrity, Todd",...]
Learning Javascript Thread Quote
05-08-2013 , 09:51 PM
Quote:
Originally Posted by Grue
JSON/javascript objects don't have a length, you're thinking of an array. turn your json object into an array value so that you iterate through it.
if the json data returned is a string representation of an array, it can be parsed with JSON.parse() and will indeed have a length.
Learning Javascript Thread Quote
05-08-2013 , 11:33 PM
Quote:
Originally Posted by Mariogs37
but that's not what my json object looks like

mine is just

Code:
["Farag, Ali", "Harrity, Todd",...]
well this isn't a valid json object. read the wiki page etc. do something like this:

Code:
var data = {
"names": ["Farag, Ali", "Harrity, Todd", ....]
}
and then use data.names[m] to iterate through it and put it in the DOM and all that.
Learning Javascript Thread Quote
05-08-2013 , 11:41 PM
Quote:
Originally Posted by Loc
if the json data returned is a string representation of an array, it can be parsed with JSON.parse() and will indeed have a length.
looks like you're right actually, never really tried doing it that way I guess. OP could probably just pass an array string and make that work.
Learning Javascript Thread Quote
05-08-2013 , 11:44 PM
Quote:
Originally Posted by Grue
well this isn't a valid json object. read the wiki page etc. do something like this:

Code:
var data = {
"names": ["Farag, Ali", "Harrity, Todd", ....]
}
and then use data.names[m] to iterate through it and put it in the DOM and all that.
just saying the length thing isn't the source of error and should iterate fine even if "['bleh', 'blah']" is returned via ajax:

https://developer.mozilla.org/en-US/...cts/JSON/parse

i suspect the problem is related to binding the anchor tag.
Learning Javascript Thread Quote
12-07-2013 , 12:44 AM
Lately I've been thinking about making a range calculator type app in javascript. It would be like flopzilla (I assume, I only ever saw it demonstrated in a youtube video, https://www.youtube.com/watch?v=avfMUeveAcc).

I want to have a ui like pokerstove that is parsed into the shorthand notation (ie 44-77 instead of 44,55,66,77 etc) and then send the shorthand string to the server where I'll run a python script I've already wrote determine the ranges and send back a json object to populate some type of chart or table.

What I'm wondering is whether backbone or angular would be of much help here. I've never built anything but todo apps with them but have been looking for an opportunity to really try them out. I can instantly invision the jQuery soup way of solving this by storing the data on the dom elements with some event handler to parse the data and create the range string, and I wonder if there is any real point in having the range of cards in code as a model?
Learning Javascript Thread Quote
12-07-2013 , 12:52 AM
thinking about it a bit more, I think it could simplify the creation of the subview for selecting suits for a specific card combo ( like selecting just cc for AJs )
Learning Javascript Thread Quote
Learning Javascript Thread
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Learning Javascript Thread

      
m