How to filter subscribed data Angular-Meteor way


#1

Hi. I do not know if I can post Angular-Meteor questions on this form, but looks that there are few questions about this subject. Please let me know if I can’t publish those questions here. :smile:

So, let say we need to show filtered tasks on view, first step we got all the list of tasks buy publishing/subscribing:

Publish:

Meteor.publish("tasks", function(options){
    return Tasks.find(options);
});

Subscribe:

var allTasks = $meteor.collection(Tasks).subscribe('tasks', {});

And now, let say I need to see in $scope only tasks that have ‘active’ variable set to true.

Something like this:

$scope.active_tasks = getFilteredTasks(allTasks, {active: true}) 

How do I replace the getFilteredTasks(allTasks, {active: true}) to get tasks that have only active==true variable?

I know that we can set ‘options’ when subscribing to tasks like:

{active: true}

But it isn’t helps to resolve the issue. The goal is to subscribe only once and later use filters to show only part of tasks.

I still not finished the Angular-Meteor tutorial, so it may be explained later, if you can please point me on right tutorial it will greatly speed-up my learning.

Thanks in advance.


#2

OK. It looks like the solution is on Angular side.
So in controller we get all the tasks:

$scope.active_tasks = $meteor.collection(Tasks);

And on view side we filter them:

<input ng-model="freeText">
<tr ng-repeat="task in active_tasks | filter:freeText ">
    <td>{{ task }}</td>
</tr>

:smile:


#3

The above is one solution.
You could also use

$scope.$meteorSubscribe ('tasks'); 
$scope.activeTasks = $meteor.collection (function (){ // active tasks will only be the 
    return Tasks.find (active: true) ; 
})

Read about the difference between subscriptions and collections in Meteor Angularjs here https://medium.com/@tally_b/coll-pub-sub-with-angular-meteor-cb13fe48f570. Check out the example and github code for more details.