loneleeandroo / ngMeteor

The simplest no-conflict way to use AngularJS with Meteor, Meteorite and Atmosphere Smart Packages.
127 stars 24 forks source link

How to deal two different selectors in same controller with same collection ? #20

Open crapthings opened 10 years ago

crapthings commented 10 years ago

i want to display 2 different queries from notes collection.

  1. query all recent sticky notes.
  2. query all recent notes with note.sticky = false

if u know what i mean.

How do i write code such condition ?

$collection 'Notes', $scope, { creatorId: Meteor.userId() },
    sort:
        createdAt: -1

$collection 'stickyNotes', $scope, { creatorId: Meteor.userId(), sticky: true },
    sort:
        createdAt: -1

<div class="list">
    <a class="item item-divider">sticky</a>
    <a class="item" ng-repeat="note in stickyNotes">
        <h2>[[note.title]]</h2>
    </a>
    <div class="item item-divider">recent</div>
    <a class="item" ng-repeat="note in notes">
        <h2>[[note.title]]</h2>
    </a>
</div>
loneleeandroo commented 10 years ago

The problem here is that the first argument of the $collection service must refer to the name of the collection object, and that name also has to be the same as the publisher function name. So, you would need to have a collection called Notes and a collection called stickyNotes, which is not viable. And adding to the problem, you cannot specify which model you want the fetched array to be bound to because the $collection object will automatically bind the fetched array to $scope.Notes and $scope.stickyNotes. From this, we can see that there are obvious limitations to how the current $collection service works and I'm working on removing these limitations in v0.2 of ngMeteor.

The alternative for now is to use AngularJS filter service to filter your collection. Optionally, you can also use AngularJS orderBy to sort your collection.

Assuming the collection is in an object called Notes, you would get all your notes using:

$collection('Notes', $scope, {creatorId: Meteor.userId()}, {sort: {createdAt: -1}});

And you would filter them in your HTML like so:

<div class="list">
    <a class="item item-divider">sticky</a>
    <a class="item" ng-repeat="note in Notes | filter:{sticky: true}">
        <h2>[[note.title]]</h2>
    </a>
    <div class="item item-divider">recent</div>
    <a class="item" ng-repeat="note in Notes">
        <h2>[[note.title]]</h2>
    </a>
</div>