2014-04-18 3 views
0

J'essaie d'utiliser une simple association hasMany ember-data. Données préparées pour sideload, ids set. Cependant, la durée d'association résultante est nulle. Et les données ne sont pas affichées.Ember Data hasMain sideload renvoie 0 longueur

JSBin: http://jsbin.com/OxIDiVU/378/edit

code de cette JSBin:

App = Ember.Application.create(); 

App.Router.map(function() { 
    this.resource('flags'); 
}); 

App.FlagsRoute = Ember.Route.extend({ 
    model: function() { 
    return this.store.find('flag'); 
    } 
}); 

App.FlagsController = Ember.ArrayController.extend({ 
}); 

App.ApplicationController = Ember.Controller.extend({ 
    init: function() { 
    this.transitionToRoute('flags'); 
    } 
}); 

App.ApplicationAdapter= DS.RESTAdapter; 

App.Flag = DS.Model.extend({ 
    country: DS.attr('string'), 
    colors: DS.hasMany('color') 
}); 

App.Color = DS.Model.extend({ 
    name: DS.attr() 
}); 

$.mockjax({ 
    url: '/flags', 
    dataType: 'json', 
    responseText: { 
    flags: [ 
     { 
     id: 1, 
     country: 'Germany', 
     color_ids: [1, 2, 3] 
     }, 
     { 
     id: 2, 
     country: 'Russia', 
     color_ids: [2, 4, 5] 
     }, 
     { 
     id: 3, 
     country: 'USA', 
     color_ids: [2, 4, 5] 
     } 
    ], 
    colors: [ 
     { 
     id: 1, 
     name: "black" 
     }, 
     { 
     id: 2, 
     name: "red" 
     }, 
     { 
     id: 3, 
     name: "yellow" 
     }, 
     { 
     id: 4, 
     name: "white" 
     }, 
     { 
     id: 5, 
     color: "blue" 
     } 
    ] 
    } 
}); 

    <script type="text/x-handlebars" data-template-name="flags"> 
    <table class="table table-bordered table-striped"> 
     <thead> 
     <tr> 
      <th>Country Name</th> 
      <th>Flag colors number</th> 
      <th>Flag color names</th> 
     </tr> 
     </thead> 
     <tbody> 
     {{#each}} 
     <tr> 
      <td>{{country}}</td> 
      <td>{{colors.length}}</td> 
      <td> 
      {{#each color in colors}} 
       {{color.name}}<br> 
      {{/each}} 
      </td> 
     </tr> 
     {{/each}} 
     </tbody> 
    </table> 
    </script> 

Répondre

1

Commander la section sur JSON conventions. Le problème est que le ember attend colors au lieu de color_ids.

http://jsbin.com/OxIDiVU/380/

flags: [ 
    { 
    id: 1, 
    country: 'Germany', 
    colors: [1, 2, 3] 
    }, 
    { 
    id: 2, 
    country: 'Russia', 
    colors: [2, 4, 5] 
    }, 
    { 
    id: 3, 
    country: 'USA', 
    colors: [2, 4, 5] 
    } 
], 
+0

Merci! J'ai vérifié cette section avant, mais j'ai manqué ce point. Le vrai problème est que passer color_ids est un comportement par défaut pour ActiveModel :: Serializers, et il est facile à négliger. –

+0

J'utilise maintenant DS.ActiveModelSerializer, où tous ces problèmes sont résolus. Il a besoin de correctifs, pour corriger la sérialisation hasMany, mais c'est facile à faire. –

+0

Pourriez-vous s'il vous plaît expliquer comment patcher? J'ai ouvert une question pour un problème similaire. http://stackoverflow.com/questions/23823850/emberjs-data-hasmany-sideloading-with-activemodelserializers – kunerd