這是一個登錄流程,用戶提供登錄詳細信息並從服務器獲取響應。
在這裏,我無法弄清楚我應該在哪裏刪除先前的視圖? dashboard View
需要知道約LoginView
。
Router
有什麼用?在這種情況下,流量是否會流向路由器?
兩種觀點骨幹:摧毀之前的路由器視圖和使用
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" href="#login" 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 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));
this.$el.appendTo('.container');
},
});
var dashboardView = new DashboardView({model: dashboardModel});
這兩種模式
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.get("0")));
dashboardModel.set(resp.get("0"));
//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:"",
orderPlace:"",
tShirtOrdered:"",
tippingPoint:"",
getPaid:""
},
parse: function(resp) {
console.log('Model: Got the response back');
return resp;
}
});
var dashboardModel = new DashboardModel();
當LoginModel.save()
會從服務器的值,並將其設置爲DashboardModel
。 DashboardView
傾聽模型中的變化。然後它調用render()
。這一切都有道理。但之後要去哪裏? 將DashboardView.el
添加到render()
中的父標記是否是個好主意? 這裏有Router
的用法嗎?
應用程序的路由器如下所示:
var Router = new (Backbone.Router.extend({
routes: {
"":"home",
"login":"login"
},
start: function() {
Backbone.history.start({pushState:true});
},
home: function() {
var loginView = new LoginView({model: loginModel});
loginView.render();
$(".container").append(loginView.el);
},
login: function() {
var loginModel = new LoginModel();
var loginView = new LoginView({model: loginModel});
loginModel.fetch();
}
}));
new Router.start();
這不是我正在尋找的東西。我會在哪裏刪除第一個視圖?這裏有什麼使用路由器? –