coffeescript - Backbone.js: Grabbing the length of a collection for use with random number -
i want grab length of collection use in random number generator. want view shows 1 model of collection, randomly generated. i'm using coffeescript, btw
so far i've tried stuff like
@collection.fetch data: id: math.floor((math.random()*@length)+1)
which won't work because length isn't there until after it's fetched... i've tried couple of other methods, such grabbing after fetching, length zero.
anyone give me idea of how this?
edit: javascript can't read coffee
this.collection.fetch({ data: { 'id': math.floor((math.random() * length) + 1) } });
i had same task in past. used underscore _.sample method. please try _.sample(collection) return random model collection or better _.sample(collection, 4) 4 random models.
Comments
Post a Comment