Every time I have a new idea for an application, I start with Meteor. And every time I end up using something else. And I think it comes down to me not knowing how to do a simple database query.
每当我有一个新的申请想法,我就从流星开始。每次我用别的东西。我想这归结于我不知道如何做一个简单的数据库查询。
Here's a hypothetical example. I want to make an app where the user types something into a box, presses a button, and then an image of whatever they typed in shows up. It uses some image search api.
这是一个假想的例子。我想做一个应用程序,用户把某样东西输入到一个盒子里,按下一个按钮,然后就可以看到他们输入的任何东西的图像。它使用一些图像搜索api。
<template name="image">
<input type="text" class="description"></input>
<button class="showImage"></button>
<img src="{{img}}"></img>
</template>
Seems simple enough so far. Now there isn't a way to send information to the client without putting it in a database first, as is my understanding. Let's assume we have some function addToDB that takes queries and enters the image information into the database.
看起来很简单。现在没有一种方法可以在不将信息放入数据库的情况下将信息发送给客户端,这是我的理解。假设我们有一个函数addToDB,它接受查询并将图像信息输入到数据库中。
Template.image.events({
'click .showImage': function() {
addToDB($('.description').val());
}
});
Great! That's still not too bad. But now to send the data back to the client...
太棒了!这还不算太糟。但是现在要把数据传回客户端……
//server.js
Meteor.publish("image", function(query) {
Images.find({q: query});
}
But wait. We can't just subscribe when the client loads, because we don't know the query yet. So maybe the event handler needs to be
但是等待。当客户端加载时,我们不能仅仅订阅,因为我们还不知道查询。事件处理程序可能需要。
Template.image.events({
'click .showImage': function() {
addToDB($('.description').val());
Deps.autorun(function() {
Meteor.subscribe("images", $('.description').val());
});
}
});
Okay, let's feed that into the template...
好的,让我们把它输入到模板中……
Template.image.img = function() {
return Images.findOne().imgsrc;
}
Nope, that results in an error because when the template is first loaded, we haven't subscribed to Images yet. So we can update the template like so:
不,这会导致一个错误,因为当第一次加载模板时,我们还没有订阅图像。所以我们可以更新模板如下:
<template name="image">
<input type="text" class="description"></input>
<button class="showImage"></button>
{{#each info}}
<img src="{{info.img}}"></img>
{{/each}}
</template>
And then change the template filling function to:
然后将模板填充函数改为:
Template.image.info = function() {
return Images.find({}, {limit: 1});
}
And voila!
瞧!
I spent longer than I'm willing to admit stumbling through all of that this evening. If it was just plain old node, I could've used some simple jQuery.
我花的时间比我愿意承认的要长。如果它只是一个普通的旧节点,我可以使用一些简单的jQuery。
But there are a million amazing features that Meteor has that I really want to take advantage of. So what I'm hoping is that someone can show me the way. What mistakes did I make in this process, and in the final result? What's the nicest, cleanest, simplest way to get this done within a meteor app.
但是我真的很想利用流星拥有的无数令人惊叹的特征。所以我希望有人能给我指路。在这个过程中,在最后的结果中,我犯了什么错误?什么是在流星应用程序中最完美、最干净、最简单的方法。
This is so complicated, within a framework that makes so many other things so simple. So how can I just make a simple database query?
这是如此复杂,在一个框架内,使得许多其他事情如此简单。那么我怎么才能做一个简单的数据库查询呢?
1 个解决方案
#1
2
Consider looking at the meteor examples they provide. All of the examples have the functionality of taking user input, managing collections, and displaying collection queries to templates.
考虑看看他们提供的流星例子。所有的示例都具有接受用户输入、管理集合和向模板显示集合查询的功能。
Most of your code looks fine but you are asking several questions at once and starting from a perspective that makes it difficult to answer. Some general notes:
您的大部分代码看起来都很好,但是您同时要问几个问题,并且从一个很难回答的角度出发。一些一般性的指出:
- consider using Session to hold variables a user sets with an event.
- 考虑使用会话来保存用户设置的带有事件的变量。
- subscribe to the data you want by passing Session variable to a Deps.autorun function
- 通过将会话变量传递给Deps来订阅所需的数据。自动运行功能
- display the data you want by passing a Session variable to the template.
- 通过向模板传递会话变量来显示所需的数据。
- basic javascript rules still apply -
null.someThingIWant
is an error. A handy pattern isreturn something && something.someThingIWant;
- 基本的javascript规则仍然适用- null。someThingIWant是一个错误。一个方便的模式是返回一些东西&一些东西。
One problem in the code above is that your publish is not returning the query results. I assume that is a typo.
上面代码中的一个问题是您的发布没有返回查询结果。我认为这是一个错字。
#1
2
Consider looking at the meteor examples they provide. All of the examples have the functionality of taking user input, managing collections, and displaying collection queries to templates.
考虑看看他们提供的流星例子。所有的示例都具有接受用户输入、管理集合和向模板显示集合查询的功能。
Most of your code looks fine but you are asking several questions at once and starting from a perspective that makes it difficult to answer. Some general notes:
您的大部分代码看起来都很好,但是您同时要问几个问题,并且从一个很难回答的角度出发。一些一般性的指出:
- consider using Session to hold variables a user sets with an event.
- 考虑使用会话来保存用户设置的带有事件的变量。
- subscribe to the data you want by passing Session variable to a Deps.autorun function
- 通过将会话变量传递给Deps来订阅所需的数据。自动运行功能
- display the data you want by passing a Session variable to the template.
- 通过向模板传递会话变量来显示所需的数据。
- basic javascript rules still apply -
null.someThingIWant
is an error. A handy pattern isreturn something && something.someThingIWant;
- 基本的javascript规则仍然适用- null。someThingIWant是一个错误。一个方便的模式是返回一些东西&一些东西。
One problem in the code above is that your publish is not returning the query results. I assume that is a typo.
上面代码中的一个问题是您的发布没有返回查询结果。我认为这是一个错字。