I am working on this project and using Angular framework. I am pretty new to using this js framework. In the past I have been only working with pure js and jQuery. It is kind of web designer application for a niche market.
As the user can move between pages while designing, I want to maintain a session of all the changes he is making.
Now if the user signs in we load the session using data from db. And when the user clicks on save button we update the db with the session data. Someone told me that I can maintain session in Angular similar to backbone. Is it possible? If yes can you please direct me to a tutorial coz all I could find on the net is about directives and ui. If not possible what are my options.
Here is a kind of snippet for you:
app.factory('Session', function($http) {
var Session = {
data: {},
saveSession: function() { /* save session data to db */ },
updateSession: function() {
/* load data from db */
Session.data = $http.get('session.json').then(function(r) { return r.data;});
}
};
Session.updateSession();
return Session;
});
Here is Plunker example how you can use that: http://plnkr.co/edit/Fg3uF4ukl5p88Z0AeQqU?p=preview
You can also try to make service based on window.sessionStorage or window.localStorage to keep state information between page reloads. I use it in the web app which is partially made in AngularJS and page URL is changed in "the old way" for some parts of workflow. Web storage is supported even by IE8. Here is angular-webstorage for convenience.
You would use a service for that in Angular. A service is a function you register with Angular, and that functions job is to return an object which will live until the browser is closed/refreshed. So it's a good place to store state in, and to synchronize that state with the server asynchronously as that state changes.