简体   繁体   English

使用主干创建帖子后导航帖子

[英]Navigating a Post after creating it with backbone

In a separate view in backbone I have a function that basically creates a new post: 在骨干网中的另一个视图中,我有一个基本上创建新帖子的函数:

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     
},

In another view all the views are listed, and if the user clicks one of these views the uri changes to "#post/:id" 在另一个视图中,列出了所有视图,如果用户单击其中一个视图,则uri更改为“#post /:id”

and in my roots I have this: 从我的根源来看:

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);

},

I am having no problem to navigate to any of these posts from this list of posts, my router handles them very well, but I am having a problem navigating to the post that I just created after creating possibly inside the save function.. 我可以从该帖子列表中导航至这些帖子中的任何一个,我的路由器都可以很好地处理它们,但是导航到我可能在save功能内创建后刚刚创建的帖子时,我遇到了问题。

Are you sure that the new model is being saved to the server before you access it via the route? 您确定通过路由访问新模型之前将其保存到服务器吗?

To be sure, you can always pass {wait: true} into create() if you'd like to wait for the server before adding the new model to the collection. 可以肯定的是,如果要在将新模型添加到集合之前等待服务器,则可以始终将{wait:true}传递给create()。

example: 例:

this.collection.create({
        name: $('#big-input').val(),
        adress: $('small-input').val(),
        postedBy: window.id
    }, {wait:true});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM