【问题标题】:Is passing the entire object really the way to do this?传递整个对象真的是这样做的方法吗?
【发布时间】:2015-08-14 19:53:47
【问题描述】:

this page,我看到了这段代码:

post._id = Posts.insert(post);
Router.go('postPage', post);

...根据 _id 值加载页面。

这真的是应该的,还是应该改为:

var id = Posts.insert(post);
Router.go('postPage', id);

?

IOW,当只需要从插入函数返回的 id 时,为什么要传递整个对象(引用上面链接的页面,“La función insert() devuelve el identificador _id del objeto que se ha insertado en la base de datos")?

即使显示的方式有效(路由器足够聪明,可以提取 id),“我的方式”不是更好吗(即使我不是 Sinatra)?

【问题讨论】:

    标签: javascript mongodb meteor dom-events


    【解决方案1】:

    你是对的。您引用的博客文章中的示例代码甚至不起作用。

    Router.route('/posts/:_id', {
      name: 'postPage',
      data: function() { return Posts.findOne(this.params._id); }
    });
    

    post 作为参数传递时会失败,因为 this.params._id 是整个 :_id 参数,而不是_id 来自传递的对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      • 1970-01-01
      相关资源
      最近更新 更多