0
votes

I've created some dummy content and let ember specify the id and created a Ember Business object like so:

Yrf.Business = DS.Model.extend({
    name: DS.attr('string'),
    rating_img_url: DS.attr('image'),
    is_closed: DS.attr('boolean'),
    address1: DS.attr('string'),
    address2: DS.attr('string'),
    phone: DS.attr('string'),
    url: DS.attr('string'),
    photo_url: DS.attr('image')
});

From there i import dummy Yelp Json and create a fixture out of it

Yrf.Business.FIXTURES = [{...}] // Data goes here

Now when i do this ember decides to create an id for me. Here's an example of one

xSKEe6upPy-pFkLAW1O5uw.

How do i change the Id's to make them sequential and start at 0 (e.g. 0-9999) ? I need to be able to reference them like so:

this.store.find('Business', 1); // Get item 1 in my model
this.store.find('Business',35234);
this.store.find('Business',randomInteger);

In the end, i'd like to generate a random number and grab a single item from my Fixtures.

1
This is just an idea. May be extending the adapter and overriding the generateIdForRecord method might work. github.com/emberjs/data/blob/v1.0.0-beta.8/packages/ember-data/… - blessenm

1 Answers

1
votes

Hmm, did you think about doing something like this:

App.__container__.lookup('store:main')
  .find('yourModelName').then(function(yourModelName) {
    var ids = yourModelName.getEach('id'));
    return _.sample(ids) // http://underscorejs.org/#sample
  });

So in your route:

model: function() {
  this.store.findAll('yourModelName');
}

Then in your controller:

randomBusinessId: function() {
  var ids = this.getEach('id');
  return _.sample(ids); // http://underscorejs.org/#sample
}.property('model.[]'),


randomBusiness: function() {
  return this.objectAt(this.get('randomBusinessId'));
}.property('randomBusinessId')

There's a bunch of untested code in here but I hope it can be of help for you!