Backbone.js模型和视图绑定重新渲染

时间:2015-10-01 06:25:48

标签: jquery backbone.js

所以我有一个默认属性设置的模型和一个登录页面,我将用户名和密码的值存储到集合中。

//collection
var userProfileCollection = Backbone.Collection.extend({
    model: UserProfileModel
});

//Model
var UserProfileModel = Backbone.Model.extend({
    defaults: {
        userName: "",
        password: ""
    }
});

//View

var LoginRegisterView = Backbone.View.extend({
            initialize: function() {
                this.render();  
            },
            render: function() {

            //loads the template containing the form
            $("#loginPage").html(loginregisteruser_Template);

            click event(){
           var username = $("#username").val();
           var password = $("#password").val();

           //userProfileModel is set as a global since another view accesses the model's attributes and their values
                var userProfileColl = new userProfileCollection();
                userProfileModel.set("userName", username);
                userProfileModel.set("password", password);
                userProfileColl.add(userProfileModel);

                window.location = "#" //home
}
}
});

//router
var AppRouter = Backbone.Router.extend({

routes:{
        'login': "login"
},
login: function() {

                this.view = new LoginRegisterView({
                    model: userProfileModel,
                    el: $('#loginPage')
                });

    });

所以我的问题是每当我刷新登录页面时,视图都会被重新渲染,因此我仍然需要在页面上显示的用户名和密码的当前值被模型中设置的默认值覆盖。即使重新呈现视图,如何维护用户名和密码字段的值?我觉得我错过了一些基本的东西。我是Backbone.js的新手。提前致谢

0 个答案:

没有答案