dimanche 24 janvier 2016

Ember JS - Destroy Empty Store Record When Transitioning With Association

Alright so it's been a while since I've been in Ember land and I'm trying to figure out some new things.

First off I'm currently using Ember 2.2.0/ Ember Data 2.3.1 / Ember CLI: 2.0.0-beta6.

Basically I'm trying to figure out the best way to remove empty store records when a component is removed from the DOM. This particular model that is created during the Route initialization has a one to one association and I can remove the primary record but not it's association from the store. I remember doing this in the route on "willTransition" in the past is that still the best way? Or is this way effective?

CompanyModel

import Ember from 'ember';
import DS from 'ember-data';

let { attr, belongsTo } = DS;

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

ContactModel

import Ember from 'ember';
import DS from 'ember-data';

let { attr, belongsTo } = DS;

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

CompanyNewRoute:

import Ember from 'ember';

export default Ember.Route.extend({
  model() {
    return this.store.createRecord('company', {
      contact: this.store.createRecord('contact')
    );
  }
});

CompanyFormComponent:

import Ember from 'ember';

export default Ember.Component.extend({
  tagName: '',

  willDestroyElement() {
    let company = this.get('company');
    if(company.get('isNew')) {
      let contact = company.get('contact');
      contact.destroyRecord(); // This errors out saying destroyRecord is not a function
      company.destroyRecord(); // This deletes properly and removes the empty record from the store
    }
  },
});




Aucun commentaire:

Enregistrer un commentaire