I am building a mobile app using apache cordova and query mobile.
I have a single page in my app.
I have a form on the page that contains a list of checkboxes and 2 buttons.
The behaviour I need is the following.
When I select an item in the list and click on the 'Complete' button, I want the page/form to refresh. By refresh, I mean that I want to call a rest service to get a new list (the same rest service that I call when the page loads for the first time), construct an html list, and then display that in the form.
This is what I have tried
In the app.initialize I call a rest service using ajax to retrieve a list of items, construct a html list and then insert it into the form.
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
// CALL TO MY FUNCTION TO GET THE LIST OF ITEMS
myapp.loadItems();
},
// rest of the auto generated code.
};
As seen above I load the list by calling loadItems which calls the rest api. The code for this is given below.
loadItems: function() {
var url = 'http://localhost:8080/WebApp/rsapi/items';
$.ajax({url: url,
dataType: 'json',
success: function(data) {
var itemList = constructHtmlListItems(data);
$('#total-num-items').val(items.length);
$(itemsList).insertAfter( "#total-num-items" );
$('#items-list-form').trigger("create")
},
error: function(data) {
alert("Unable to connect to server.");
}
});
}
I have overridden the default form submit behaviour, so when I click on the button I use the onsumbit event on the form. I call the rest service, trigger a refresh. The problem is that the screen flickers, I see the new list for a brief moment and then I see the empty form with just the buttons on it.
The function that handles the form submit is shown below.
refreshPage: function() {
/*$.mobile.changePage(
window.location.href,
{
allowSamePageTransition : true,
transition : 'none',
showLoadMsg : false,
reloadPage : true
}
);*/
loadItems();
//$('#items-list-form').trigger("refresh")
//.trigger('create');
return false;
}
Please note in the above function you will see some commented code I have shown it here just to show you the various options I have tried.
What am I doing wrong?
The list shows up the first time I start the app. Thereafter when I click on the button I see is an empty form, with out the dynamically constructed list.
How do I fix this? How do I achieve a refresh?