0

I'm using Ember: 1.3.0-beta.2 and Ember Data: 1.0.0-beta.2

Here's my setup:

Models

Rise.User = DS.Model.extend({
  //....
  user_training_plans: DS.hasMany('training_plan', { async: true }),
  user_training_histories: DS.hasMany('training_history', { async: true }),
});

Rise.TrainingHistory = DS.Model.extend({
  //....
  user: DS.belongsTo('user', { async: true }),
  training_resource: DS.belongsTo('training_resource', { async: true })
});

Rise.TrainingPlan = DS.Model.extend({
  //....
  user: DS.belongsTo('user', { async: true }),
  training_resource: DS.belongsTo('training_resource', { async: true })
});

In a controller action, I'm trying to "move" a training resource from a user's training plan to training histories. I'd like to determine if the training resource has already been added to training histories before adding it.

I'd like to do something like this, which is currently in the training plans route

markCompleted: function(trainingPlanItem) {
  completed_training_resource_id = trainingPlanItem.get('training_resource.id');

  this.controller.get('user_training_histories').then( function(user_training_histories) {
    var already_exists = user_training_histories.any( function(item) {
      var item_id = item.get('training_resource').get('id');
      if (item_id == completed_training_resource_id) {
        return true;
      }
    });

    // ...continues
  });
}

Of course, item.get('training_resource').get('id'); returns a promise if the data has not been fetched, so this if (item_id == completed_training_resource_id) comparison line doesn't work correctly.

Is there a way to run any and use a promise inside? Or is there a way to see the training_resource_id without loading the training resource? This is a belongs_to relationship, so the training resource id is fetched from the server.

Community
  • 1
  • 1
claptimes
  • 1,615
  • 15
  • 20
  • I think you should be able to access training_resource_id in your user_training_history, like this: item.get('training_resource_id') – fanta Dec 13 '13 at 15:36
  • Unfortunately, that value is `undefined`... It would make sense that I should be able to access that value though, since my server passed the associated id to Ember. – claptimes Dec 13 '13 at 16:37
  • I haven't used that ember-data version yet. Have you tried with trainingResourceId ? what if you define that property in the model ?, maybe the model needs it in order to map it – fanta Dec 13 '13 at 16:43
  • Tried that as well with the same result. I could define that property in the model, and might end up going that route, but I'm frustrated because Ember already knows what that model id is and has it saved...somewhere. – claptimes Dec 13 '13 at 17:03

1 Answers1

1

Get belongsTo ID without fetching record

I'm not sure if i agree with this route, but you can grab it off the data obj

item.get('data.training_resource.id')
Community
  • 1
  • 1
Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
  • 1
    Alternatively, `item.get('data.training_resource.id')`. If you have any better suggestions for checking the `user_training_histories` for duplicates, please let me know! Maybe there's a better approach than using `any`. – claptimes Dec 13 '13 at 17:46