How to get random value from Fixture data in Ember

I play with a sample Ember application, which displays all the data stored in the Fixture, and finally tries to show random data from the device.

Full demo here: http://jsbin.com/ifatot/2/edit

Everything works fine, however I cannot get a random index from Ember data. I try to find its length and capture a random index, but I believe that the length is always 0, although I have data.

The function is as follows:

App.ThoughtsController = Ember.ArrayController.extend({ randomMessage: function() { var thoughts = this.get('model'); var len = thoughts.get('length'); var randomThought = (Math.floor(Math.random()*len)); return thoughts.objectAt(randomThought); }.property('model') }); 
+4
source share
1 answer

You must add the length property as another, depending on the computed randomMessage property. This will allow the content to complete resolution and have length .

 randomMessage: function() { var len = this.get('length'); var randomThought = (Math.floor(Math.random()*len)); return this.objectAt(randomThought); }.property('model', 'length') 

Here is updated jsbin .

0
source

Source: https://habr.com/ru/post/1493061/


All Articles