Two Plus Two Poker Forums

Two Plus Two Poker Forums (https://forumserver.twoplustwo.com/)
-   Computer and Technical Help (https://forumserver.twoplustwo.com/48/computer-technical-help/)
-   -   Learning Javascript Thread (https://forumserver.twoplustwo.com/48/computer-technical-help/learning-javascript-thread-1223133/)

Mariogs37 05-08-2013 05:52 PM

Re: Learning Javascript Thread
 
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?

e i pi 05-08-2013 06:36 PM

Re: Learning Javascript Thread
 
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.

Grue 05-08-2013 07:32 PM

Re: Learning Javascript Thread
 
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>');
 }


Mariogs37 05-08-2013 09:41 PM

Re: Learning Javascript Thread
 
but that's not what my json object looks like

mine is just

Code:

["Farag, Ali", "Harrity, Todd",...]

Loc 05-08-2013 09:51 PM

Re: Learning Javascript Thread
 
Quote:

Originally Posted by Grue (Post 38413948)
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.

Grue 05-08-2013 11:33 PM

Re: Learning Javascript Thread
 
Quote:

Originally Posted by Mariogs37 (Post 38423417)
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.

Grue 05-08-2013 11:41 PM

Re: Learning Javascript Thread
 
Quote:

Originally Posted by Loc (Post 38423555)
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.

Loc 05-08-2013 11:44 PM

Re: Learning Javascript Thread
 
Quote:

Originally Posted by Grue (Post 38424734)
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.

e i pi 12-07-2013 12:44 AM

Re: Learning Javascript Thread
 
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?

e i pi 12-07-2013 12:52 AM

Re: Learning Javascript Thread
 
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 )


All times are GMT -4. The time now is 11:10 PM.

Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.

Copyright © 2008-2020, Two Plus Two Interactive