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.
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.
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 |
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.
Flag this post as spam/abuse.
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.
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.
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 |
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.
Flag this post as spam/abuse.
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();
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 |
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.
Flag this post as spam/abuse.
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.