How can I provide AJAX calls to 3er party libraries?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How can I provide AJAX calls to 3er party libraries?

Mariano Martinez Peck
Hi guys, 

I know how to use AJAX but always everything inside Seaside. I know need a way to provide JSON responses to AJAX calls made by a 3er party lib. For example, in Highstocks [1], there is an example like this:


  /**
     * Load new data depending on the selected min and max
     */
    function afterSetExtremes(e) {

        var chart = Highcharts.charts[0];

        chart.showLoading('Loading data from server...');
        $.getJSON('https://www.highcharts.com/samples/data/from-sql.php?start=' + Math.round(e.min) +
                '&end=' + Math.round(e.max) + '&callback=?', function (data) {

            chart.series[0].setData(data);
            chart.hideLoading();
        });
    }



So... how could I provide that at my server side? 


Above JS would be code that I write in Seaside and it is passed to the client. Therefore, I THINK I am able to write that JS generating a URL with the correct _s and _k or whatever..

Thanks in advance for any lead you can give me here.

--

_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: How can I provide AJAX calls to 3er party libraries?

JupiterJones
Hi Mariano,

I’ve been trying to work out callbacks recently. There seems to be a number of ways of doing this depending on your requirements. There may be typos in the following but hopefully it points you in the right direction :)

In the example you provided, the JSON is coming from an external server. I’m guessing that you want the JSON coming from your own backend via seaside.

renderContentOn: canvas
| start end |
(canvas jQuery getJson
        callback: [ :eventData |
 start := eventData first.
end := eventData last ]
          value: (JSStream on: 'new Array(Math.round(e.min),Math.round(e.max))'));
        text: [ :stream | 
              stream
                json:
                      GRSmallDictionary new
                        at: ’someKey' put: ’someValue';
                        yourself ]) ];
        onSuccess: ((JSStream on: 'chart.series[0].setData(data);') asFunction: #(‘data'));
        dataType: 'jsonp').

or

(html jQuery ajax
callback: [ :eventData | self doSomething: eventData ]
json: (JSStream on: 'new Array(Math.round(e.min),Math.round(e.max))'));

Then you can respond from somewhere in #doSomething:

doSomething: eventData
| start end jsonResponse |
start := eventData first.
end := eventData last.
jsonResponse := GRSmallDictionary new at: ‘someKey’ put: ‘someValue’; yourself.
 self requestContext
    respond: [ :response | 
      response
        doNotCache;
        contentType:
            (WAMimeType applicationJson charset: self requestContext handler charSet).
      response stream json: jsonRespnose

Alternatively if you need to specify a URL that the JS Library may use many times and pass or post query options, you can do something like:

urlForCallbacksFromJSLib
  | connectorUrl |
  connectorUrl := self renderContext actionUrl copy
    addField:
        (self renderContext callbacks
            store: (JSAjaxCallback on: [ self processCallback ]));
    yourself.
  self url: connectorUrl

…and then you can do whatever you need in the #processCallback method...

processCallback
  | start end jsonRespnose |
start := self requestContext request at: ‘start’.
end := self requestContext request at: ‘end’.
  jsonRespnose := self jsonObjectToBeReturnedToClient
  self requestContext
    respond: [ :response | 
      response
        doNotCache;
        contentType:
            (WAMimeType applicationJson charset: self requestContext handler charSet).
      response stream json: jsonRespnose ]

I hope there’s something there that will help.

Cheers,

J

On 12 Jan 2017, at 3:15 am, Mariano Martinez Peck <[hidden email]> wrote:

Hi guys, 

I know how to use AJAX but always everything inside Seaside. I know need a way to provide JSON responses to AJAX calls made by a 3er party lib. For example, in Highstocks [1], there is an example like this:


  /**
     * Load new data depending on the selected min and max
     */
    function afterSetExtremes(e) {

        var chart = Highcharts.charts[0];

        chart.showLoading('Loading data from server...');
        $.getJSON('https://www.highcharts.com/samples/data/from-sql.php?start=' + Math.round(e.min) +
                '&end=' + Math.round(e.max) + '&callback=?', function (data) {

            chart.series[0].setData(data);
            chart.hideLoading();
        });
    }



So... how could I provide that at my server side? 


Above JS would be code that I write in Seaside and it is passed to the client. Therefore, I THINK I am able to write that JS generating a URL with the correct _s and _k or whatever..

Thanks in advance for any lead you can give me here.

--
_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside


_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: How can I provide AJAX calls to 3er party libraries?

JupiterJones
In reply to this post by Mariano Martinez Peck
The InfoVis library also does what you’re looking for:


There’s likely some good examples of dealing with JSON in there.

_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: How can I provide AJAX calls to 3er party libraries?

Mariano Martinez Peck
In reply to this post by JupiterJones
Hi Jupiter,

Thank you very much for your code. That was exactly what I was looking for and it did work!  It looks like indeed you have worked with callbacks :)

