Jump to content

Read LocalStorage


N.Marzio

Recommended Posts

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

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

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

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

  • 2 years later...

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

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

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
Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...