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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -