Skip to content

Commit

Permalink
make a singleton TeachAPI instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
toolness committed Mar 16, 2015
1 parent 787613b commit 6b8f066
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
27 changes: 20 additions & 7 deletions components/sidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var React = require('react');
var Router = require('react-router');
var Link = Router.Link;

var TeachAPI = require('../lib/teach-api');
var teachAPI = require('../lib/teach-api');

var TriangleCorner = React.createClass({
propTypes: {
Expand All @@ -27,33 +27,46 @@ var TriangleCorner = React.createClass({
});

var Login = React.createClass({
getDefaultProps: function() {
return {
teachAPI: teachAPI
};
},
componentDidMount: function() {
var teachAPI = new TeachAPI();
var teachAPI = this.props.teachAPI;

teachAPI.on('login:error', this.handleApiLoginError);
teachAPI.on('login:cancel', this.handleApiLoginCancel);
teachAPI.on('login:success', this.handleApiLoginSuccess);
teachAPI.on('logout', this.handleApiLogout);
this.teachAPI = teachAPI;
this.setState({username: this.getUsername()});
},
componentWillUnmount: function() {
var teachAPI = this.props.teachAPI;

teachAPI.removeListener('login:error', this.handleApiLoginError);
teachAPI.removeListener('login:cancel', this.handleApiLoginCancel);
teachAPI.removeListener('login:success', this.handleApiLoginSuccess);
teachAPI.removeListener('logout', this.handleApiLogout);
},
getInitialState: function() {
return {
username: null,
loggingIn: false
};
},
getUsername: function() {
var info = this.teachAPI.getLoginInfo();
var info = this.props.teachAPI.getLoginInfo();
return info && info.username;
},
handleLoginClick: function(e) {
e.preventDefault();
this.setState({loggingIn: true});
this.teachAPI.startLogin();
this.props.teachAPI.startLogin();
},
handleLogoutClick: function(e) {
e.preventDefault();
this.teachAPI.logout();
this.props.teachAPI.logout();
},
handleApiLoginError: function(err) {
this.setState({loggingIn: false});
Expand All @@ -64,7 +77,7 @@ var Login = React.createClass({
"address you used?");
} else {
window.alert("An error occurred! Please try again later.");
this.teachAPI.logout();
this.props.teachAPI.logout();
}
},
handleApiLoginCancel: function() {
Expand Down
3 changes: 2 additions & 1 deletion lib/teach-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ _.extend(TeachAPI.prototype, {
}
});

module.exports = TeachAPI;
module.exports = new TeachAPI();
module.exports.TeachAPI = TeachAPI;
2 changes: 1 addition & 1 deletion test/browser/teach-api.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var should = require('should');
var sinon = window.sinon;

var TeachAPI = require('../../lib/teach-api');
var TeachAPI = require('../../lib/teach-api').TeachAPI;

describe('TeachAPI', function() {
var xhr, requests, storage;
Expand Down

0 comments on commit 6b8f066

Please sign in to comment.