Sebastian 506563
Sebastian 506563

Reputation: 7228

Ember data store return undefined record from FIXTURES

I am new in ember and i am trying to simply get log of items stored by Fixtures model and i cannot. Here is what I have done:

app.js

    App = Ember.Application.create();

    App.Store = DS.Store.extend({
        adapter: DS.FixtureAdapter.extend()
    });

    App.Documenter = DS.Model.extend({
        firstName: DS.attr('string'),
        lastName: DS.attr('string')
    });

    App.Documenter.FIXTURES = [
      { id: 1, firstName: 'Trek', lastName: 'Glowacki' },
      { id: 2, firstName: 'Tom', lastName: 'Dale' }
    ];


App.IndexRoute = Ember.Route.extend({
    controllerName: 'application',
    actions: {
        addtostore: function () {
        },
        displaystore: function () {
             var obj =  this.store.all('documenter');
            console.log(obj.objectAt(0).firstName); //here i get undefined

        }
    }

});

html:

<script type="text/x-handlebars">
    <h2> Ember POC</h2>
    <p>POC</p>
    {{outlet}}
     <button {{action 'displaystore'}}>TestButton</button>
</script>

I've looked at few answers already on stackoverflow: Ember-Data Fixture Adapter Ember App Kit with ember data

but still I dont get, why button TestButton dont log into console. I tried many ways but always it is undefined

Upvotes: 1

Views: 723

Answers (2)

Sebastian 506563
Sebastian 506563

Reputation: 7228

Because I am fresh in Ember, I made few common mistakes. I did not understand routes, handlebars, etc., and I messed up. The problem was that I did not assign the model to a controller.

App.IndexRoute = Ember.Route.extend({
    model: function () {
        var store = this.get('store');
        return store.findAll('documenter');
    }
});

Upvotes: 0

Kingpin2k
Kingpin2k

Reputation: 47367

No need to define the store, and your adapter should be defined like so:

App.ApplicationAdapter = DS.FixtureAdapter;

And all only returns records that have already been found and are in the store. The FIXTURES hash isn't automatically loaded into the store, you still need to use find in order to get records from the FIXTURES hash.

displaystore: function () {
    this.store.find('documenter').then(function(records){ // this is async
      console.log(records.get('firstObject.firstName'));
    });
}

It's doubtful you would want to call find from the action like this, since it'd call back to the server each time, generally you'd get the records in the route (or even prime your store in the route so you can use all).

Upvotes: 4

Related Questions