Search code examples
javascriptbackbone.jsfirebasebackfire

How do you pass a variable limit into a Backbone.Firebase.Collection?


The firebase documentation here (https://github.com/firebase/backfire#backbonefirebasecollection) mentions that you can apply a limit to a Backbone.Firebase.Collection:

var Messages = Backbone.Firebase.Collection.extend({
  firebase: new Firebase("https://<your-firebase>.firebaseio.com").limit(10)
});

I would like to make that limit a variable - so I could sometimes show 10 records from the collection and sometimes 100 (as an example).

Can anyone recommend the best way to achieve this?


Solution

  • Firebase now supports query limits, using limitToFirst or limitToLast

    The following code would get the first 2 records.

    var ref = new Firebase("https://dinosaur-facts.firebaseio.com/"); ref.orderByChild("height").limitToFirst(2).on("child_added", function(snapshot) { console.log(snapshot.key()); });

    More info in the docs: https://www.firebase.com/docs/web/api/query/limittofirst.html