1

i need a very simple login system for my web application with backbone.js

Workflow: App Start -> LoginView -> When Logged In -> App

Here is my solution. What can i do better?

Login Status Model:

window.LoginStatus = Backbone.Model.extend({
defaults: {
    loggedIn: false,
    userId: null,
    username: null,
    error: "An Error Message!"
},

initialize: function () {
    _.bindAll(this, 'getSession', 'setStorage');
},

getSession: function (username, password) {
    var tmpThis = this;
    $.getJSON('http://requestURL.de/getSession.php?username=' + username + '&password=' + password, function(data) {
        if (data != null) { 
            tmpThis.setStorage(data.id, data.username);
            $.mobile.changePage("#home");
        }                                   
    });
},
setStorage: function(userId, username) {
    localStorage.setItem("userId", userId);
    localStorage.setItem("username", username);
    this.set({ "loggedIn" : true});
}
});

Here is my Login View:

window.LoginView = Backbone.View.extend({

el: $("#login"),

initialize:function () {
    this.render();
},

events: {
    "click input[type=submit]": "onSubmit"
},

onSubmit: function(event) {
        var username = $(this.el).find("#user-username").val(),
            password = $(this.el).find("#user-password").val();

        this.model.getSession(username, password);

        return false;
},
render:function () {
    if (this.model.get("loggedIn")) {
        var template = _.template( $("#login_template").html(), {} );
    } else {
        var template = _.template( $("#login_template").html(), { "error" : this.model.get("error") } );
    }
    $(this.el).html( template );
}

});
4

2 回答 2

2

我对您的建议是在主干应用程序之外进行登录,并且只有在成功登录过程后才让他们访问“单页应用程序”

于 2012-07-31T23:37:44.787 回答
0

您可以参考这个使用 POST 方法进行登录请求的主干项目。https://github.com/denysonique/backbone-login

于 2013-04-06T14:46:22.767 回答