Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 主干网:设置从一个模型到另一个模型的服务器响应_Backbone.js - Fatal编程技术网

Backbone.js 主干网:设置从一个模型到另一个模型的服务器响应

Backbone.js 主干网:设置从一个模型到另一个模型的服务器响应,backbone.js,Backbone.js,登录后,用户被重定向到另一个页面。因此,响应登录模型从服务器获取,它尝试设置为另一个模型。 第二个模型从第一个模型正确设置。但当它到达另一个页面的视图时,它将变为空。 型号 var LoginModel = Backbone.Model.extend({ url:'http://localhost:3000/login', defaults: { email:"", password:"" }, parse: function(r

登录后,用户被重定向到另一个页面。因此,响应登录模型从服务器获取,它尝试设置为另一个模型。
第二个模型从第一个模型正确设置。但当它到达另一个页面的视图时,它将变为空。
型号

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));
                    dashboardModel.set(resp.result);
                    window.location = 'templates/dashboard.html'
                },
                error: function(error) {
                    console.log('error: '+JSON.stringify(error));
                }
            });
    },
    redirect: function() {
        console.log('inside redirect method');
    }
});
var loginModel = new LoginModel();

var DashboardModel = Backbone.Model.extend({
    defaults: {
        campaignName:"",
        orderedAndGoal:"",
        status:"",
        endDate:"",
    },
    parse: function(resp) {
        console.log('Model: Got the response back');
        return resp;
    }
});
var dashboardModel = new DashboardModel();
查看

var DashboardView = Backbone.View.extend({
    template:_.template('<div>'+
                        '<h3><%= campaignName %></h3>'+
                        '<span><%= orderedAndGoal %>, </span>'+
                        '<span><%= status %>, </span>'+
                        '<span><%= endDate %>, </span>'+
                        '</div>'),
    initialize: function() {
        this.model.on('change', this.render, this); 
    },
    render: function() {
        console.log('what happens here')
        var attributes = this.model.toJSON();
        this.$el.html(this.template(attributes));
    },
});
var dashboardView = new DashboardView({model: dashboardModel});
dashboardView.render();
$(".container").append(dashboardView.el);
var DashboardView=Backbone.View.extend({
模板:u.template(“”+
''+
', '+
', '+
', '+
''),
初始化:函数(){
this.model.on('change',this.render,this);
},
render:function(){
log('这里发生了什么')
var attributes=this.model.toJSON();
this.el.html(this.template(attributes));
},
});
var dashboardView=新的dashboardView({model:dashboardModel});
dashboardView.render();
$(“.container”).append(dashboardView.el);

您正通过
window.location=…
导航到另一个HTML页面。那不行。当浏览器导航到另一个页面时,所有正在运行的代码和它们设置的任何变量都会被清除。主干网是关于创建“单页应用程序(SPA)”,其中浏览器只加载1页,然后在运行时动态更改DOM。了解这一点的出发点是查看主干网路由器。您将调用此路由器上的方法将用户移动到另一个“视图”,而不是触摸
窗口。位置

解决了这个问题,您的代码应该可以工作:)