Jump to content

Recommended Posts

Posted

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

Posted
22 minutes ago, marzio said:

lEdt.JSInterface.JSCallGlobal('localStorage.getItem', ['"' + lKey + '"']);
    Result := lEdt.Text;

Hello,

How should this code work in your opinion?

Posted

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

Posted

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.

Posted
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;

 

Posted

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

Posted

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

  • 2 years later...
Posted

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]);

 

Posted

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;
 

Posted
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 !😅

  • Like 2

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...