Javascript 使用主干创建帖子后导航帖子
在主干中的另一个视图中,我有一个基本上创建新帖子的功能:Javascript 使用主干创建帖子后导航帖子,javascript,backbone.js,backbone-routing,Javascript,Backbone.js,Backbone Routing,在主干中的另一个视图中,我有一个基本上创建新帖子的功能: save : function(){ console.log($('#big-input').val()); console.log(window.id); this.collection.create({ name: $('#big-input').val(), adress: $('small-input').val(), postedBy: window.id
save : function(){
console.log($('#big-input').val());
console.log(window.id);
this.collection.create({
name: $('#big-input').val(),
adress: $('small-input').val(),
postedBy: window.id
});
//navigate to that post
},
在另一个视图中,列出了所有视图,如果用户单击其中一个视图,uri将更改为“#post/:id”
在我的根中,我有这样一个:
routes: {
"" : "showForm",
"post/:id" : "showPost"
},
showPost: function(id){
var curmodel = this.collection.get(id);
console.log(curmodel);
var posts = new postView({model:curmodel});
posts.render();
$(".maincontainer").html(posts.el);
},
我可以从这个帖子列表中导航到这些帖子中的任何一篇,我的路由器处理得很好,但是我在导航到我刚刚创建的帖子时遇到了问题,这篇帖子可能是在
save
函数中创建的 在通过路由访问新模型之前,是否确定已将其保存到服务器
当然,如果要在将新模型添加到集合之前等待服务器,则始终可以将{wait:true}传递到create()中
例如:
this.collection.create({
name: $('#big-input').val(),
adress: $('small-input').val(),
postedBy: window.id
}, {wait:true});