2

I'm busy following this Ember.js tutorial, but I'm trying to implement it using the Ember 2.0 way of doing things (modules, using Ember CLI and the ember-cli-rails gem). It's rather difficult since none of the Ember guides follow these conventions.

As per the tutorial, I'm using Rails as a JSON API and it looks like everything works like it should in terms of serving the proper JSON responses. Problem is, I can't get my leads model to work.

I'm getting a TypeError: Cannot read property 'typeKey' of undefined error from ember.debug.js. I'm also getting a undefined is not a function error from ember.adapter.js

My project looks as follows:

app/store.js

  import DS from 'ember-data';

  export default DS.RESTAdapter.reopen({
    url: 'https://localhost:3000',
    namespace: 'api/1'
  });

app/router.js

import Ember from 'ember';
import config from './config/environment';

var Router = Ember.Router.extend({
  location: config.locationType
});

Router.map(function() {
    this.resource('leads', { path: '/' });
});

export default Router;

app/adapters/application.js

import DS from "ember-data";

var ApplicationAdapter = DS.ActiveModelAdapter.extend({
    host: 'http://localhost:3000',
    namespace: 'api/v1'
});

export default ApplicationAdapter;

app/models/lead.js

import DS from 'ember-data';

export default DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  email: DS.attr('string'),
  phone: DS.attr('string'),
  status: DS.attr('string', { defaultValue: 'new' }),
  notes: DS.attr('string'),
});

app/routes/leads.js

import Ember from 'ember';

export default Ember.Route.extend({
    model: function() { return this.store.find('lead') }
});

I don't see any HTTP requests being made to Rails, so I assume that it breaks even before trying to use the API. Can anybody please point out what I'm doing wrong here?

4
  • Are you seeing anything in your browser console? Commented Mar 15, 2015 at 5:36
  • Yup - third paragraph from top lists the two errors I'm getting in the Chrome console. Commented Mar 15, 2015 at 6:12
  • As far as I can see there is nothing wrong with this code. Can you recreate the problem in a fiddle? BTW why are you reopening RESTAdapter? This is dangerous. Commented Mar 15, 2015 at 22:51
  • According to that tutorial, this is how you configure Ember Data in store.js - by using reopen. Is there another way? I've added the whole project to a Git repo here: github.com/constantm/ember-crm/tree/master/frontend. I have no idea how to get this to work with a Fiddle. :) Commented Mar 16, 2015 at 7:29

1 Answer 1

1

Looks like the issue was store.js, which shouldn't be used here. Not sure why, but removing it causes everything to work as it should.

UPDATE: So, upon learning more of how Ember works, I now know the issue was not the file, but that Rails returns the wrong type of json back. The Ember REST adapter expects json keys to use an underscore, eg. first_name, while Rails returns it in camel case, eg. firstName.

To use Ember with Rails, you need to use the ActiveModel adapter. The newest version of ActiveModelSerialisers(AMS) supports JSON API.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.