Victor
Victor

Reputation: 5353

Finding a model in ember.js

I'm completely new to ember js. I've downloaded the last rc 2 version of ember, ember-data.js 12 revision, and looked at the manual and copy-past this code in order to be able to see the GET request to my server:

App = Ember.Application.create();

App.Router.map(function() {
... some resources...
});

App.Store = DS.Store.extend({
    revision: 12 // Default is the REST Adapter 
});

App.Person = DS.Model.extend({
    firstName: DS.attr('string'),
    lastName: DS.attr('string'),
    birthday: DS.attr('date'),

    fullName: function() {
            return this.get('firstName') + ' ' + this.get('lastName');
    }.property('firstName', 'lastName')
});

var person = App.Person.find(1);

And I get the next error:

Uncaught Error: assertion failed: Your application does not have a 'Store' property defined. Attempts to call 'find' on model classes will fail. Please provide one as with 'YourAppName.Store = DS.Store.extend()'

But as I understand I defined the Store property. Maybe I missed something because I havent read the whole manual, but honestly I can't see what's wrong. As I get it after this code I'll see get /post/1/ request to my server, and it should be an amazing thing, but I'm still struggling with this error

Upvotes: 1

Views: 702

Answers (1)

Mike Grassotti
Mike Grassotti

Reputation: 19050

This is happening because ember applications are initialized asynchronously. In general you just define classes when js is being loaded, executable code belongs in hooks/callbacks. Mostly you will be using model find() from the model hook on your routes, but if you really need to do something like this right away you can do this:

App.then(function() {
  console.log('App has been initialized...');
  var person = App.Person.find(1);
});

If you want to experiment with this approach try this jsfiddle which demonstrates using App.then() with the ember-data fixture adapter based on the getting started screencast

Upvotes: 3

Related Questions