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

Jupiter Jones <[email protected]>
Newsgroups gmane.comp.lang.smalltalk.squeak.seaside
Message-ID <[email protected]>
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 <[email protected]> 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= <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.
> 
> -- 
> Mariano
> http://marianopeck.wordpress.com <http://marianopeck.wordpress.com/>
> _______________________________________________
> seaside mailing list
> [email protected]
> http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside

_______________________________________________
seaside mailing list
[email protected]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.