source: Dev/trunk/src/client/qed-client/session.js @ 494

Last change on this file since 494 was 494, checked in by hendrikvanantwerpen, 11 years ago
  • Removed all Coffeescript from codebase (build process is still there).
  • Nicer message on failed login.
  • Own all event subscriptions from widgets.
  • Update objects in _ObjectPage with invalid info too, or our refresh will overwrite what user did.
File size: 1.8 KB
Line 
1define([
2    "dojo/Deferred",
3    "dojo/Evented",
4    "dojo/_base/declare",
5    "dojo/_base/json",
6    "dojo/_base/lang",
7    "dojo/request"
8], function(Deferred, Evented, declare, json, lang, request) {
9    var Session = declare([Evented], {
10        info: null,
11        get: function() {
12            return this.info;
13        },
14        restore: function() {
15            return request('/api/login', {
16                method: "GET",
17                handleAs: "json"
18            }).then(lang.hitch(this,function(res) {
19                return this._set(res);
20            }), lang.hitch(this,function() {
21                return new Deferred().reject(this._set(null));
22            }));
23        },
24        login: function(username, password) {
25            return request('/api/login', {
26                method: "POST",
27                handleAs: "json",
28                data: {
29                    username: username,
30                    password: password
31                }
32            }).then(lang.hitch(this,function(res) {
33                return this._set(res);
34            }), lang.hitch(this,function() {
35                return new Deferred().reject(this._set(null));
36            }));
37        },
38        logout: function() {
39            return request('/api/logout', {
40                method: "POST",
41                handleAs: "json"
42            }).then(lang.hitch(this,function(res) {
43                return this._set(null);
44            }), lang.hitch(this,function() {
45                return this._set(null);
46            }));
47        },
48        _set: function(newInfo) {
49            if (newInfo !== this.info) {
50                this.info = newInfo;
51                this.emit('change', this.info);
52            }
53            return this.info;
54        }
55    });
56
57    return new Session();
58});
Note: See TracBrowser for help on using the repository browser.