Emberjs + data + rails - Uncaught TypeError: Unable to call the 'map' method from undefined

When I try to load data from rails db with emberjs + ember data, I get this error

Uncaught TypeError: Unable to call 'map' method from undefined

Here's the coffescript code:

window.Cosmetics = Ember.Application.create

Cosmetics.store = DS.Store.create
  revision: 4
  adapter: DS.RESTAdapter.create 
    bulkCommit: false

Cosmetics.admin_user = DS.Model.extend({
  name: DS.attr('string')
  email: DS.attr('string')
});

Cosmetics.view = Ember.View.extend
  templateName: 'ember/views/aaa'

Cosmetics.admin_user.reopenClass
  url: 'admin/user'

Cosmetics.store.findAll(Cosmetics.admin_user)

The controller receives the correct JSON data. I tried to match the code with examples found over the Internet, but nothing helped. Any ideas? I guess I'm doing it wrong. Thanks in advance for any help.

+5
source share
5 answers

ember-data expects findAll to be nested in a pluralizable form of the model:

The following data are expected:

{
    "users": [
        {
            "activated": null, 
            "created_at": "2012-05-14T19:35:44Z", 
            "email": "john@doe.com", 
            "id": 1, 
            "name": "john doe", 
            "updated_at": "2012-05-15T20:23:06Z"
        }
    ]
}

What does it get in your example:

[
    {
        "activated": null, 
        "created_at": "2012-05-14T19:35:44Z", 
        "email": "john@doe.com", 
        "id": 1, 
        "name": "john doe", 
        "updated_at": "2012-05-15T20:23:06Z"
    }
]

, ember, JSON "" "users", , "map" "undefined".

+3

. node JSON index . :

format.json { render json: @things }

:

format.json { render json: { things: @things }}

, Ember- root node JSON, Rails .

, .

+3

, .

,

App.Event = DS.Model.extend({
  name: DS.attr('string'),
});

App.Event.reopenClass({
  url: 'api/event'
})

ember json, - json[plural] findAll, json['events'], json['api/events'] , , . , , .

, :

def index
  respond_to do |format|
    format.json { render json: { 'api/events': Event.all } }
  end
end
+3

!

, , , . , :

Cosmetics.store = DS.Store.create
  revision: 4
  adapter: DS.RESTAdapter.create 

bulkCommit: false Uncaught TypeError: Cannot call method 'map' of undefined.

, -, ember- rails.

It seems that I was a little hasty, as if I had solved the problem, called out strangers; |

0
source

The main cause of the problem is the pluralize function in rest_adapter.js , so the solution should fix it;)

  DS.RESTAdapter.reopenClass({
    pluralize: function(name) {
      if (!this.plurals[name])
        name = name.substring(name.lastIndexOf('/')+1);

      return this.plurals[name] || name + "s";
    }
  });
0
source

All Articles