goinstant / goangular

AngularJS bindings for GoInstant. Build realtime, multi-user apps with AngularJS and GoInstant easily. https://developers.goinstant.com/v1/GoAngular/index.html
BSD 3-Clause "New" or "Revised" License
137 stars 30 forks source link

Query Interface Discussion #65

Open colinmacdonald opened 10 years ago

colinmacdonald commented 10 years ago

Alternative 1: Add a $query method to $goKey and a $key method to $goQuery

var todos = $goKey('todos').$query(args);
var todos = $goQuery('todo's, args).$key('key')

Alternative 2: Add a $query method to model

var todos = $goKey('todos').$sync();
todos.$query(args);

var stuff = todos.$key('users').$query({ thing: false }).$key('whatever');
mattcreager commented 10 years ago

I feel that the second option you've suggested provides the more intuitive interface. I'm slightly concerned it will impact the extensibility of the model!

colinmacdonald commented 10 years ago

After looking a how query is used with the todo example app I find it much cleaner to just use a goKey model and filter the data in that model rather than overwriting the model with a query model each time you wish to perform a different query.

$scope.todos = $goKey('todos').$sync();
<li ng-repeat="todo in todos | keyFilter | filter:{complete:false/true} | limitTo:5 | orderBy:'-$name'">

VS

$scope.showActive = function() {
  $scope.todos = $goQuery('todos', {complete:false}, { limit: 5}).$sync();
};

$scope.showComplete = function() {
  $scope.todos = $goQuery('todos', {complete:true}, {limit:5}).$sync();
};

$scope.showAll = function() {
  $scope.todos = $goQuery('todos', {limit:5}).$sync();
};
<li ng-repeat="todo in todos | keyFilter">

Using $goQuery appears to be just a more complicated and foreign way (for angular devs) to filter/sort/limit data in comparison to filters which becomes super easy with #32

ianlivingstone commented 10 years ago

The difference is that Query will be faster and more flexible then Angular's filter/sort/limit that updates in realtime. The todo example doesn't do it justice.

colinmacdonald commented 10 years ago

Not sure how it can be more flexible when you can create your own filter for any specific purpose and chain filters together. I see the advantage of using query as a preliminary filter before applying the realtime angular filtering, but that can be done using angular filters too.

var data = $goKey('todos').$sync();
data = $filter('keyFilter')(data);

$scope.data = $filter('orderBy')($scope.data, '-$name');

All that has to be done to make this work like goQuery is to apply that filter each time the model changes, at the moment it is just being executed the once.