Reputation: 3743
Hi and thanks for taking the time to answer my question.
I have the following models:
App.Project = DS.Model.extend({
name: DS.attr('string'),
project_teams: DS.hasMany('project_team'),
});
App.ProjectTeam = DS.Model.extend({
val: DS.attr('number'),
project: DS.belongsTo('project'),
team: DS.belongsTo('team')
});
App.Team = DS.Model.extend({
project_teams: DS.hasMany('project_team'),
name: DS.attr('string')
});
ProjectTeam is basically the link between Project and Team and it holds some extra metadata. A project can have multiple teams working on it through the ProjectTeam model.
I have the following code in my ProjectController:
var project = store.createRecord('project', {
name: projectName
});
project.save().then(function(record){
Ember.run.next(function(){
for(var i = 0; i < valArr.length; i++){
var retTeam;
store.find('team', teamIds[i]).then(function(team){
Ember.run.next(function(){
retTeam = team;
console.log(team);
});
});
console.log(record);
var project_team = store.createRecord('project_team', {
project: record,
val: valArr[i],
team: retTeam
});
project_team.save().then(function(pt){
Ember.run.next(function(){
projectTeamIds.push(pt.get('id'));
if(i === sdeHeads.length){
record.set('project_teams', projectTeamIds);
record.save();
}
});
});
//
}
});
});
First I need to add a Project object which I do. After that, I add multiple ProjectTeam objects and I need to bind them to the newly inserted Project's Id. I've been banging my head trying to figure out why this won't work for hours to no avail.. Was wondering if you could spot my mistake.
I can add Project objects and they are saved into the DB. Also I can add ProjectTeam object however they are not bound to a team or a project. I checked the JSON and those fields (project and team) are null :/
Basically this should be the flow:
EDIT:
Another approach I tried which also did not work..
var project = store.createRecord('project', {
name: projectName
});
for(var i = 0; i < sdeHeads.length; i++){
store.find('team', teamIds[i]).then(function(team){
var project_team = store.createRecord('project_team', {
sde_effort: sdeEffort,
team: team
});
project.get('project_teams').pushObject(project_team);
});
}
project.save();
The other approach was to create a project object, embed the children object and only then save to the db. However, this approach did not work either as now Project_Teams are not added to the db at all. The code within store.find('team', teamIds[i]).then(function(team){
doesnt execute at all. There's either gotta be an easier way to do this or Ember still has a way to go.. Really frustrating..
Thanks for your help!
Upvotes: 1
Views: 80
Reputation: 47367
Well there are a few async issues here. So it's a loop, so it will go from 3 to 9 without ever touching the 99/99* portions. 99 and 99* will execute as the server returns results and save returns, but it won't necessarily be in order (99* could execute before 99, and even more exciting 99 from one iteration of the loop could execute before 99 from another loop (IE retTeam and record are going to be way out of sync with what you think will work.) Additionally you'd have a loop closure issue valArr[i]
should always be out of bounds.
1. project.save().then(function(record){
2. Ember.run.next(function(){
3. for(var i = 0; i < valArr.length; i++){
4. var retTeam;
5. store.find('team', teamIds[i]).then(
99. function(team){
99 Ember.run.next(function(){
99 retTeam = team;
99 console.log(team);
99 });
5. });
6. console.log(record);
7. var project_team = store.createRecord('project_team', {
7 project: record,
7 val: valArr[i],
7 team: retTeam
7 });
8. project_team.save().then(
99* function(pt){
99 Ember.run.next(function(){
99 projectTeamIds.push(pt.get('id'));
99 if(i === sdeHeads.length){
99 record.set('project_teams', projectTeamIds);
99 record.save();
99 }
});
});
9. }
});
});
Something like this should work, Though I'm a little hesitant cause there are a plethora of variables I don't know about: valArr
, sdeHeads
, teamIds
, projectTeamIds
...
project.save().then(function(projectRecord){
var teamHookerUpper = function(teamId, arrVal, idx){
store.find('team', teamId).then(function(team){
var project_team = store.createRecord('project_team', {
project: projectRecord,
val: arrVal,
team: team
});
project_team.save().then(function(pt){
projectTeamIds.push(pt.get('id'));
if(idx === sdeHeads.length){
projectRecord.set('project_teams', projectTeamIds);
projectRecord.save();
}
});
};
for(var i = 0; i < valArr.length; i++){
teamHookerUpper(teamIds[i], valArr[i], i);
}
});
Upvotes: 1