i have some problem, I'm creating apps for commerce, and the cart is work, but when I'm reloading the pages, the session is gone, cart, and other.
here's my code
//show product page
$scope.showProductInfo=function (id,desc,img,name,price) {
sessionStorage.setItem('product_info_id', id);
sessionStorage.setItem('product_info_desc', desc);
sessionStorage.setItem('product_info_img', img);
sessionStorage.setItem('product_info_name', name);
sessionStorage.setItem('product_info_price', price);
window.location.href = "/#/page8";
};
//add to cart function
$scope.addToCart=function(id,image,name,price){
cart.add(id,image,name,price,1);
};
how can i save the data to session or cookies, like php did, so if the reloading page, the session not destroying/ session_destroy();
hope somebody care enough to help me
Move cart management into service - so you will get same data from any controller.
When service is initialized - try to load from local storage (if not - create empty cart)
Any change of cart must update localstorage
Related
I am creating a shopping cart.
Basically, when I reload the page with the products in my cart, the local storage doesn't refresh, which is what I want.
But I also want it to refresh at some point only (when I open another page). How do I get the local storage to refresh completly?
localStorage.clear()
The clear() method of the Storage interface clears all keys stored in a given Storage object.
for example
//feeds storage
localStorage.setItem('foo', 'bar');
localStorage.setItem('john', 'wick');
localStorage.setItem('joe', 'doe');
//lets get the value bar from the localStorage
console.log(localStorage.getItem('foo'))//outputs 'bar'
//clears all data
localStorage.clear();
//now if you try to do it again after you clear it will print null
console.log(localStorage.getItem('foo'))//outputs null
You can use session storage if you want to delete data anytime user closes the tab, localStorage does not refresh on page refresh or anything like that, what you have to do is use setItem on same key you were using before and pass new or updated data(data you want to change)
localStorage.setItem("same-object-key-you-have-used-before",data);
by doing this data(updated one) will be stored in localStorage
I have below scenario
Page 1 has a link, when user clicks on it, it gets navigated to portal page with page reload. So just before navigation, a JSON object is created
The size of this object comes around 4KB roughly.
Sample object
let obj = {
"date":"12/31/2018",
"year":"2019",
"zip":"93252",
"members":[
{
"sdf":true,
"age":21,
"fdssss":false,
"aaaa":false,
"fdss":null,
"fsdfsd":[
"ADULT"
]
},
{
"sdf":true,
"age":21,
"fdssss":false,
"aaaa":false,
"fdss":null,
"fsdfsd":[
"ADULT"
]
}
}
There is a back link from that portal page, on clicking page will be navigated back to Page 1 with a page reload.
So when the page is navigated back, I need the created JSON object back again. I need it only for that session or the data should be persistent even if the page is reloaded.
Do I have to use localStorage? If i store the object in localStorage, at what point i should clear the storage? How should I handle between different users?
Do I have to use sessionStorage? what will be the scope of the data availability
I'm using AWS service.
Q1:
you can have localStorage, and you should handle it at the code when first page loaded and you can delete it when user do signout or login, storage is about browser not user, if there are some users behind one computer at different times you must clear all data manually.
Q2:
you can also have sessionStorage, per tab and will be removed by closing browser.
in details:
This is depends on your scenario which means localStorage used for long time but sessionStorage used when you need to store something temporary.
but the important thing about sessionStorage is that it is exist per tab if you close tab and windows the sessionStorage completely removed, it used for critical data such as username and password whereas localStorage is used to shared data whole the browser.
localStorage has no expiration date, and it gets cleared only by code, or clearing the browser cache or locally stored data whereas sessionStorage object stores data only for a session, meaning that the data is stored until the browser (or tab) is closed.
at the end I suggest you to use localStorage because you may want to share that data whole the browser event after closing browser and you can store more data, in the other side there are limitation about them, when you are used storage you should handle them manually and take care.
suppose:
function removeStorage()
{
var obj = localStorage.getItem('obj');
if(obj !== null)
localStorage.removeItem('obj')
}
and in login or logout success action call removeStorage() and in Page1 load have something like below:
var obj = localStorage.getItem('obj');
if(obj !== null)
{
....
//show the obj in label or do what you want with it
...
}
Anyone provide me way, I can use HTML5 LocalStorage to use in Javascript to maintain session between server and client to keep track of session Timeout which will redirect the all tabs or pages in session Expired page.
Is there any way(Other than this) i can maintain session across the page/tabs under same domain to keep track of Session Timeout.
Thanks in Advance
You have not posted any code so the only think I can do for you is just show you a generic example which may not completely suit your requirement but will give an idea on how you could do it:
// show last settings pane if it's set
if ( localStorage.activePill ) {
$('.nav-pill-control > li').removeClass('active');
$('.nav-pill-control > li > a[href="' + localStorage.activePill + '"]').parent().addClass('active');
$('.nav-pill-pane').hide();
$(localStorage.activePill).show();
}
The sessionStorage object is equal to the localStorage object, except that it stores the data for only one session. The data is deleted when the user closes the specific browser tab.
In HTML5 you can use session to pass object from page to another:
// Save data to sessionStorage
sessionStorage.setItem('key', 'value');
// Get saved data from sessionStorage
var data = sessionStorage.getItem('key');
// Remove saved data from sessionStorage
sessionStorage.removeItem('key')
More Information
I'm new to meanjs, I just created a new module for adding products. These products are displayed in the home page. But the display in home page is not getting updated real time. I just added new product in one tab, and the products list in the other tab need to be refreshed to see the change. How can this be done at real time ?
Edit:
By updation I meant is, when ever a new record is been added to database, the product display should update in realtime. Now I need to refresh the page to see the newly added product.
My code is
Client
$http.get('/latestproducts').
success(function(data, status, headers, config) {
$scope.latestproducts = data;
})
Server
exports.getlatestProducts = function(req, res) {
Product.find().sort('-created').populate('user', 'displayName').exec(function(err, products) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(products);
}
});
If you mean browser tabs, mean.js won't do if fo you. You can use sockets to inform server that changes were made and then broadcast message to all active tabs to refresh data. You can also try window.blur/window.focus events to reload data.
If you have list of products and product form on the same page, you have 2 options:
add saved item to your local collection after you save poduct and recive success message from server.
Update local collection(get list of objects from the server) after you save poduct and recive success message from server.
I just released angular-socket-resource. Once you use a service instead of performing the http request manually, you can use that to automatically listen for socket.io updates that will update your local data.
I am making a website that has a shopping basket feature and I am using the session to store the shopping cart. When I navigate through the site it works fine and displays the number of items in the basket, but when I click on a link that is create by a javascript function it loses the variable on the session. It only causes this problem on the links generated by javascript
Adding item code
if (context.Session["jobBasket"] == null)
{
context.Session.Add("jobBasket", new System.Collections.ArrayList());
}
var list = context.Session["jobBasket"] as System.Collections.ArrayList;
var item = int.Parse(context.Request["jobId"]);
if (!list.Contains(item))
{
list.Add(item);
}
Are sessions even enabled for your app? for instance - if you have a single page that reads, updates, and prints out the value - does it stay? if not your session is potentially disabled or configured odd, check your web.config