{"size": 1607, "ext": "js", "max_stars_repo_path": "src/main/resources/public/js/view/Form-View.js", "max_stars_repo_name": "infsci2560sp16/full-stack-web-project-liaokaien", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/main/resources/public/js/view/Form-View.js", "max_issues_repo_name": "infsci2560sp16/full-stack-web-project-liaokaien", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/main/resources/public/js/view/Form-View.js", "max_forks_repo_name": "infsci2560sp16/full-stack-web-project-liaokaien", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 29.2181818182, "max_line_length": 81, "alphanum_fraction": 0.5301804605}
var Backbone = require('backbone');
var _ = require('underscore');
var $ = require('jquery');

var config = require('../config.js');
var baseUrl = config.baseUrl;
var fetchUrl = baseUrl + '/login';

module.exports = Backbone.View.extend({
    el: '.form_container',
    template: _.template($('#form_template').html()),
    events: {
        'click #btn_submit': 'submit',
        'click #btn_cancel': 'cancel'
    },
    initialize: function(){
        this.render();
    },
    render: function(){
        this.$el.append(this.template());
    },

    submit: function(){
        var password = this.$el.find('#form_password').val(),
            uname    = this.$el.find('#form_password').val(),
            token = btoa(password + ':' + uname),
            self = this;
        var passwordConfirmation = this.$el.find('#form_confirm_password').val();
        if (typeof passwordConfirmation !== 'undefined' ){
            if (passwordConfirmation !== password) {
                this.pop('Please confirm password again');
                return;
            }else {
                fetchUrl = 'http://www.liaokaien.com/api/code2gether/signup';
            }
        }
        $.post(fetchUrl, {token:token}, function(res){
            if (res.success && res.token === token){
                window.location = '/index.html';
            } else {
                self.pop(res.error);
            }
        });
    },

    cancel: function(){
        window.history.back();
    },

    pop: function(error){
        // Indicate the error message
        this.$el.find('.error_msg').text(error);
    }
});
