How to get inital state of the sapui5 app without reopening - sapui5

My 'create new User' app can only create one new user. If I want to create an additional user I have to restart the app. What I want to do is, create some button which does this task without restarting the app.
To be more specific, after the initial call of the app I create a new user (include save). The user is stored in the back-end and still visible on the front-end (so I can further edit the user). In case I want to create an additional user, I want to push some button and the view will be restarted/reloaded (and also the Model). Eventually I want the inital state of my app. Then I want to be able to create the next new user (include save) and maybe another one.
What I already tried:
'CrossApplicationNavigation' to my 'create new user' app without any
parameters. It works only the first time, because when I push the
button the second time nothing happens. The URL stays the same (no parameters are changing).
Deleting data of the Model and subsequently calling the oninit()
function. But I get problem with refilling the model.
Is there some function or something else I can try?
As descripted in comment (component.js):
init: function () {
//set model
this.setModel(models.createTableModel(this), "table");
if (this.getComponentData().startupParameters.ID) {
var sID = this.getComponentData().startupParameters.ID[0];
if (sID !== "") {
this.getModel("table").setProperty("/ID", sID);
} else {
this.getModel("table").setProperty("/ID", "");
}
}
}

I think, you're using a local model? Then you'll have to reset your local model. Reset your local model in onInit function and on pressing button "Add new user".
onInit: function(){
this.oLocalModel = this.getModel("myLocalModel");
this._resetModel(this.oLocalModel);
},
_resetModel: function(oModel){
oModel.setData();
},
onPressAddNewUser: function(){
this._resetModel(this.oLocalModel);
}

Related

Flutter: Calling a function once in a lifetime of the flutter app

I want to call a function when app runs for the first time, which will ask the user to enter firstName , lastName and add a profile picture.
Once the above process is done the function will never again be called during the lifetime of the app.
For something like this you need to check if the user has already entered that data or not. If not then show him the page where he can enter information otherwise take him to HomePage. For this When the user enters the information you need to save it to some persistent storage and check it whenever the app runs. In this way, your function will be called only once until the user deletes the app or clear its memory. You could use the following libraries to store the data.
Hive,
Shared Preference
These libraries save the data in key-value pair and read data faster especially hive.
Use SharePreference
see below code snippet with little advancement in code you can achieve your result.
Future<bool> isFirstTime() async {
var firstTime = SharedPref.pref.getBool('first_time');
if (firstTime != null && !firstTime) {
SharedPref.pref.setBool('first_time', false);
return false;
} else {
SharedPref.pref.setBool('first_time', false);
return true;
}
}

How to update ion-toggle status real in time?

I have an ion toggle in my application that stores his status in a firebase Database, is there a way to change his status with the data loaded from firebase?
For example, if in the database the status is true, then when I load the view the toggle will be on. I've tried using checked:
<ion-toggle checked="status()" >
where status() returns a boolean.
But due to the async function of firebase, the view loads 1st before the value in status(). Can't find a solution to this problem so far, I'd apreciate any help.
Yes there's a way to do this, but using a function in your attribute is no good. When using a function in the DOM that returns a value you'll make that function executes every time there's a change in the DOM, so since this is a function that fetchs something on Firebase you'll make your user do an request to Firebase every time the DOM is updated.
A good way to do this is storing the Firebase result in a variable. you can do like this when you enter de page:
public myStatus: boolean;
// USING A LIFECYCLE HOOK
ionViewWillLoad(){
firebase.database().ref(`Path/To/Your/Status`).once('value', snapshot => {
this.myStatus = snapshot.val();
});
}
And in your HTML
<ion-toggle [checked]="myStatus">
<!-- You need to bind your checked attribute to myStatus, using checked="{{myStatus}}" also works -->
If you need to always check if your status has changed you can also create and observable on firebase, so if your status change you can change your toggle:
ionViewWillLoad(){
firebase.database().ref(`Path/To/Your/Status`).once('value', snapshot => {
this.myStatus = snapshot.val();
});
firebase.database().ref(`Path/To/Your/Status`).on('child_changed', snapshot => {
this.myStatus = snapshot.val();
});
// YOU'LL NEED BOTH SINCE THE FIRST LOADS THE STATUS FOR THE FIRST TIME AND THE SECCOND CREATES AN OBSERVABLE FOR WHEN THE STATUS CHANGE.
}
Hope this helps.

form submission with jquery

