Dealing with Mongoid\Mongodb embedded documents


#1

Stackoverflow link:

Updated json: http://paste.kde.org/p03df54cd/

My current models:

App.Player = DS.Model.extend
  name: DS.attr 'string'
  abilities: DS.hasMany App.Ability, {embedded: 'always'}
  build_orders: DS.hasMany App.BuildOrder, {embedded: 'always'}
  units: DS.hasMany App.Unit, {embedded: 'always'}
  army_values: DS.hasMany App.ArmyValue, {embedded: 'always'}
  is_winner: DS.attr 'boolean'
  race: DS.attr 'string'
  pick_race: DS.attr 'string'
  color: DS.attr 'string'
  result: DS.attr 'string'
  handicap: DS.attr 'number'
  region: DS.attr 'string'
  apm: DS.attr 'number'
  is_human: DS.attr 'boolean'
  team_num: DS.attr 'number'

App.Team = DS.Model.extend
  players: DS.hasMany App.Player, {embedded: 'always'}

App.Team.reopenClass
    url: '/teams?ids=%@'

App.Replay = DS.Model.extend
  teams: DS.hasMany App.Team, {embedded: 'load'}
  engagements: DS.attr 'raw'
  buildOrderExtracted: DS.attr 'boolean'
  baseBuild: DS.attr 'number'
  frames: DS.attr 'number'
  game_type: DS.attr 'string'
  build: DS.attr 'number'
  map_hash: DS.attr 'string'
  map: DS.attr 'string'
  expansion: DS.attr 'string'
  started_at: DS.attr 'date'
  finished_at: DS.attr 'date'
  message: DS.attr 'string'
  category: DS.attr 'string'
  gateway: DS.attr 'string'

So the idea is, App.Replay.find() should contain replay data with team_ids, when we choose specific replay it sideloads teams. Every team got a “fake” id contains replayId + teamId (1 or 2).

The problem is, the Player model doesn’t have unique id, it should be accessed only through the Team model, but if I choose another replay the Player models from the previous replay are kept.

I tried to do something like this:

App.ReplayRoute = Ember.Route.extend
  model: (params) ->
    App.Replay.find(params.replay_id)
  setupController: (controller, model) ->
    model.get("teams").forEach( (team) ->
      team.reload() if team.get("isLoaded")?
    )
    controller.set('content', model)

But then I get this error: Extract requested, but no data given for App.Team. This may cause weird problems and then endless loading.

I got many nested models, and giving every model unique id seems like bad solution, I think a model beforeLoad or sideload overwrite would be a better option.

Thanks in advance, BBLN.