Simon Bauman
Posts: 0
Joined: Wed Oct 16, 2013 9:50 pm

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

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?

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

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

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.

Simon Bauman
Posts: 0
Joined: Wed Oct 16, 2013 9:50 pm

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

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

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

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

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

Simon Bauman
Posts: 0
Joined: Wed Oct 16, 2013 9:50 pm

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

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

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

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

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...

Simon Bauman
Posts: 0
Joined: Wed Oct 16, 2013 9:50 pm

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

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???

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

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

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.

Return to “Issues”