Loose data on refresh page Angular js - javascript

I have an AngularJS app like this one:
http://next.plnkr.co/edit/Wtkv71LIqUR4OhzhgpqL?p=preview&preview
and the $scope data that I have, is from an API,
When the page first loaded I call to this API and I save the data that returned in the session,
Now I can't understand why when I refresh the page I Losing information and all the data,
How can I use the data dynamic that when I refresh the page I don't loos data?

First of all, are you using routing in your app? Here is simple example :
http://next.plnkr.co/edit/Svg4Po13hMq7WxzNwKDc?p=preview&preview
With routing you do not need to refresh your page.
As to the problem itself (don't loss data after refresh):
if you want to keep your data even when you refresh your browser use localStorage. There're lots of handy localStorage angularJS modules to use - ex.
https://github.com/grevory/angular-local-storage
if you don't want to store data on the client side, you could request your API for the data you need to store each time your angular app is getting loaded. This data seems to be static, as you are storing it on client side and reuse then, so you could configure API endpoint to throw this data really fast.

Related

Ember Mirage persist data

Currently, when I delete or change some records (model), I can see HTTP requests that intercepted by Mirage, and data on page really changes. But after page reload - it returns back. Is it possible to configure Mirage somehow to avoid rollback data after page reload, and allow me to do it manually?
I suspect your issue is that Ember Mirage is being reloaded on the page reload (which makes sense).
You will want to create your own server that your frontend will connect to that maintains fake/mock responses and can persist the data. You can use something like fake-server or MockServer or just write your own code in Node/Ruby/Python/whatever language that fakes the REST API for your Ember app. This is the only way to maintain persistent data since your Ember app is short-lived within a browser.

Persist data in same window even if url changes

I have graph with data in welcome page like widget(/welcome). when the user clicks the graph the page change to /home/default and the same graph should be displayed along with some extra data which is populated by Ajax call. What I want is to persist the graph data from /welcome into /home/default page. I don't want the data to go controller and back to the /home/default page. Kindly suggest.
In a nutshell, you need to set some state for the user and then when the /home/default page is rendered, you need to check that state and make corresponding changes to the presentation of the page.
This can be done either server-side (in the rendering of the page) or client-side (via Javascript adding things to the page).
If you do this client-side, then the state can be stored in a cookie, in LocalStorage or in a query parameter in the URL when you redirect. Then, you place Javascript code into /home/default that checks that state and adds content to the page dynamically based on the state.
If you do this server-side, then the state can be stored in a cookie or in some server-side data store and then when the /hoome/default page is rendered, your server side page rendering process can check the state for this particular user and modify the rendering of the page to include the desired content.
You have a plethora of options. The best solution depends on how your application is currently implemented -- whether in a framework or not, with sessions or not, etc. The principle whatever method you choose is almost identical: store a value and then retrieve it later.
Single Page Application (SPA)
If you aren't already using a framework, I would urge you to consider migrating to one as tasks like these are made infinitely more elegant in their implementation.
Service / Data Store
If you are building an SPA then you may not have to consider any of the options below... so long as it doesn't matter if the data is lost if the user performs a 'real' navigation that cannot be intercepted by the framework (for example, refreshing the page).
In Angular you can maintain a temporary data store in the form of a service. Simply store the data and then pick it up later from another controller. Similar functionality can be achieved in all other popular SPA frameworks:
Angular
Ember
React
Local Storage
Local Storage is available in IE8 and above and has a really simple API.
Angular: angular-local-storage
React: react-local-storage
Ember: ember-local-storage-adapter
jQuery: jStorage
IndexedDB
If you're into the cutting edge and aren't tied down by browser support, consider using IndexedDB. I don't recommend using this unless you are wanting to persist large amounts of data remotely on the client's machine. (It really does have bad support at the moment.)
Angular: angular-indexed-db
React: ???
Ember: ember-indexeddb-adapter
jQuery: jquery-indexeddb
Cookies
If your application is inflexible then cookies will be the easiest and least time-consuming. However Local Storage may be a contender.
Angular: $cookie service
React: react-cookie
Ember: ???
jQuery: jquery-cookie

Persist large amounts of data in AngularJS

I have a really big angularjs web application which loads large files when you first enter the site. The site is composed by a few SPAs, and every time the user change from one to the other this data is lost and has to be loaded again. Also, if the user presses refresh the data is also loaded again.
This data will very unlikely change during the user's session, so it would be great if we could persist it and reload only when there's an update. I know about local and session storage, but they have a 5000k or 10000k limit, which won´t be enough. I would need aroung 20000k storage.
Is there any way to do that?
We had the same issue a couple of months ago and found no valid solution. We used two work arounds:
Use one SPA (put the various SPAs in a Super SPA) . We had the chance to do so. Don' know if that is an option for you. The module ui-router which routes using state instead of urls might be helpful.
We used paginated data to reduce the size of initial loads.
If you need this huge amount of data to compute e.g. statistics in the browser or rendering graphics, consider to move this computation to the backend.
You can also use the http service to load the data and cache it. But this is only an option, if the data is not changed while the session is active.
Yes and no.
You can use server side code to inject javascript to your page in many different ways, so you will not have to make a ajax request to the server to get the data.
For example if you use php you can do something like
<?php
$script = "<script> var Data = $data;</script>";
echo $script;
?>
Then inside your control do something like
$scope.data = Data;
But browser is not built to handle big data at once. If your data is bigger than 5MB you are going to have a very bad user experience and your app will crash. So instead you should organize your data in a way that all of your data aggregation will be handled at the server side and you will send small package to the client.
Maybe you can create a service that uses $http to load the data asynchronously and set the cache parameter to true, as explained here: How to cache an http get service in angularjs

How can I write data to and update HTML5 cached web pages while working offline?

In the application I am writing, a user captures information about a person via an online form. When they have completed the form they save their work, repeating this process several times in a session. When they hit 'Save and End Session' they are returned a list of the several person instances they have just saved, all data being saved to a server.
I wish to replicate this functionality in an offline app. Using HTML5 I understand how to cache pages, and how store the JSON form data in localStorage using raw Javascript (or perhaps Angular.js cache).
But is it possible to dynamically update cached webpages with cached data while offline? how, for example, can I write the the cached form data to a cached copy of the list page, updating that page with the data just produced, all during the offline session?
I cannot find an answer to this one. All suggestions are much appreciated!
If I understood this correctly, you want to dynamically update the html view while offline.
If you are using Angular, this is pretty simple.
You just have to cache also the JS controller, not only the html file (set it in the cache.manifest). The page will have the same functionality as the online app then. But if you want to send the stored offline data back to the server when offline, you can write a simple code that will:
Save the parameter in localStorage, which will mark if the data was saved while running online/offline app (you can recognize onine/offline by sending AJAX request to an existing part of the app, which is not available offline (so not cached one))
When app runs then in online mode, it will collect all the data stored offline and send it to the server

How do you send payload data with initial request?

Lets say, for example, we have a page that has a table with list of users. I could render the HTML with the table of users, but that isn't very friendly with Angular. I can also AJAX back to the server for the data, but then I have to wait for the initial page to render and make an additional HTTP request, which adds additional delay.
Is there a way to send my payload data along with my initial request so I don't have to AJAX for the data and I can still use angular binding (e.g. ng-repeat)?
The only way to access server data without executing Javascript is to have the server include that data in the initial request for the web page. This would require using a server side language such as PHP, Ruby, or ASP.NET that can query the data, add it to your web page, and send the updated web page in it's inital http resonse.
However, this concept completely undermines the purpose of using a client side framework such as AngularJS. Angular wants to be in charge of your data access, and I would recommend letting it be. Angular doesn't know about Data that it didn't retrieve, so you are going to have to also dynamically modify your page scripts to tell Angular about the data that the server retrieved. This is all going down a dark road that you do not want to go down. you will simply be doing double work.
If your concern is that your users will be see a brief delay between the page load and the initial data loading, you even have the option to keep the screen blank until the initial data loads using things directives like ng-cloak and ng-bind. If you are concerned about the delays in retrieving data after the initial page load, remember that a full-page HTTP request and full page reload takes much longer than an ajax call that only updates a single part of your page.
If you don't want to be using Ajax calls, you don't want to be using AngularJS.

Categories