Hi fellows ! Iām doing a web-app, where in a database are continuously inserted new items by some scraping bots.Iām using meteorhacks:search-source to filter these items in the homepage of the app.The problem is that when a new item is added by the scraping bots, itās not displayed in real time, unless i do a manual refresh of the page.What i want achieve is that the app is updated in real time whenever an item is added in the database andat the same time it must satisfy the searchās criteria.
EX:
1)If i donāt search anything(""), every new item inserted should be displayed in real time
2)If i search āRomeā, every new item that contains āRomeā in the title or description should be displayed in real time, the other shouldnāt
Hereās my code:
//SERVER
SearchSource.defineSource('jobs_available', function(searchText, options) {
var description_regex, s_keyword_regex, selector, title_regex;
options = { //options to pass with the mongoDB selector
sort: {
date: -1
},
limit: 10
};
if (searchText) { //If some text is searched
s_keyword_regex = '.*' + searchText + '.*'; //Compiling the regex exp for title,description
title_regex = {
title: {
'$regex': s_keyword_regex,
'$options': 'i'
}
};
description_regex = {
description: {
'$regex': s_keyword_regex,
'$options': 'i'
}
};
selector = { //Creating the selector for mongoDB
$or: [title_regex, description_regex]
};
return Jobs.find(selector, options).fetch(); //Return the results to the client
} else {
return Jobs.find({}, options).fetch(); //If no text is inserted, return the last 10 items inserted
}
});
//CLIENT
Template.jobs.onCreated(function() {
return JobSearch.search(""); // First time search
});
Template.jobs.helpers({
js: function() { // display the items in the template, with #each
return JobSearch.getData({
transform: function(matchText, s_keyword_regex) { // Add effect on matching text
return matchText.replace(s_keyword_regex, "<span><b>$&</b></span>");
},
sort: {
date: -1
}
}, true); //true, returns a cursor instead of a fetched array
},
//SEARCH INPUT
Template.search_bar.events({
"keyup input": _.throttle(function(e) {
var text;
text = e.currentTarget.value.trim();
return JobSearch.search(text);
}, 300)
});
I hope i explained myself! I tried with cursor.observe(added:) but then i donāt know how to retrigger another search with the same parameters from the server side