Created
February 4, 2014 16:50
-
-
Save yeputons/8807589 to your computer and use it in GitHub Desktop.
This file contains 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>search-test</title> | |
</head> | |
<body> | |
{{> hello}} | |
</body> | |
<template name="hello"> | |
<h1>Paginated:</h1> | |
<ul> | |
{{#each paginated}} | |
<li>{{value}}</li> | |
{{/each}} | |
</ul> | |
<a href="#" class="load-more">Load more</a> | |
<h1>Searched:</h1> | |
<ul> | |
{{#each searched}} | |
<li>{{value}}</li> | |
{{/each}} | |
</ul> | |
</template> |
This file contains 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'); | |
function searchQuery(text) { | |
var query = new RegExp(text, 'i'); | |
return Items.find({value: query}); | |
} | |
if (Meteor.isClient) { | |
Session.set('paginatedCount', 10); | |
Session.set('searchKeyword', '23'); | |
Template.hello.helpers({ | |
paginated: function() { | |
return Items.find({}, {sort: {value: 1}, limit: Session.get('paginatedCount')}); | |
}, | |
searched: function() { | |
return searchQuery('23'); | |
} | |
}); | |
Template.hello.events({ | |
'click .load-more': function(e) { | |
e.preventDefault(); | |
Session.set('paginatedCount', Session.get('paginatedCount') + 5); | |
} | |
}); | |
Deps.autorun(function() { | |
window.paginated = Meteor.subscribe('paginatedItems', Session.get('paginatedCount')); | |
}); | |
Deps.autorun(function() { | |
window.searched = Meteor.subscribe('searchedItems', Session.get('searchKeyword')); | |
}); | |
} | |
if (Meteor.isServer) { | |
if (Items.find().count() == 0) { | |
for (var i = 0; i < 1000; i++) | |
Items.insert({value: "" + i}); | |
} | |
Meteor.publish('paginatedItems', function(limit) { | |
return Items.find({}, {sort: {value: 1}, limit: limit}); | |
}); | |
Meteor.publish('searchedItems', function(text) { | |
return searchQuery(text); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment