Skip to content

Instantly share code, notes, and snippets.

@rubyist
Last active February 19, 2017 21:39
Show Gist options
  • Save rubyist/5670520 to your computer and use it in GitHub Desktop.
Save rubyist/5670520 to your computer and use it in GitHub Desktop.
How one might filter a list in meteor
<head>
<title>searchy</title>
</head>
<body>
{{> searchy}}
</body>
<template name="searchy">
<h1>Searchy</h1>
<input id="search" name="search" />
<ul>
{{#each items}}
<li>{{name}}</li>
{{/each}}
</ul>
</template>
Items = new Meteor.Collection('items');
if (Meteor.isClient) {
Template.searchy.helpers({
items: function() {
return Items.find();
}
});
Template.searchy.events({
'keyup input' : function (e) {
Session.set('itemFilter', $(e.target).val());
}
});
Deps.autorun(function() {
Meteor.subscribe('items', Session.get('itemFilter'));
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
Meteor.publish('items', function(filter) {
return Items.find({name: {$regex:filter, $options:'i'}}, {sort: {name: 1}});
});
// Load up some items fixtures
if (Items.find().count() === 0) {
Items.insert({name: 'Black Sabbath'});
Items.insert({name: 'The White Stripes'});
Items.insert({name: 'The Doors'});
Items.insert({name: 'The Monkees'});
Items.insert({name: 'Black Keys'});
Items.insert({name: 'Black Eyed Peas'});
Items.insert({name: 'Johnny Cash'});
Items.insert({name: 'Johnny Mathis'});
Items.insert({name: 'Johnny Gill'});
Items.insert({name: 'Frank Sinatra'});
}
}
@teerachel
Copy link

Hi, does this work? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment