define([ "dojo/Deferred", "dojo/Evented", "dojo/_base/declare", "dojo/_base/json", "dojo/_base/lang", "dojo/request" ], function(Deferred, Evented, declare, json, lang, request) { var Session = declare([Evented], { info: null, get: function() { return this.info; }, restore: function() { return request('/api/login', { method: "GET", handleAs: "json" }).then(lang.hitch(this,function(res) { return this._set(res); }), lang.hitch(this,function() { return new Deferred().reject(this._set(null)); })); }, login: function(username, password) { return request('/api/login', { method: "POST", handleAs: "json", data: { username: username, password: password } }).then(lang.hitch(this,function(res) { return this._set(res); }), lang.hitch(this,function() { return new Deferred().reject(this._set(null)); })); }, logout: function() { return request('/api/logout', { method: "POST", handleAs: "json" }).then(lang.hitch(this,function(res) { return this._set(null); }), lang.hitch(this,function() { return this._set(null); })); }, _set: function(newInfo) { if (newInfo !== this.info) { this.info = newInfo; this.emit('change', this.info); } return this.info; } }); return new Session(); });