N.Marzio Posted April 20, 2020 Share Posted April 20, 2020 Can you tell me why he doesn't read me the localStore? function ReadLocalStorage(aKey : string): string; var lEdt : TUniEdit; begin lEdt := TUniEdit.Create(Application.MainForm); Try lEdt.Visible := False; lEdt.JSInterface.JSCallGlobal('localStorage.getItem', ['"' + lKey + '"']); Result := lEdt.Text; Finally lEdt.Free; End; end Link to comment Share on other sites More sharing options...
Sherzod Posted April 20, 2020 Share Posted April 20, 2020 22 minutes ago, marzio said: lEdt.JSInterface.JSCallGlobal('localStorage.getItem', ['"' + lKey + '"']); Result := lEdt.Text; Hello, How should this code work in your opinion? Link to comment Share on other sites More sharing options...
N.Marzio Posted April 20, 2020 Author Share Posted April 20, 2020 From the examples I found on the forum with this function I should get the key value on an edit field. My need is to value a variable by reading a key on the localStorage Link to comment Share on other sites More sharing options...
Sherzod Posted April 20, 2020 Share Posted April 20, 2020 33 minutes ago, marzio said: with this function I should get the key value on an edit field. This will not work. Link to comment Share on other sites More sharing options...
N.Marzio Posted April 20, 2020 Author Share Posted April 20, 2020 Thank you. So how can I read a key saved on the Locastorage? Link to comment Share on other sites More sharing options...
Sherzod Posted April 20, 2020 Share Posted April 20, 2020 Well, you must pass this from the client side (using ClientEvents, using specific JS events) to the server through ajaxRequest for example. Link to comment Share on other sites More sharing options...
N.Marzio Posted April 20, 2020 Author Share Posted April 20, 2020 Sorry, but I started using UniGui a few days ago ..... Could you give me an example of where to insert the javascript code. From Js I have to run: var lVal = localStorage.getItem ("lKey"); Thank you. Link to comment Share on other sites More sharing options...
Sherzod Posted April 20, 2020 Share Posted April 20, 2020 33 minutes ago, marzio said: Sorry, but I started using UniGui a few days ago ..... Could you give me an example of where to insert the javascript code. OK, Try this approach: 1. MainForm.ClientEvents.ExtEvents -> function window.boxready(sender, width, height, eOpts) { if (localStorage.getItem('lKey')) { ajaxRequest(sender, 'getKey', ['lKey=' + localStorage.getItem('lKey')]); } } 2. private { Private declarations } FLKey: string; 3. MainForm.OnAjaxEvent: procedure TMainForm.UniFormAjaxEvent(Sender: TComponent; EventName: string; Params: TUniStrings); begin if EventName = 'getKey' then begin FLKey := Params.Values['lKey']; end; end; Link to comment Share on other sites More sharing options...
N.Marzio Posted April 20, 2020 Author Share Posted April 20, 2020 how is OnAjaxEvent invoked? Tks Link to comment Share on other sites More sharing options...
Abaksoft Posted April 20, 2020 Share Posted April 20, 2020 6 minutes ago, marzio said: how is OnAjaxEvent invoked? Tks http://forums.unigui.com/index.php?/topic/12306-how-can-i-access-local-storage/&do=findComment&comment=65734 Link to comment Share on other sites More sharing options...
N.Marzio Posted April 20, 2020 Author Share Posted April 20, 2020 This would be my need: 1) When the application starts, I store an array with the settings of my forms on the localStorage. lKey = [ {"Form": 1, "Info1": "...", "Info2", ".."}, {"Form": 1, "Info1": "...", "Info2", ".."}. .... ] (OK, I managed to write on the LocalStorage) 2) On the creation of the forms I would like to read read the data saved on the LocalStogare to "prepare" the Form before viewing it. Is it possible to invoke OnAjaxEvent on the FormCreate? Thank you Link to comment Share on other sites More sharing options...
gerardocrisci Posted April 21, 2020 Share Posted April 21, 2020 maybe you can use this example of mine.. I created you a generic procedure (GetLocalStorage) that shows you all the values of localStorage and waits to read them in UniFormAjaxEvent. the procedure is started when the form is ready .. but you can also call it at the click of a button .. object MainForm: TMainForm OnReady = UniFormReady OnAjaxEvent = UniFormAjaxEvent type TMainForm = class(TUniForm) procedure UniFormReady(Sender: TObject); procedure UniFormAjaxEvent(Sender: TComponent; EventName: string; Params: TUniStrings); private { Private declarations } FLKey: string; public { Public declarations } procedure GetLocalStorage; end; procedure TMainForm.GetLocalStorage; begin UniSession.AddJS( ' var p = [], keys = Object.keys(localStorage), i = 0, key; '+ ' for (; key = keys[i]; i++) { p.push( key + "=" + localStorage.getItem(key)); } '+ ' ajaxRequest(MainForm.window, "getKey", p); ' ); end; procedure TMainForm.UniFormReady(Sender: TObject); begin FLKey:=''; GetLocalStorage; end; procedure TMainForm.UniFormAjaxEvent(Sender: TComponent; EventName: string; Params: TUniStrings); begin if EventName = 'getKey' then begin FLKey := Params.Values['lKey']; UniMemo1.Text := Params.Text; // <---- read all values end; I hope you find it useful .. Link to comment Share on other sites More sharing options...
leons Posted April 22, 2020 Share Posted April 22, 2020 Thx! Link to comment Share on other sites More sharing options...
N.Marzio Posted April 22, 2020 Author Share Posted April 22, 2020 It works! I would like something easy, but that's okay! Thank you! Link to comment Share on other sites More sharing options...
andyhill Posted June 15, 2022 Share Posted June 15, 2022 Is there a way to set a LocalStorage Key without going through a component interface ? "eDatabase.JSInterface.JSCallGlobal('localStorage.setItem', ['K0', s] );" LIKE THIS ---> JSCallGlobal('localStorage.setItem', ['K0', s]); Link to comment Share on other sites More sharing options...
N.Marzio Posted June 15, 2022 Author Share Posted June 15, 2022 Unfortunately, we have been waiting for management for some years simple of the localstorage ..... Link to comment Share on other sites More sharing options...
andyhill Posted June 15, 2022 Share Posted June 15, 2022 This is how I am doing it at present (using Form Interface), could someone please confirm if I am doing it correctly ? procedure TfLogin.UnimLoginFormCreate(Sender: TObject); begin try JSInterface.JSCallGlobal('localStorage.setItem', ['BB', 'now']); // TEST except end; Link to comment Share on other sites More sharing options...
andyhill Posted June 15, 2022 Share Posted June 15, 2022 I am now getting strange errors that do not make sense ??? Link to comment Share on other sites More sharing options...
Fred Montier Posted June 17, 2022 Share Posted June 17, 2022 On 6/15/2022 at 5:08 PM, N.Marzio said: Unfortunately, we have been waiting for management for some years simple of the localstorage ..... Next release of CodeInjector Package we will have a component to support LocalStorage read/write by item with macro substitution. At release 3, I'm make client side microtable with this feature with JSon interaction and server side up/down dataexchange. A nice solution to my final goal: a uniGUI PWA !😅 2 Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now