【问题标题】:Backbone: Navigating to next page after successful response from node serverBackbone:节点服务器成功响应后导航到下一页
【发布时间】:2014-03-02 19:15:37
【问题描述】:

尝试从节点服务器实现登录功能。我能够将请求从骨干模型发送到节点服务器。节点服务器调用另一个站点并从另一个站点获取响应。
现在,如果登录成功,应该会显示另一个页面并显示响应。但是如果登录失败,登录页面应该会显示错误信息。
型号:

var LoginModel = Backbone.Model.extend({
    url:'http://localhost:3000/login',

    defaults: {
        email:"",
        password:""
    },
    parse: function(resp) {
        console.log('Model: Got the response back');
        return resp;
    },
    login: function() {
        console.log('Model: Login function:'+JSON.stringify(this));
        this.save(
            {}, {
                success: function(resp) {
                    console.log('success'+JSON.stringify(resp));
                },
                error: function(error) {
                    console.log('error: '+JSON.stringify(error));
                }
            });
        console.log('Model: Got the response back:');
    }
});
var loginModel = new LoginModel();

查看:

var LoginView = Backbone.View.extend({
    url: 'http://localhost:3000/login',
    template:_.template('<div class="form-signin">'+
                        '<h2 class="form-signin-heading">Please sign in</h2>'+
                        '<input type="text" id="email" class="form-control" placeholder="Email address" required="" autofocus="">'+
                        '<input type="password" id="password" class="form-control" placeholder="Password" required="">'+
                        '<button id="loginBtn" class="btn btn-lg btn-primary btn-block" >Sign in</button>'+
                        '</div>'),
    events: {
        "click #loginBtn":"login"
    },
    initialize: function() {
        this.model.on('change', this.render, this); 
    },
    render: function() {
        var attributes = this.model.toJSON();
        this.$el.html(this.template(attributes));
    },
    login: function() {
      console.log('view signIn');
      this.model.set({
        "email": $('#email').val(),
        "password": $('#password').val()
      });
      this.model.login();
    }
});
var loginView = new LoginView({model: loginModel});
loginView.render();
$(".container").append(loginView.el);

注意:我是骨干网的新手,并且阅读了有关路由的信息。对我来说不是很清楚。

【问题讨论】:

  • 如果成功则只处理响应,使用window.href 重定向,否则调用视图方法以更新错误。您也可以只调用登录视图,而不使用.append,只需将所有内容放在initialize 函数中或使用.setElement 传递元素
  • 感谢@GeoPhoenix。那是两个很多信息。你能把它分解一下并用代码解释一下吗?

标签: javascript node.js backbone.js


【解决方案1】:

Backbone.Router提供路由客户端页面的方法

因此,在您的情况下,您的代码应如下所示:

var LoginModel = Backbone.Model.extend({
    url:'http://localhost:3000/login',

    defaults: {
        email:"",
        password:""
    }
}

查看

var LoginView = Backbone.View.extend({
    el: '.container'
    template:_.template('...'),
    events: {
        "click #loginBtn":"login"
    },
    initialize: function() {
        this.listenTo(this.model, 'change', this.render); 
    },
    render: function() {
        var attributes = this.model.toJSON();
        this.$el.html(this.template(attributes));
    },
    login: function() {
        console.log('view signIn');
        this.model.set({
            "email": $('#email').val(),
            "password": $('#password').val()
        });

        console.log('Model: Login function:'+JSON.stringify(this.model));
        this.model.save(
            {}, {
            success: function(resp) {
                console.log('success'+JSON.stringify(resp));
            },
            error: function(error) {
                console.log('error: '+JSON.stringify(error));
            }
        });
        console.log('Model: Got the response back:');
    }
});

路由器

var Router = Backbone.Router.extend({
    routes : {
        "": "home",
        "login" : "login"
    },

    home: function() {
        // your home page
    }

    login: function() {
        var loginModel = new LoginModel();
        var loginView = new LoginView({model: loginModel});
        loginModel.fetch();
    }
})

new Router();
Backbone.history.start();

【讨论】:

  • 这里有几个问题:如何处理视图的事件?就像在这种情况下视图的登录功能是设置模型并调用模型的保存功能。 this.model.set({ "email": $('#email').val(), "password": $('#password').val() }); this.model.login();
  • 此交互与您的视图相关,因此将保持不变,我已经更新了答案。
  • 因此视图将直接调用模型,就像它现在正在做的那样。如果有人直接点击 url http:\\localhost:3000\login,路由器的 login() 将被调用。是吗?
  • 不,是视图必须调用model.save
猜你喜欢
  • 2021-03-15
  • 1970-01-01
  • 2021-02-07
  • 1970-01-01
  • 1970-01-01
  • 2021-03-24
  • 2020-05-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多