Page 1 of 1

how to navigate to a page when a json response list returns empty?

Posted: Thu Jun 13, 2013 8:30 pm
by John Herdean

hi, i have a json response with a list, and sometimes it will return an empty list with empty brackets:

[

]

And, when it does I want to navigate to another page. Im imagining that i have to run a JS to tell it that if the result has a list in its response proceed to load the response to a certain page, else navigate to a different page (or popup)

here's a snapshot of what my test results look like when I get a list:

Image

and heres a snapshot of what i get back when the list comes back empty:

Image


how to navigate to a page when a json response list returns empty?

Posted: Thu Jun 13, 2013 8:32 pm
by John Herdean

BTW: the cursor is hovering right before the first bracket...


how to navigate to a page when a json response list returns empty?

Posted: Thu Jun 13, 2013 8:45 pm
by Maryna Brodina

Hello! On service Success event check if response returns empty or not. All data is in variable "data". You can check it using the following code:

codeif (data.length == 0) {
// empty data. do something.
}/code


how to navigate to a page when a json response list returns empty?

Posted: Thu Jun 13, 2013 8:47 pm
by John Herdean

SO, let me be a little clearer because I tried to write the JS for this and cant find your code to invoke a service on page load.

If theres a list, I want a service to be invoked on a page when loading, if the list is empty then I want it to navigate to another page.
Thanks


how to navigate to a page when a json response list returns empty?

Posted: Thu Jun 13, 2013 8:51 pm
by John Herdean

i need the code to invoke a service when loading a page... That was my biggest issue actually. Im thinking I can even run the JS code directly on the page i want to load..


how to navigate to a page when a json response list returns empty?

Posted: Thu Jun 13, 2013 8:55 pm
by Maryna Brodina

You should invoke service first to see if response is empty. So you need to invoke the service on page Load, on service Success event run the code we suggested above. Instead of "line // empty data. do something." you can add navigation to page.


how to navigate to a page when a json response list returns empty?

Posted: Thu Jun 13, 2013 9:13 pm
by John Herdean

yes, perfect.. That did the trick. I tried to run the JS in 2nd step after invoking he service on page load. That just crashed. Running in success mode worked. This is what i did:

if (data.length == 0) {
Appery.navigateTo('no_active_loads', {
transition: 'slide',
reverse: false
});
}

Thanks so much Marina.