Page 1 of 1

How do I set and reference non-local (dynamic or global) variables in service request fields?

Posted: Fri Oct 18, 2013 8:44 pm
by Simon Bauman

How do I set and reference non-local (dynamic or global) variables in service request fields?

Example:
I want to include the current date/time to the 'where' field request parameter for a service.

I've tried:

  • Adding variables into the, "Add JS" text area (button next to the "where" request field) parameter, then trying to reference in the text area.

  • Creating a new javascript project file, setting variables, then trying to reference in the "where" field.

    Any suggestions?


How do I set and reference non-local (dynamic or global) variables in service request fields?

Posted: Fri Oct 18, 2013 9:45 pm
by Maryna Brodina

Hello!
[quote:]Creating a new javascript asset, setting variables, then trying to reference in the "where" field[/quote] this way you can create global variable, but global variables are missing while page refreshing or navigating between screens with full screen refresh. Also local variable with the same name rewrites global one. It's better to use localStorage for this purpose.


How do I set and reference non-local (dynamic or global) variables in service request fields?

Posted: Fri Oct 18, 2013 10:02 pm
by Simon Bauman

Maryna,

So if I put this in a javascript asset page:
var d = new Date();

...and I want to reference the 'd' variable in the "where" field of a request parameter, I could do this?:

{"start": {"$lte":"'+ d +'"}

...this doesn't seem to work


How do I set and reference non-local (dynamic or global) variables in service request fields?

Posted: Fri Oct 18, 2013 11:39 pm
by Illya Stepanov

Hi Simon, the better solution is to use localStorage in this case, as Maryna suggested you.


How do I set and reference non-local (dynamic or global) variables in service request fields?

Posted: Fri Oct 18, 2013 11:56 pm
by Simon Bauman

So would I create a localStorage variable called, "now", then populate it on the page load with the javascript Date() function in order to use the "now" variable in the "where" (query) field? ...a little walkthrough or reference to an example would go a long way to help.

Image


How do I set and reference non-local (dynamic or global) variables in service request fields?

Posted: Sat Oct 19, 2013 12:34 am
by Illya Stepanov

You describe it very right and if you try it what will be the result?
How to use localStorage reference is here ::
http://docs.appery.io/documentation/u...


How do I set and reference non-local (dynamic or global) variables in service request fields?

Posted: Sat Oct 19, 2013 6:04 am
by Simon Bauman

The result is that it doesn't work.

1) I created a local variable and mapped it to the "where" request field.

2) I clicked, "Add JS" button next to the "where" request field

3) In the javascript text area I tried entering the following (note the double quite escape elements):

var query = "{start: {\x22$gte\x22:\x222013-10-18 20:00:00.000\x22}}";
return query;

...with plain javascript, the return is: {start: {"$gte":"2013-10-18 20:00:00.000"}}

... in the service test area, when I enter the start string (above) into the "where" field, I get back the expected results.

This may be some nuance to javascript that I'm missing but the fundamental question is this: How do I get the current Date(); into the "where" field???


How do I set and reference non-local (dynamic or global) variables in service request fields?

Posted: Sat Oct 19, 2013 6:26 am
by Alena Prykhodko

Hi! Please try the following JS:
pre
Service_name.execute({ "start" : {"where": '{"$gte":"2013-10-18 20:00:00.000"}'}});/pre

Service_name - service name where data should be passed.


How do I set and reference non-local (dynamic or global) variables in service request fields?

Posted: Sat Oct 19, 2013 7:54 am
by Simon Bauman

Put this in the "Add JS" screen?


How do I set and reference non-local (dynamic or global) variables in service request fields?

Posted: Sat Oct 19, 2013 7:59 am
by Alena Prykhodko

Yes