铁路由器获得当前物品? [英] Iron Router Get Current Item?

查看:45
本文介绍了铁路由器获得当前物品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我的应用程序中有两条路由.有一个主页路由包含一个想法列表(只是文本项),然后还有一个显示和编辑想法的路由.

Right now I have two routes in my application. There's the main page route that has a list of Ideas (just text items) and then there's a route for showing and editing Ideas.

我的问题是编辑想法...我不确定如何真正获取要编辑的项目并保留更改.我可以使用全局变量,但这几乎可以肯定是hack,而不是流星方式"(就此而言,不是流星方式).我可以使用Session,但这似乎不太正确还是对我来说...我给人的印象是Session仅用于视觉对象(例如暂时隐藏内容).

My problem is with editing ideas... I'm not sure how to actually get the item being edited and persist the changes. I could use a global variable, but that's almost certainly a hack and not the "meteor way" (or the right way in any software, for that matter.) I could use the Session, but that doesn't seem quite right to me either... I got the impression that Session was just for visual things (like hiding stuff temporarily.)

所以这是我到目前为止的相关代码:

So here's the relevant code I have so far:

Router.route('/idea/:_id', function() {
    var idea = Ideas.findOne({_id: this.params._id});
    // Should I save this.params._id in a Session?
    // Is that "the meteor way"?
    this.render('ShowIdea', {data: idea});
}, {
    name: 'idea.show'
});

Template.ShowIdea.events({
    "input div": function (event) {
        var newText = $(event.target).text();
        console.log("newText: " + newText);
        // this event gets called at the right time, and I can see the new text,
        // but how do I know which item to save it on?
    }
});

模板没什么可看的,但这里是:

Not much to see for the template, but here it is:

<template name="ShowIdea">
    <div contentEditable="true">{{text}}</div>
</template>

还有两个侧面问题:

1-$(event.target).text()是提取新文本的最佳方法吗?我什么都没想,但对我来说似乎有点漫长.

1 - Is $(event.target).text() the best way of extracting the new text? I couldn't think of anything else, but it seems a bit long winded to me.

2-某处是否有一些有关Iron-Router的全面文档?我看到了《快速入门》和《指南》,但似乎在任何地方都没有直接列出的对象和方法.

2 - Is there some comprehensive documentation to Iron-Router somewhere? I see a QuickStart and a Guide, but there doesn't seem to be a straight up list of Objects and Methods anywhere for it.

推荐答案

我认为您的方法很好. 要保存新文本,只需执行以下操作:

I think that your approach is good. To save the new text just do something like:

Ideas.update(this._id, {$set: {text: newText}});

您可以使用this._id,因为您已经使用data: idea在路由器中设置了数据上下文,因此在Template.ShowIdea.helpers/events中,"this"指的是特定的想法.

You can use this._id because you already set your data context in the router with data: idea, so in your Template.ShowIdea.helpers/events "this" refers to the specific idea.

关于您的附带问题:

  1. 您还可以像这样使用 template.find(): /p>

"input div": function (event, template) {
  var newText = template.find("div").text();
}

但是它不太直接,我认为event.target更好,因为已经填充了DOM元素(另请注意event.target和event.currentTarget之间的="nofollow">差异,但您的示例中没有问题).

But it is less straightforward and I think that event.target is better as the DOM element is already populated (also note the difference between event.target and event.currentTarget but there is no issue in your example).

  1. 除了您提到的指南,我再也没有找到Iron Router的任何其他文档.

这篇关于铁路由器获得当前物品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