Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cascade unloading #1

Open
buschtoens opened this issue Oct 9, 2017 · 3 comments
Open

Cascade unloading #1

buschtoens opened this issue Oct 9, 2017 · 3 comments

Comments

@buschtoens
Copy link

If I unload a Human I would expect its Legs to be unloaded as well, since they cannot exist without their corresponding Human.

This isn't currently possible by only using an Adapter mixin. Do you have some alternative ideas?

@buschtoens
Copy link
Author

I suggest switching to decorators and then relying on the didDelete event and overwriting the unloadRecord method.

// app/models/human.js
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { hasMany, belongsTo } from 'ember-data/relationships';
import { cascadeDelete, cascadeUnload } from 'ember-data-cascade-delete';

export default Model.extend({
  name: attr('string'),

  @cascadeUnload
  @cascadeDelete
  heart: belongsTo('heart', { async: true, inverse: 'human' }),

  @cascadeUnload
  @cascadeDelete
  legs: hasMany('leg', { async: true, inverse: 'human' })
});

@buschtoens
Copy link
Author

Using the above pattern, you could even walk the extra mile and make sure that when a relationship is cleared, the orphaned child record gets deleted / unloaded.

@miguelcobain
Copy link
Owner

miguelcobain commented Jun 4, 2020

For the record, 2 years after, I now think that this shouldn't be a decorator, but an option of belongsTo/hasMany like it always was.

Here is how it looks nowadays:

// app/models/human.js
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';

export default class Human extends Model {
  @attr('string')
  name;

  @belongsTo('heart', { async: true, inverse: 'human', cascadeDelete: true })
  heart;

  @hasMany('leg', { async: true, inverse: 'human', cascadeDelete: true })
  legs;
}

The reason is that this is still describing a relationship.

Cascade unloading is still desirable and it would be nice if we could not use mixins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants