ember.js - Getting length of hasMany association -
i have following models defined in ember project (project created ember-cli 0.2.3)
import ds 'ember-data'; var game = ds.model.extend({ title: ds.attr('string'), description: ds.attr('string'), geekrating: ds.attr('number') }); game.reopenclass({ fixtures: [ { id: "1", title: "catan", description: "catan game", geekrating: "5.65"} ] }); export default game; import ds 'ember-data'; var ownedgame = ds.model.extend({ rating: ds.attr('number'), plays: ds.hasmany('gameplay'), game: ds.belongsto('game', {async: true}), playcount: function() { return this.get('plays.length') }.property('plays') }); ownedgame.reopenclass({ fixtures: [ { id: "1", rating: "8.25", game: "1"} ] }); export default ownedgame; import ds 'ember-data'; var gameplay = ds.model.extend({ date: ds.attr('date'), ownedgame: ds.belongsto('ownedgame', {async: true}) }); gameplay.reopenclass({ fixtures: [ {id: "1", date: "2015-01-01", ownedgame: "1"}, {id: "2", date: "2015-02-01", ownedgame: "1"} ] }); export default gameplay;
and in owned-games/index.hbs
<h1>my games</h1> <table class="table table-striped table-condensed table-hover"> <thead> <tr> <th>title</th> <th>description</th> <th>your rating</th> <th>geek rating</th> <th>plays</th> </tr> </thead> <tbody> {{#each game in this}} <tr> <td>{{game.game.title}}</td> <td>{{game.game.description}}</td> <td>{{game.rating}}</td> <td>{{game.game.geekrating}}</td> <td>{{game.playcount}}</td> </tr> {{/each}} </tbody> </table>
i'm trying display length of plays
ownedgame
computed property. note can see i'm using fixture data not sure if might limitation. i'm not getting errors, , list of ownedgames displays fine (just 1 of them now). i'm getting 0 size of plays.
i've read other similar threads issue, , believe have code correct, not sure why isn't working.
update:
i tried doing on each iteration of game:
{{#each play in game.plays}} {{play.date}} {{/each}}
and no output.
update #2:
i created necessary code view list of gameplay's. size 0 until visit list of gameplay's. if go list of ownedgames, length of gameplay's 2, accurate. think need figure out how computed property query data?
with hasmany
relationship, have 2 options on how go fetching side loaded data.
first option specify plays
records want specifying array game-play
ids on owned-game
fixture. make request game-play
automatically , populate model asynchronously.
app.ownedgame.reopenclass({ fixtures: [ {id: 1, rating: 8.25, game: 1, plays: [1,2]} ] });
second option manually make second request fetch game-play
data, populate plays
property of owned-game
asynchronously.
app.indexroute = ember.route.extend({ model: function () { return new ember.rsvp.hash({ gameplay: this.store.find('game-play'), ownedgame: this.store.find('owned-game') }); }, setupcontroller: function(controller, model) { controller.set('model', model.ownedgame); } });
Comments
Post a Comment