Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintaining Session through Angular.js

I am working a project using the AngularJS framework. I am pretty new to using this framework; in the past I have only worked with pure JavaScript and jQuery. The project is a kind of web designer application for a niche market.

As the user moves between pages while designing I want to maintain a session of all the changes they are making.

Now if the user signs in we load the session using data from the database. When the user clicks on save button we update the database with the session data. Someone told me that I can maintain session in Angular similar to backbone. Is this possible? If yes, can you please direct me to a tutorial that does not focus on directives or UI? If this is not possible are there other viable options?

like image 989
Jehanzeb.Malik Avatar asked Feb 19 '13 12:02

Jehanzeb.Malik


1 Answers

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 */       $http.get('session.json').then(function(r) { return Session.data = r.data;});     }   };   Session.updateSession();   return Session;  }); 

Here is Plunker example how you can use that: http://plnkr.co/edit/Fg3uF4ukl5p88Z0AeQqU?p=preview

like image 53
Valentyn Shybanov Avatar answered Sep 18 '22 07:09

Valentyn Shybanov