I have these models:
Fight:
export default Model.extend({
teams: hasMany('team')
});
Team:
export default Model.extend({
closed: attr('boolean'),
fight: belongsTo('fight'),
players: hasMany('player')
});
Player:
export default Model.extend({
team: belongsTo('team'),
nickname: belongsTo('nickname')
});
Nickname:
export default Model.extend({
nickname: belongsTo('nickname')
});
Now I'm in the fight page and I have a list of all the nicknames and with a click on each one I need to:
- if there aren't teams already in fight (this model) I create a team and after that I insert my player in that team;
- if there is a team already in fight and is "NOT CLOSED" I simply insert my player in that last team;
- if there is a team already in fight and is "CLOSED" I need to add a new team for this new player and all others new players until that team is closed.
Everything works with this code:
fight.get('teams').then((all_teams) => {
let count_teams = all_teams.get('length');
if (count_teams > 0) {
let last_team = all_teams.objectAt(count_teams - 1);
if (last_team.get('closed') === false) {
saveNewPlayer(last_team);
} else {
saveNewTeam();
}
} else {
saveNewTeam();
}
function saveNewTeam() {
let new_team = store.createRecord('team', {
fight: fight
});
new_team.save().then((new_team) => {
saveNewPlayer(new_team);
}).catch(() => {
new_team.rollbackAttributes();
});
}
function saveNewPlayer(last_team) {
let newPlayer = store.createRecord('player', {
team: last_team
});
newPlayer.save().then(() => {
// handle aftersave
}).catch(() => {
newPlayer.rollbackAttributes();
});
}
});
But there is a problem when the internet connection with server is slow.
If I click many nicknames the code checks the actual status of the fight, it says: "No teams open (available) here, I need a new one" and as you can imagine (because internet is crappy) it creates a lot of teams with just one player, as many as I clicked each of nicknames.
How can I fix this?
I need something like a queue?
Maybe I can click one player and then during the check if I click again I can queue the next player? But I need to show instantly the list of players added.
How to?
Aucun commentaire:
Enregistrer un commentaire