从 Backbone 中的另一个视图调用新视图
Call a new View from another View in Backbone
我完全是 JS 和 Mongo/Mongoose/Express/Node/Backbone/Require 堆栈的新手...所以有很多东西要学,我完全有可能在这里错过了一些东西。但是,我试图存储提交表单的结果,如果保存操作成功,则将用户移动到不同的视图,所以我的视图看起来像这样:
define(['CoreView', 'text!templates/profile.html', 'models/Account'],
function(CoreView, profileTemplate, Account) {
var profileView = CoreView.extend({
el: $('#content'),
events: {'submit form': 'update'},
template: _.template(profileTemplate),
update: function() {
/*
* Gather the form data... then:
*/
$.post('/accounts/' + this.model.get('_id'),
{data: formData},
function(response) {
if (response == 'OK') {
$.get('/#index'); // NO CHANGE???
} else {
console.err('Save Failed :(');
}
});
return false;
},
render: function() {
if (this.model.get('_id') !== 'me') {
this.$el.html(this.template(this.model.toJSON()));
}
return this;
}
});
return profileView;
});
现在很可能我错过了路由中的某些机制,我可以在其中指定此类内容,欢迎就为什么这不起作用或更好的解决方案提出任何建议。
$.get()
发出页面的 GET 请求 - 它不会对 "move the user on to a different view" 执行任何操作。为此,您可以使用:
document.location.href = /#index`
或者如果您有 Backbone.Router
或类似的更好的解决方案:
router.navigate('index')
我完全是 JS 和 Mongo/Mongoose/Express/Node/Backbone/Require 堆栈的新手...所以有很多东西要学,我完全有可能在这里错过了一些东西。但是,我试图存储提交表单的结果,如果保存操作成功,则将用户移动到不同的视图,所以我的视图看起来像这样:
define(['CoreView', 'text!templates/profile.html', 'models/Account'],
function(CoreView, profileTemplate, Account) {
var profileView = CoreView.extend({
el: $('#content'),
events: {'submit form': 'update'},
template: _.template(profileTemplate),
update: function() {
/*
* Gather the form data... then:
*/
$.post('/accounts/' + this.model.get('_id'),
{data: formData},
function(response) {
if (response == 'OK') {
$.get('/#index'); // NO CHANGE???
} else {
console.err('Save Failed :(');
}
});
return false;
},
render: function() {
if (this.model.get('_id') !== 'me') {
this.$el.html(this.template(this.model.toJSON()));
}
return this;
}
});
return profileView;
});
现在很可能我错过了路由中的某些机制,我可以在其中指定此类内容,欢迎就为什么这不起作用或更好的解决方案提出任何建议。
$.get()
发出页面的 GET 请求 - 它不会对 "move the user on to a different view" 执行任何操作。为此,您可以使用:
document.location.href = /#index`
或者如果您有 Backbone.Router
或类似的更好的解决方案:
router.navigate('index')