mercredi 7 septembre 2016

Link models in Ember and save using Firebase

I am attempting to create a football app, in the application a team can have many players and a player can get added to one team.

Here are my models in ember

Team Model:

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  player: DS.hasMany('player', {aysnc: true})
});

Player Model:

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  team: DS.belongsTo('team', {aysnc: true})
});

When I save my team in Firebase I want to be able to add multiple players in the team data. At the moment it says my player is undefined when I try and save. Here is my route code:

import Ember from 'ember';

export default Ember.Route.extend({

 model() {
   return this.store.createRecord('team');
 },

  actions: {
    saveTeam(newTeam) {
      newTeam.save().then(() => this.transitionTo('team'));
    },

    willTransition() {
          // rollbackAttributes() removes the record from the store
          // if the model 'isNew'
          this.controller.get('model').rollbackAttributes();
        }
    }

});

And my template looks like this:

<div class="col-xs-10 col-xs-offset-1 col-sm-6 col-sm-offset-1 col-md-5 col-md-offset-2">
    
</div>
<div class="col-xs-10 col-xs-offset-1 col-sm-6 col-sm-offset-1 col-md-5 col-md-offset-2">
    
</div>
<div class="col-xs-10 col-xs-offset-1 col-sm-offset-0 col-sm-4 col-md-3">
    <button class="btn btn-primary btn-lg btn-block" >Add Team</button>
</div>

So I would like

-team --name --players ---player ids

Can anyone give me advice on how to achieve this?




Aucun commentaire:

Enregistrer un commentaire