Outputting results of Pylons function to Javascript

2007-11-04 Thread JamesT

I have a javascript file in /public/javascript that I want to receive
data from a pylons function. I am returning JSON formatted text in
this function in /controller/player.py and want the JSON output going
to a Javascript function. I am not sure how to route this data to this
function. I have tried the following:

this.dataSource= new YAHOO.util.DataSource(http://localhost:2985/
player/get_data);
this.dataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;

where get_data is the function that returns a JSON object, but it does
not receive anything. If I type that URL in my browser, I see the JSON
formatted data. Do I need to do something completely different to get
the data to the Javascript?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~--~~~~--~~--~--~---



Re: Outputting results of Pylons function to Javascript

2007-11-04 Thread Christoph Haas

On Sun, Nov 04, 2007 at 07:45:13AM -0800, JamesT wrote:
 I have a javascript file in /public/javascript that I want to receive
 data from a pylons function.

I assume that you are including and running that Javascript from the
HTML output you send to the browser. :)

 I am returning JSON formatted text in this function in
 /controller/player.py and want the JSON output going to a Javascript
 function. I am not sure how to route this data to this function. I
 have tried the following:
 
 this.dataSource= new YAHOO.util.DataSource(http://localhost:2985/
 player/get_data);
 this.dataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
 
 where get_data is the function that returns a JSON object, but it does
 not receive anything. If I type that URL in my browser, I see the JSON
 formatted data. Do I need to do something completely different to get
 the data to the Javascript?

That looks right to me. I don't know or use the YUI library myself
though. But I use the @jsonify decorator for certain actions in a
controller and my Javascript (jQuery Javascript library) roughly looks
like:

$.getJSON(
'/ajax/someaction',
{ parameter1: 'foobar1' },
callback_function_to_be_called_when_the_data_is_received
);

def callback_function_to_be_called_when_the_data_is_received(data) {
alert(data['something']);
}

The Pylons controller would look like this:

class AjaxController(BaseController):
@jsonify
def someaction(self):
return ['something' : 42]

Otherwise - in case you are using Firefox - the FireBug extension helps
debugging Javascript code. Is YAHOO.util.DataSource() perhaps an
asynchronous call and doesn't block until you get the data back?

Cheers
 Christoph

P.S.: All code untested so copy/paste may fail miserably.
P.P.S.: I've been using AJAJ in my application for a week but I love
how Pylons and jQuery really make that trivial. If only there
were documentation on that. :)


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~--~~~~--~~--~--~---



Re: Outputting results of Pylons function to Javascript

2007-11-04 Thread JamesT

I think it has to do with the route to the pylons controller/function
from the javascript file. Is your javascript located in /public/
javascripts/? Mine is located here and my controller is in /
controllers/player.py. Can i refer to the controller/function from the
javacript file by using /player/get_data or must I do something
else? It seems in the YUI documentation they say to use a local URL or
local Proxy, but I would think Pylons Routes would handle this.
@jsonify seems to work fine for the output of the function if I load /
player/get_data in my browser.

On Nov 4, 10:18 am, Christoph Haas [EMAIL PROTECTED] wrote:
 On Sun, Nov 04, 2007 at 07:45:13AM -0800, JamesT wrote:
  I have a javascript file in /public/javascript that I want to receive
  data from a pylons function.

 I assume that you are including and running that Javascript from the
 HTML output you send to the browser. :)

  I am returning JSON formatted text in this function in
  /controller/player.py and want the JSON output going to a Javascript
  function. I am not sure how to route this data to this function. I
  have tried the following:

  this.dataSource= new YAHOO.util.DataSource(http://localhost:2985/
  player/get_data);
  this.dataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;

  where get_data is the function that returns a JSON object, but it does
  not receive anything. If I type that URL in my browser, I see the JSON
  formatted data. Do I need to do something completely different to get
  the data to the Javascript?

 That looks right to me. I don't know or use the YUI library myself
 though. But I use the @jsonify decorator for certain actions in a
 controller and my Javascript (jQuery Javascript library) roughly looks
 like:

 $.getJSON(
 '/ajax/someaction',
 { parameter1: 'foobar1' },
 callback_function_to_be_called_when_the_data_is_received
 );

 def callback_function_to_be_called_when_the_data_is_received(data) {
 alert(data['something']);
 }

 The Pylons controller would look like this:

 class AjaxController(BaseController):
 @jsonify
 def someaction(self):
 return ['something' : 42]

 Otherwise - in case you are using Firefox - the FireBug extension helps
 debugging Javascript code. Is YAHOO.util.DataSource() perhaps an
 asynchronous call and doesn't block until you get the data back?

 Cheers
  Christoph

 P.S.: All code untested so copy/paste may fail miserably.
 P.P.S.: I've been using AJAJ in my application for a week but I love
 how Pylons and jQuery really make that trivial. If only there
 were documentation on that. :)


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~--~~~~--~~--~--~---



Re: Outputting results of Pylons function to Javascript

2007-11-04 Thread Christoph Haas

On Sun, Nov 04, 2007 at 05:48:10PM -, JamesT wrote:
 I think it has to do with the route to the pylons controller/function
 from the javascript file. Is your javascript located in /public/
 javascripts/?

Yes. And in one case I load the Javascript from my HTML document like:

script src=/javascripts/foobar.js type=text/javascript

Mostly I have it contained in my Mako templates so the user's browser
doesn't need to do an extra HTTP request.

 Mine is located here and my controller is in /
 controllers/player.py. Can i refer to the controller/function from the
 javacript file by using /player/get_data or must I do something
 else?

No, that's fine. You tried that in the browser and as you say it worked.
A relative path is fine if you access it from the same site. And I
believe that doing AJAXy requests to other web sites is prohibited for
security reasons anyway. At least my browser refuses to do that.

 @jsonify seems to work fine for the output of the function if I load /
 player/get_data in my browser.

Then I suspect the YUI is not doing what you expect. Perhaps we have
some YUI experts here.

 Christoph


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~--~~~~--~~--~--~---



Re: Outputting results of Pylons function to Javascript

2007-11-04 Thread JamesT

I just got it, I just needed to include  $
{h.javascript_include_tag('yui/build/connection/connection-min.js')}
in my .mako file before including the datasource component. This makes
the /controller/function map as expected.
Thank you for your help and your quick responses.

On Nov 4, 1:47 pm, Christoph Haas [EMAIL PROTECTED] wrote:
 On Sun, Nov 04, 2007 at 05:48:10PM -, JamesT wrote:
  I think it has to do with the route to the pylons controller/function
  from the javascript file. Is your javascript located in /public/
  javascripts/?

 Yes. And in one case I load the Javascript from my HTML document like:

 script src=/javascripts/foobar.js type=text/javascript

 Mostly I have it contained in my Mako templates so the user's browser
 doesn't need to do an extra HTTP request.

  Mine is located here and my controller is in /
  controllers/player.py. Can i refer to the controller/function from the
  javacript file by using /player/get_data or must I do something
  else?

 No, that's fine. You tried that in the browser and as you say it worked.
 A relative path is fine if you access it from the same site. And I
 believe that doing AJAXy requests to other web sites is prohibited for
 security reasons anyway. At least my browser refuses to do that.

  @jsonify seems to work fine for the output of the function if I load /
  player/get_data in my browser.

 Then I suspect the YUI is not doing what you expect. Perhaps we have
 some YUI experts here.

  Christoph


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~--~~~~--~~--~--~---