Ember App Kit with ember data
Asked Answered
T

3

5

I'm trying to start a new project with ember app kit and ember data using ES6. I've managed to create a store using the following code in adapter.js

var ApplicationAdapter = DS.FixtureAdapter.extend();
export default ApplicationAdapter;

However, I'm failing to create a model and access it. In models/account.js I have this

var Account = DS.Model.extend({
  name: DS.attr('string')
});

Account.FIXTURES = [
  {
    'id': 1,
    'name': 'Acc 1'
  }, {
    'id': 2,
    'name': 'Acc 2'
  }
]

export default Account;

and in my routes/accounts.js I have this:

var AccountsRoute = Ember.Route.extend({
  model: function() {
    var store = this.get('store');
    return store.find('account');
  }
});
export default AccountsRoute;

At this stage I'm simply trying to get a list of accounts from the fixtures displayed on screen. The route works nicely and if I put in static data (like the index route) then all works fine. However, with the code above, I run into trouble

DEPRECATION: Action handlers contained in an `events` object are deprecated in favor of putting them in an `actions` object (error on <Ember.Route:ember352>)
    at Object.triggerEvent (http://localhost:8000/vendor/ember/index.js:30519:13)
    at trigger (http://localhost:8000/vendor/ember/index.js:29641:16)
    at handleError (http://localhost:8000/vendor/ember/index.js:29903:9)
    at invokeCallback (http://localhost:8000/vendor/ember/index.js:8055:19)
    at null.<anonymous> (http://localhost:8000/vendor/ember/index.js:8109:11)
    at EventTarget.trigger (http://localhost:8000/vendor/ember/index.js:7878:22)
    at http://localhost:8000/vendor/ember/index.js:8180:17
    at Object.DeferredActionQueues.flush     (http://localhost:8000/vendor/ember/index.js:5459:24)
    at Object.Backburner.end (http://localhost:8000/vendor/ember/index.js:5545:27) index.js:394
Error while loading route: 
Object {readyState: 4, getResponseHeader: function, getAllResponseHeaders: function,   setRequestHeader: function, overrideMimeType: function…}
 index.js:394
Uncaught #<Object> index.js:30566

Where am I going wrong?

Thigpen answered 11/9, 2013 at 13:31 Comment(0)
C
7

Your Account model is using the DS.RESTAdapter instead of the DS.FixtureAdapter, because you are setting the adapter in ApplicationAdapter, the expected is AccountAdapter. So you receive an error from the ajax, probably because the url does not match a server.

To configure the DS.FixtureAdapter per model use:

var AccountAdapter = DS.FixtureAdapter.extend();
export default AccountAdapter; 

Or as global adapter for all models:

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

I hope it helps

Corpuz answered 11/9, 2013 at 13:41 Comment(8)
I'm using the fixtureadapter. It's almost exactly as you've suggestedThigpen
Sorry. I mean, you are using the rest adapter, because of the wrong configuration. But you want to use the fixture ...Corpuz
I'm not using the rest adapter, i'm using the fixture adapter. Unless i'm missing something!? Nowhere do I have restadapter definedThigpen
The rest adapter is the default. So ember is doing the following: try to take the adapter from model, in your case AccountAdapter. AccountAdapter not found. Now take the global adapter, the default adapter isn't changed so it will take the rest. My first example in the answer show how to set the adapter per model, and the second is, how to override the default adapter.Corpuz
I tried the first and it didn't work. Next I used your global adapter (which is what I was trying in the first place). It works beautifully. For some reason I was confused having read in the docs that a store is available in ember 1.0 For what its worth, I simply added the adapter code in the app.js. I'm not sure if thats the right place, but it works. If not please enlighten me.Thigpen
What is the version of your ember-data?Corpuz
Version: v1.0.0-beta.1-167-g54915f4Thigpen
In the 1.0.0-beta.2, I get ApplicationAdapter and AccountAdapter working. Maybe your problem just happen in 1.0.0-beta.1Corpuz
K
2

I think the real issue was that you defined your Fixture Adapter in adapters/adapter.js.

When you called:

store.find('account');

It correctly found the model but then looked for the correct adapter. You don't have an adapters/account.js so it used the application default, which has been mentioned is a RESTAdapter.

To get your example working, just change the filename.

Koss answered 6/1, 2014 at 4:50 Comment(0)
S
1

I was getting the same error...

However I was able to fix this by importing my ApplicationAdapter, and using that to define my store:

app/adapters/application.js:

var ApplicationAdapter = DS.FixtureAdapter.extend();

export default ApplicationAdapter;

app/store/application.js:

import ApplicationAdapter from 'appkit/adapters/application';
var Store = DS.Store.extend({
    adapter: ApplicationAdapter
});

export default Store;

Keep in mind I have not changed the default application name away from appkit yet, you may have to change this name or the paths to make this function properly for you.

Schoolman answered 29/9, 2013 at 4:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.