MartinElvar
MartinElvar

Reputation: 5804

Why is my associations not getting binded?

Ember : 1.1.2

Ember Data : 1.0.0-beta.3

Handlebars : 1.0.0

Hi fellows,

After upgrading my application with EmberData 1.0.0-beta.3 i am struggling to traverse my data the way i want to.

In my application you are creating job, and you have to pick a Field of service.

MyApp.ServiceField = DS.Model.extend
  name: DS.attr('string')
  description: DS.attr('string')
  services: DS.hasMany('service')

In addition you pick a service in that field.

MyApp.Service = DS.Model.extend
  name: DS.attr('string')
  description: DS.attr('string')
  service_field: DS.belongsTo('serviceField')

Now these services get fetched from the server, when the user visit the new route.

MyApp.Router.map ()->
  @route 'home.index', { path: '/' }
  @resource 'jobs', ->
    @route 'new'


MyApp.JobsNewRoute = Ember.Route.extend
  setupController: (controller) ->
    controller.set 'model', @store.createRecord('job')
    controller.set 'serviceFields',  @store.find('service_field')

Now the server returns the following JSON

{"services":[
  {"id":1,"name":"NLP Coaching","description":"NLP coaching er noget med NLP"}, 
  {"id":2,"name":"Familie rådgiver","description":"Har i problemer i familien? Få dem løst med en Familie rådgiver"}],
 "service_fields": [
   {"id":1,"name":"Coaching","description":"Her står en masse godt om coaching faget","service_ids":[1,2]}]}

This seem to work just fine, as i can see in my inspector that the models are actually created

Data models

Now i want to traverse this in my template, i do this the following way

<p>Creating a new job</p>
<div class="ui input">
  <p>Select a service field</p>

  {{#each serviceFields }}
    <a class="ui label blue">{{name}}</a>
    <br><br>
    {{#each services}}
      <a class="ui label blue">{{name}}</a>
    {{/each}}
  {{/each}}
</div>

This results in

result

As you can see, i only see the Service Field, but can't figure out how to traverse the associated services, please help me out.

Upvotes: 0

Views: 59

Answers (1)

Anton Lantsov
Anton Lantsov

Reputation: 336

ember-data expected other key in json for hasMany association: "services" instead of "service_ids".

your json:

{"id":1,"name":"Coaching","description":"description","service_ids":[1,2]}]}

expected:

{"id":1,"name":"Coaching","description":"description","services":[1,2]}]}

http://emberjs.jsbin.com/eWesIQuV/1

http://emberjs.jsbin.com/eWesIQuV/1/edit example

Upvotes: 1

Related Questions