Storing values in dataset

Posted by mflanegan on 11-Dec-2014 01:44

Hi there, 

I have a shopping cart that i would like to append items to. I need to have the dataset for this available through out my application as there are many places in the application where you can add items to the shopping cart. We are trying to avoid doing an appserver hit for each time we add an item to the cart and only when the user decides to save the shopping cart do we want to write those records to the database.

So my question is, how do I have my dataset (shopping cart) available globally on the client side to add/delete records whenever the user wants to and only save these records when they are ready? 

 Thanks in advance.

All Replies

Posted by Santosh Patel on 11-Dec-2014 01:48

How about using  localStorage? See here www.w3schools.com/.../html5_webstorage.asp  for more info.

You can use the localStorage object to store any json object, in this case your shopping cart.

Posted by mflanegan on 11-Dec-2014 03:50

I’m trying to store my JSDO into a local storage variable for use later in my app, this function is just a test to see if i can store it, retrieve it and later append to it:
 
function jsdoTest()
{
    var jsdo = WebService_beBasicSearch_JSDO.jsdo;
    var testObject = JSON.stringify(jsdo);
 
    // Put the object into storage
    localStorage.setItem('testObject', testObject);
   
    // Retrieve the object from storage
    var retrievedObject = localStorage.getItem('testObject');
   
    console.log('retrievedObject: ', JSON.parse(retrievedObject));
}
 
I’m getting the following error: Uncaught TypeError: Converting circular structure to JSON.
 
what am i doing wrong?
 

Meyrick Flanegan

Developer - Managed Services

Email: mflanegan@elcb.co.za

 

ELCB Information Services (Pty) Ltd

Customer Service Email  elcb@elcb.co.za · www.elcb.co.za

E A S T  L O N D O N

Tel: +27(43)  704 0700

Fax: +27(43) 704 0701

J O H A N N E S B U R G

Tel: +27(11) 879 6179

Fax: +27(11) 454 0384

P O R T  E L I Z A B E T H

Tel: +27(41) 373 0529

Fax: +27(86) 650 0135

Disclaimer


[collapse]
From: Santosh Patel [mailto:bounce-sapatel@community.progress.com]
Sent: 11 December 2014 09:49 AM
To: TU.Mobile@community.progress.com
Subject: RE: [Technical Users - Mobile] Storing values in dataset
 
Reply by Santosh Patel

How about using  localStorage? See here www.w3schools.com/.../html5_webstorage.asp  for more info.

You can use the localStorage object to store any json object, in this case your shopping cart.

Stop receiving emails on this subject.

Flag this post as spam/abuse.

[/collapse]

Posted by Santosh Patel on 11-Dec-2014 04:38

Note that object references are also not supported by JSON, but valid in JavaScript objects (which the JSDO is), so I guess storing the JSDO which may contain object references is not possible in localStorage which stores only strings. The JSON format does not support references, so it will need to follow the properties until an end is reached, but because an child points to its parent which has a list of its children, this is an endless loop, thats the reason why you get that error.

Working with JSDOs is not my area of  expertise but do you need to store the  JSDO as is or can it be processed into a JSON?

Adding [mention:98b1534299774239b816c694c6295179:e9ed411860ed4f2ba0265705b8793d05] for JSDO expertise.

Posted by egarcia on 11-Dec-2014 05:16

Hello Meyrick,

The jsdo object is a reference that points to Session object which also points to the JSDOs that it contains. Calling JSON.stringify() for the jsdo reference would give you the exception "Converting circular structure to JSON".

In this scenario, you actual want to get to just the data of the JSDO (jsdo.getData()).

You can call getData() to get to the data and then call addRecords() to populate the JSDO.

In release 3.1 of Mobile, we have added new APIs to the JSDO to save the data to local storage and then retrieve it.

The APIs are the following:

- saveLocal()

- readLocal()

- addLocalRecords()

- deleteLocal()

The methods use a default "local storage area" name but you can specify a parameter if you prefer to use a different name. By default all  the data in the JSDO (data and changes) are stored. There is a parameter that you can specify to only save the changes.

There are also support methods hasData() and hasChanges().

Here is how your could would look with the new APIs:

function jsdoTest()

{

   var jsdo = WebService_beBasicSearch_JSDO.jsdo;

   // Put the object into storage

   try {

       jsdo.saveLocal();

   }

   catch(e) {

       console.log("Exception while executing saveLocal();");

   }

   // Retrieve the object from storage

   try {

       var hasLocalData = jsdo.readLocal();

   }

   catch(e) {

       console.log("Exception while executing readLocal();");

   }

   console.log('retrievedObject: ', JSON.stringify(jsdo.getData()));

}

I hope this helps.

Posted by mflanegan on 11-Dec-2014 06:26

Once you have saved your jsdo to local storage and you want to read from it again it throws the Exception while executing readLocal() error.
 
Here im saving to local memory:
 
function jsdoTest()
{
    var jsdo = WebService_beBasicSearch_JSDO.jsdo;
    try
    {
        jsdo.saveLocal();
        console.log('retrievedObject save: ', JSON.stringify(jsdo.getData()));
    }
    catch(e)
    {
        console.log("Exception while executing saveLocal();");
    }
 
}
 
Here im trying to read from that local storage in a separate function to display my records into another datagrid on another page and the below causes an error.
 
function hasRecords()
{
   
    try
    {
        var hasLocalData = jsdo.readLocal();
        console.log('retrievedObject read: ', JSON.stringify(jsdo.getData()));
    }
   catch(e)
    {
        console.log("Exception while executing readLocal();");
    }
 
}
 
Its most likely because I don’t have the jsdo defined in the other fuction on the other page. How do you find that jsdo in the other function that is saved to local memory?
 

Meyrick Flanegan