Best regards, 

On Wed, Jan 11, 2017 at 7:21 PM, Jupiter Jones <[hidden email]> wrote:
Hi Mariano,

I’ve been trying to work out callbacks recently. There seems to be a number of ways of doing this depending on your requirements. There may be typos in the following but hopefully it points you in the right direction :)

In the example you provided, the JSON is coming from an external server. I’m guessing that you want the JSON coming from your own backend via seaside.

renderContentOn: canvas
| start end |
(canvas jQuery getJson
        callback: [ :eventData |
 start := eventData first.
end := eventData last ]
          value: (JSStream on: 'new Array(Math.round(e.min),Math.round(e.max))'));
        text: [ :stream | 
              stream
                json:
                      GRSmallDictionary new
                        at: ’someKey' put: ’someValue';
                        yourself ]) ];
        onSuccess: ((JSStream on: 'chart.series[0].setData(data);') asFunction: #(‘data'));
        dataType: 'jsonp').

or

(html jQuery ajax
callback: [ :eventData | self doSomething: eventData ]
json: (JSStream on: 'new Array(Math.round(e.min),Math.round(e.max))'));

Then you can respond from somewhere in #doSomething:

doSomething: eventData
| start end jsonResponse |
start := eventData first.
end := eventData last.
jsonResponse := GRSmallDictionary new at: ‘someKey’ put: ‘someValue’; yourself.
 self requestContext
    respond: [ :response | 
      response
        doNotCache;
        contentType:
            (WAMimeType applicationJson charset: self requestContext handler charSet).
      response stream json: jsonRespnose

Alternatively if you need to specify a URL that the JS Library may use many times and pass or post query options, you can do something like:

urlForCallbacksFromJSLib
  | connectorUrl |
  connectorUrl := self renderContext actionUrl copy
    addField:
        (self renderContext callbacks
            store: (JSAjaxCallback on: [ self processCallback ]));
    yourself.
  self url: connectorUrl

…and then you can do whatever you need in the #processCallback method...

processCallback
  | start end jsonRespnose |
start := self requestContext request at: ‘start’.
end := self requestContext request at: ‘end’.
  jsonRespnose := self jsonObjectToBeReturnedToClient
  self requestContext
    respond: [ :response | 
      response
        doNotCache;
        contentType:
            (WAMimeType applicationJson charset: self requestContext handler charSet).
      response stream json: jsonRespnose ]

I hope there’s something there that will help.

Cheers,

J

On 12 Jan 2017, at 3:15 am, Mariano Martinez Peck <[hidden email]> wrote:

Hi guys, 

I know how to use AJAX but always everything inside Seaside. I know need a way to provide JSON responses to AJAX calls made by a 3er party lib. For example, in Highstocks [1], there is an example like this:


  /**
     * Load new data depending on the selected min and max
     */
    function afterSetExtremes(e) {

        var chart = Highcharts.charts[0];

        chart.showLoading('Loading data from server...');
        $.getJSON('https://www.highcharts.com/samples/data/from-sql.php?start=' + Math.round(e.min) +
                '&end=' + Math.round(e.max) + '&callback=?', function (data) {

            chart.series[0].setData(data);
            chart.hideLoading();
        });
    }



So... how could I provide that at my server side? 


Above JS would be code that I write in Seaside and it is passed to the client. Therefore, I THINK I am able to write that JS generating a URL with the correct _s and _k or whatever..

Thanks in advance for any lead you can give me here.

--
_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside


_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside




--

_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside