Last active
February 19, 2017 21:39
-
-
Save rubyist/5670520 to your computer and use it in GitHub Desktop.
How one might filter a list in meteor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, does this work? :)