Developer - Managed Services

Email: mflanegan@elcb.co.za

 

ELCB Information Services (Pty) Ltd

Customer Service Email  elcb@elcb.co.za · www.elcb.co.za

E A S T  L O N D O N

Tel: +27(43)  704 0700

Fax: +27(43) 704 0701

J O H A N N E S B U R G

Tel: +27(11) 879 6179

Fax: +27(11) 454 0384

P O R T  E L I Z A B E T H

Tel: +27(41) 373 0529

Fax: +27(86) 650 0135

Disclaimer


[collapse]
From: egarcia [mailto:bounce-egarcia@community.progress.com]
Sent: 11 December 2014 01:17 PM
To: TU.Mobile@community.progress.com
Subject: RE: [Technical Users - Mobile] Storing values in dataset
 
Reply by egarcia

Hello Meyrick,

The jsdo object is a reference that points to Session object which also points to the JSDOs that it contains. Calling JSON.stringify() for the jsdo reference would give you the exception "Converting circular structure to JSON".

In this scenario, you actual want to get to just the data of the JSDO (jsdo.getData()).

You can call getData() to get to the data and then call addRecords() to populate the JSDO.

In release 3.1 of Mobile, we have added new APIs to the JSDO to save the data to local storage and then retrieve it.

The APIs are the following:

- saveLocal()

- readLocal()

- addLocalRecords()

- deleteLocal()

The methods use a default "local storage area" name but you can specify a parameter if you prefer to use a different name. By default all  the data in the JSDO (data and changes) are stored. There is a parameter that you can specify to only save the changes.

There are also support methods hasData() and hasChanges().

Here is how your could would look with the new APIs:

function jsdoTest()

{

   var jsdo = WebService_beBasicSearch_JSDO.jsdo;

   // Put the object into storage

   try {

       jsdo.saveLocal();

   }

   catch(e) {

       console.log("Exception while executing saveLocal();");

   }

   // Retrieve the object from storage

   try {

       var hasLocalData = jsdo.readLocal();

   }

   catch(e) {

       console.log("Exception while executing readLocal();");

   }

   console.log('retrievedObject: ', JSON.stringify(jsdo.getData()));

}

I hope this helps.

Stop receiving emails on this subject.

Flag this post as spam/abuse.

[/collapse]

Posted by egarcia on 11-Dec-2014 06:53

Your guess is correct.

The variable "jsdo" in the code is set as a local variable of the function:

   var jsdo = WebService_beBasicSearch_JSDO.jsdo;

However, the variable WebService_beBasicSearch_JSDO.jsdo is available in the global scope.

You would either need to set "jsdo" again or use the following:

   WebService_beBasicSearch_JSDO.jsdo.readLocal();

Posted by mflanegan on 12-Dec-2014 00:21

Thanks Edsel. This sorted my issues out.
I’m am creating my dataset using this on the client side:
 
  var dataSet = {}; 
   dataSet.dsOrder = {};
   dataSet.dsOrder.ttOrder = [];
 
I don’t have a problem appending records to the dataset but how would I populate a grid using this dataset as I am not using business entities etc. to get my data back?
 

Meyrick Flanegan

Developer - Managed Services

Email: mflanegan@elcb.co.za

 

ELCB Information Services (Pty) Ltd

Customer Service Email  elcb@elcb.co.za · www.elcb.co.za

E A S T  L O N D O N

Tel: +27(43)  704 0700

Fax: +27(43) 704 0701

J O H A N N E S B U R G

Tel: +27(11) 879 6179

Fax: +27(11) 454 0384

P O R T  E L I Z A B E T H

Tel: +27(41) 373 0529

Fax: +27(86) 650 0135

Disclaimer


[collapse]
From: egarcia [mailto:bounce-egarcia@community.progress.com]
Sent: 11 December 2014 01:17 PM
To: TU.Mobile@community.progress.com
Subject: RE: [Technical Users - Mobile] Storing values in dataset
 
Reply by egarcia

Hello Meyrick,

The jsdo object is a reference that points to Session object which also points to the JSDOs that it contains. Calling JSON.stringify() for the jsdo reference would give you the exception "Converting circular structure to JSON".

In this scenario, you actual want to get to just the data of the JSDO (jsdo.getData()).

You can call getData() to get to the data and then call addRecords() to populate the JSDO.

In release 3.1 of Mobile, we have added new APIs to the JSDO to save the data to local storage and then retrieve it.

The APIs are the following:

- saveLocal()

- readLocal()

- addLocalRecords()

- deleteLocal()

The methods use a default "local storage area" name but you can specify a parameter if you prefer to use a different name. By default all  the data in the JSDO (data and changes) are stored. There is a parameter that you can specify to only save the changes.

There are also support methods hasData() and hasChanges().

Here is how your could would look with the new APIs:

function jsdoTest()

{

   var jsdo = WebService_beBasicSearch_JSDO.jsdo;

   // Put the object into storage

   try {

       jsdo.saveLocal();

   }

   catch(e) {

       console.log("Exception while executing saveLocal();");

   }

   // Retrieve the object from storage

   try {

       var hasLocalData = jsdo.readLocal();

   }

   catch(e) {

       console.log("Exception while executing readLocal();");

   }

   console.log('retrievedObject: ', JSON.stringify(jsdo.getData()));

}

I hope this helps.

Stop receiving emails on this subject.

Flag this post as spam/abuse.

[/collapse]

Posted by egarcia on 12-Dec-2014 04:52

Hello Meyrick,

Since you are using the JSDO, you can use the READ Service with readLocal = true to read from the JSDO memory and map to a grid.

You can use your existing READ Service and map the readLocal request parameter to a localStorage variable or create a new READ Service instance with readLocal = true in the parameters.

I hope this helps.

This thread is closed