John Herdean
Posts: 0
Joined: Tue May 14, 2013 3:56 am

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

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

John Herdean
Posts: 0
Joined: Tue May 14, 2013 3:56 am

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

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

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

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

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

John Herdean
Posts: 0
Joined: Tue May 14, 2013 3:56 am

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

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

John Herdean
Posts: 0
Joined: Tue May 14, 2013 3:56 am

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

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

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

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

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.

John Herdean
Posts: 0
Joined: Tue May 14, 2013 3:56 am

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

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.

Return to “Issues”