Reputation: 2058
Following the Angular-Meteor tutorial step 9, I'm trying to create an Angular directive that uses a Meteor collection.
This file is in the root folder:
TicTacToeBoards = new Meteor.Collection("tic_tac_toe_boards");
if (Meteor.isServer) {
Meteor.publish('TicTacToeBoards', function() { return TicTacToeBoards.find(); });
}
This file is in the /client folder:
angular.module('TicTacToe').directive('tictactoegraph', function() {
return {
templateUrl: 'client/graph/tictactoegraph.ng.html',
scope: true,
controller: function($scope, $meteor, Sigma, TicTacToeClass) {
$scope.TicTacToeBoards = false;
$meteor.subscribe('TicTacToeBoards').then(function(subscriptionHandle){
$scope.TicTacToeBoards = $meteor.collection(TicTacToeBoards);
});
},
link: function($scope, element, attrs) {
// TODO: Ask SO if there's a better way to wait on the subscription....
$scope.$watch('TicTacToeBoards', function(newValue, oldValue) {
if ($scope.TicTacToeBoards) {
console.log($scope.TicTacToeBoards); // An array of objects.
var nextBoards = $scope.TicTacToeBoards.find({ numberOfMoves: 0 });
}
});
}
}
}
Unfortunately, it gives an error:
TypeError: $scope.TicTacToeBoards.find is not a function
It appears that $scope.TicTacToeBoards
is not a Mongo cursor, but is the array of objects that TicTacToeBoards.find() would return. Why isn't it a cursor?
Upvotes: 0
Views: 225
Reputation: 3185
You are right, $meteor.collection doesn't return a cursor, it returns an array of type AngularMeteorCollection which is different: http://angular-meteor.com/api/AngularMeteorCollection
It does that because we wanted to give Angular developers a regular array with ll it's API to work easily with.
It's an interesting idea to add a find
function to that array though.
Do want want to use that function to return a filtered object?
You could use filters for that, but maybe we can add this option as well
Upvotes: 1