Meteor JS (Part 7): Item Search

This is a post in a multiple part series on getting started with Meteor JS. If you don’t have Meteor JS installed you will want to start at  the first post in the series. Part 2 is where we start building the application.

If you would like to follow along I have tagged the git repository on Github at the starting point for this post. Here is the URL to the tag

In this post we are going to add the email address of the currently logged in user to our layout. Then we are going add an item search feature. The way the search feature will work is by letting you enter a search term for each column. This will allow us to limit the number of records coming back.

So let’s get started.

Logged In User Email Address

In the last post we discussed authentication and authorization. We added a sign in button but forgot something very important. How do we know which user is logged in? We don’t. Thanks goes out to AlanB for calling this one out.


First let’s create a UI helper that we can call from any template. This will allow us to place {{currentUserName}} anywhere we need to display the username.

Add the following code to the end of client/lib/helpers.js.


Now let’s use the UI helper in our layout template.

Add the following to client/views/layouts/layout.html.

After you save you should start to see the email address of the currently logged in user, beside the sign out button, if you are signed in.

Item Search

We will be placing a search button in the header of the first column. When a user clicks the search button a filter field will appear in each column. All string type fields will use a regular expression search. All number type fields will use an exact search. For the query, all fields that are filled in will be combined together using an and operator. Below is an image of he finished product.


Item Search Template

First, we are going to create a template to handle filtering a column. When we use this template we will use the following syntax.

Let’s create a file at client/views/items/itemsearch.html and add the following code.

Nothing really that interesting here other than value getting assigned from searchValue. We will get to see where that comes from when we get to itemsearch.js.

Next, let’s create a file at client/views/items/itemsearch.js and add the following code.

There is a lot going on in here. We are attaching an event on the key up of the .searchInput within the current template. Then, we are throttling it to only fire once every half second.

The event handler itself is taking in the event and template as arguments. Within the handler, we are checking for our session object. If one doesn’t exist we create an empty object. Next, we assign searchValue the value from the .searchInput within the template.

At this point we need to check if we got an empty string or not. If we didn’t get an empty string we need to look at our number parameter on the template. If it is a number just parse it as a number. If it isn’t a number just assign the string value of the input to our searchQuery object.

Now here is where things get interesting. At the point that the property is created it will always be on the searchQuery object. We need to get rid of empty properties because we don’t want to burden the server with doing a search on empty strings. So we delete the property by using the delete keyword.

Finally, we save the searchQuery to our session. What this event handler allows us to do is place as many item search templates as we have columns in our items collection.

All that is left is to create the helper that returns the value. This allows the item search template to survive a hot code push because session is preserved when a hot code push happens.

Finally let’s create a file at client/views/items/itemsearch.css and add the following code.

Here we are just saying hide the inputs by default. You might be wondering why I have used so many classes to define this rule? Originally I tried just using my filterInput class but Semantic UI had rules that were more specific than mine. In order to override them I had to create a rule with higher specificity. If you would like to know more about CSS specificity check out my post on the subject.

CSS: Specificity

Item Publication

Everything we have done so far has been on the client. We need to modify the allItems publication on the server side to take in our searchQuery object that we create on the client side. This is where the true beauty of Meteor starts to shine. All I have done on the client side is create an object in Javascript. And I just need to use it on the server side.

Modify server/publications/items/item.js by replacing the code with the following.

All we are doing here is just looping the properties of the searchQuery object and transforming them into a Mongo DB query. What is really cool about this is that it makes only the search results available to the UI. You will notice, in the next section, that we are not going to make any changes to how the list of items is generated. This is part of the reactive nature or Meteor JS.

Item List

Next, let’s modify client/views/items/itemlist.js by adding the following code. You will notice at this point that we have moved our subscription within a call to Tracker.autorun. This allows the subscription to update every time the Session searchQuery object changes. At the bottom we are adding an event handler to make our filter inputs appear.

All we need to now is to modify client/views/items/itemlist.html to use our new itemSearch template by adding the following code.

Finally, let’s create a  file at client/views/items/itemlist.css and add the following code. This just changes the cursor to a pointer on our search button.


In this post we added a pretty sophisticated search mechanism. There are better ways of doing search. Like using a Mongo DB fulltext index. But this is all we need for now. I may visit better ways of searching in a later post.

If you have any questions leave a comment on this blog or send me a tweet on twitter to @dhirshjr. And as always the code from this post is up on Github at the following URL.

Leave a Reply

Your email address will not be published. Required fields are marked *