2

I am new to Ember and not very strong in JavaScript.

I am trying to use Java Spring MVC on a back-end with Ember on the front end. When I am trying to access a page. Ember makes a request for data to the server. Server answers with an Array, but the Array is not retrieved on the Ember side properly. I get an exception.

Error while loading route: Error: No model was found for '0'
at new Error (native)
at Error.Ember.Error (http://localhost:8080/libs/js/ember.js:844:19)
at Ember.Object.extend.modelFor (http://localhost:8080/libs/js/ember-data.js:9805:33)
at JSONSerializer.extend.extractArray (http://localhost:8080/libs/js/ember-data.js:3172:28)
at superWrapper (http://localhost:8080/libs/js/ember.js:1239:16)
at Ember.Object.extend.extractFindAll (http://localhost:8080/libs/js/ember-data.js:2380:21)
at Ember.Object.extend.extract (http://localhost:8080/libs/js/ember-data.js:2365:37)
at http://localhost:8080/libs/js/ember-data.js:10396:34
at invokeCallback (http://localhost:8080/libs/js/ember.js:9753:19)
at publish (http://localhost:8080/libs/js/ember.js:9423:9) 

When debuging the javascript i found out that there is for in loop looping over an array

 for (var prop in payload)

This for in loop is not retrieving the array elements, but is rather going throw properties. It might be because of what they talk about in this question on SO.

The - for me - problematic code is on github here.

Am I doing something wrong? should I report a bug?

I have created the following Ember model:

App.User = DS.Model.extend({
email: DS.attr('string'),
active: DS.attr('boolean'),
});

This is how the mapping looks like on server side

@RequestMapping("/users")
public
@ResponseBody
List<User> users() {
    System.out.println("getting users");
    List<User> list = new LinkedList<User>();
    list.add(new User(1,"test@b.c"));
    list.add(new User(2,"test2@b.c"));
    list.add(new User(3,"test3@b.c"));
    return list;
}

This JSON I got from fiddler:

[{"id":1,"email":"test@b.c","active":true},{"id":2,"email":"test2@b.c","active":false},{"id":3,"email":"test3@b.c","active":false}]
Community
  • 1
  • 1
Jiri Peinlich
  • 739
  • 1
  • 6
  • 10

1 Answers1

2

By default, I believe ember-data expects your JSON response to be prefixed with the model name, even if it's an array. This means you're data will need to be formatted like this:

{
   "users":[
      {
         "id":1,
         "email":"test@b.c",
         "active":true
      },
      {
         "id":2,
         "email":"test2@b.c",
         "active":false
      },
      {
         "id":3,
         "email":"test3@b.c",
         "active":false
      }
   ]
}
Peter Brown
  • 50,956
  • 18
  • 113
  • 146