When submitting a form, I tried to submit the form by using the id and call submit on it by JQuery, but offcourse, the first script gets me in an infinite loop.
After using plupload, I noticed that they use another way to submit the form, which doesn't cause an infinite loop.
I tried to find information about this, but I cannot seem to get specific information about this?
$('#test').submit(function(e) {
alert("here");
$('#test').submit();
return false;
});
$('form').submit(function(e) {
alert("here");
$('form')[0].submit();
return false;
});
Begin edit
In first case you are triggering the jQuery submit event on your jQuery object (see http://api.jquery.com/submit/)
In second case, you are calling the javascript submit method on a javascript object. It does not trigger the submit event.
It is not the same 'submit' in both case.
In your second case, if you replace :
$('form')[0].submit();
with
$($('form')[0]).submit();
you will also end up with an infinite loop.
End edit
in fact, with plupload, the point is waiting for the time when you will be able to successfully submit the form. The main feature is subscribing to StateChanged event.
It works this way :
1- first check if the queue is empty. If so, validate form submission(return true)
2- if the queue is not empty
a- startuploading remaining files
b- subscribe to future 'StateChanged' events where it will check the queue. If it is empty at that time, goto step 1 that will pass successfully (because the queue will be empty at that time)
c - cancel form submission (the queue is not currently empty)
Some code with comments :
// Client side form validation
$('form').submit(function(e) {
var myForm = e.currentTarget;
var uploader = $(item).pluploadQueue();
// if files in queue upload them first. Cancel current form submission.
// Subscribe to 'StateChanged' to check if the queue is empty and try
// a new submission, each time a file is finished uploading
if (uploader.files.length > 0) {
// When all files are uploaded submit form
uploader.bind('StateChanged', function() {
if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
myForm.submit(); //
}
});
// start uploading remaining files.
// End of each upload will trigger the 'StateChanged'
uploader.start();
// there are files in queue. (the reason we are running current block)
// Cancel current form submission
return false;
}
return true; // the queue is empty, validate current form submission
});
Hope this will help

GWT - Trouble with History first token

I have this problem : when i call the Content class (the one who decide which page to view, due to the #param) I do somethings like this :
History.addValueChangeHandler(this);
if(!History.getToken().isEmpty()){
changePage(History.getToken());
} else {
History.newItem("homepage");
}
So, now, if i look at browser's navigation bar, i see http://localhost:8084/GWT/?gwt.codesvr=127.0.0.1:9997#homepage. And that's right. Unfortunatly, If i press Back on my browser, i see that it load the previous address, such as http://localhost:8084/GWT/?gwt.codesvr=127.0.0.1:9997
I have a sort of "fake" page at the beginning.
1 - How can I fix it? And start the application with a default token, or remove it in the history. Or just call the onValueChange method when there is empty token, and after decide the workflow with a sort of switch/if-else.
2 - As related question, when i call History.addValueChangeHandler(this); in the costructor class, netbeans say "Leaking this in constructor". What it means?
Cheers
Maybe you forgot to add History.fireCurrentHistoryState(); to end of onModuleLoad() method?
You need to set a history token and fire the history change event with current token.
Heres how you could do it:
/ If the application starts with no history token, redirect to a new
// 'homepage' state.
String initToken = History.getToken();
if (initToken.length() == 0) {
History.newItem("homepage");
}
// Add widgets etc
// Add history listener
History.addHistoryListener(yourHistoryHandler);
// Fire the initial history state.
History.fireCurrentHistoryState();
IMHO, home url in form of "proto://hostname#homepage" is ugly :)
1. Just a suggestion:
String token = History.getToken();
String page = token.isEmpty() ? "homepage" : token;
changePage(page);
2. Does Your EntryPoint implement ValueChangeHandler<String>?

Problem in Large scale application development and MVP tutorial

I recently tried to follow the Large scale application development and MVP tutorial. The tutorial was great but I am having a hard time with a few things.
If you try and add a contact to the list, the contact is created. If you try and add another contact, you are taken to the edit screen of the last contact you created. No more contacts can be added once you add your first contact. What needs to be changed so you can add more than one contact.
Changes I have made to try and get it to work:
Create a new editContactsView each time the add button is pressed. This brings up a blank edit screen, but the new contact still overwrites the previous addition.
Changed contacts.size() to contacts.size()+1 when determining the ID of the new contact.
Actually, there are a couple of problems (from what I can see):
like Lumpy already mentioned, the new Contact created via EditContactPresenter doesn't get an id assigned (it's null). This is because EditContactPresenter uses the default Contact() constructor which doesn't set the id. There are many possible solutions to this: add setting the id in the default constructor (so that you don't have to keep track of the ids somewhere else in the app), delegate that function to your server (for example, make your DB generate the next available id and send it back) or just add a contact.setId(whatever); in the appropriate place in EditContactsPresenter
AppController.java:134 - this example reuses the view (which is a good idea), but it doesn't clear it if you use it for creating a new Contact. Solution: either disable view reusing (just make a new EditContactsView every time) or add a clear() or sth similar to your Views and make the Presenters call it when they want to create a new entry, instead of editing an exisiting one (in which case, the values from the current entry overwrite the old values, so it's ok).
It's weird that this sample was left with such bugs - although I understand that it's main purpose was to show how MVP and GWT go together, but still :/
When a new contact is added it's id is never set. Because the id field is a string it is stored as "". That is how the first contact is added. Now every time you create a new contact you overwrite the contact with key "". To fix this you need to set the value of the id. I did this by changing the doSave method in EditContactsPresenter.
private void doSave() {
contact.setFirstName(display.getFirstName().getValue());
contact.setLastName(display.getLastName().getValue());
contact.setEmailAddress(display.getEmailAddress().getValue());
if(History.getToken.equals("add")
rpcService.updateContact(contact, new AsyncCallback<Contact>() {
public void onSuccess(Contact result) {
eventBus.fireEvent(new ContactUpdatedEvent(result));
}
public void onFailure(Throwable caught) {
Window.alert("Error updating contact");
}
});
else
rpcService.updateContact(contact, new AsyncCallback<Contact>() {
public void onSuccess(Contact result) {
eventBus.fireEvent(new ContactUpdatedEvent(result));
}
public void onFailure(Throwable caught) {
Window.alert("Error updating contact");
}
});
}