Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

Ember CLI addon: a mixin that makes the `isDirty` property of a record respect the dirtiness of its belongsTo/hasMany children

License

Notifications You must be signed in to change notification settings

secondstreet/ember-cli-stained-by-children

 
 

Repository files navigation

NPM package Build Status

This addon grew into a collection of mixins. The addon is named after the first of those mixins. Thus, the name doesn't reflect the capabilities of the addon very well, but i decided not to change it. -- @lolmaus

Installation

You must have Ember CLI installed in your system and your project must be Ember CLI-driven.

In your project folder:

ember install:addon ember-cli-stained-by-children

The mixins

stained-by-children

This mixin modifies the isDirty property of a model, making it respect the dirtiness of its children.

Usage

In your parent model:

  1. Import stained-by-children/stained-by-children (path has changed in 0.3!).
  2. Extend your model with the mixin.
  3. Apply stains: true to the relationships whose dirtiness should affect your model's dirtiness:
// app/models/foo.js

import DS from 'ember-data';
import StainedByChildrenMixin from 'stained-by-children/stained-by-children';

Foo = DS.Model.extend( StainedByChildrenMixin, {
  bars: DS.hasMany   ( 'bar',  {stains: true} )
  baz:  DS.belongsTo ( 'baz'                  )
  quux: DS.belongsTo ( 'quux', {stains: true} )
});

export default Foo;

In the above example, a foo will be marked dirty, whenever its quux or any of its bars become dirty. The baz will not affect foo's dirtiness because the respective relationship is not flagged with stains: true.

How it works

The mixin creates an areChildrenDirty computed property property bound to <rels>[email protected], where <rels> are all its relationships marked with stains: true. The property is true when any staining child is dirty.

The mixin also modifies the isDirty property. Originally, isDirty is merely an alias to currentState.isDirty. After you import the mixin, isDirty will compute to

this.get('currentState.isDirty') || this.get('areChildrenDirty')

⚠️ Warning!

  • Circular references are not supported!

    If you define two models staining/cleaning each other, your app will be unable to start.

  • This mixin only reflects the dirtiness of children. It does not make the parent record dirty when the relationship content changes. That is, when a non-dirty child is replaced with another non-dirty child, the result is non-dirty. :(

    I struggle to figure out how to address this use case. The tricky part simply creating a parent record with children already triggers observers for child and children.[]. If you can think of a way to overcome this, chime into the discussion.

clean-embedded-children

DS.EmbeddedRecordsMixin has a known issue: when you save a parent record, embedded children remain dirty. This is a bummer, especially when you use stained-by-children.

You can resolve this issue with clean-embedded-children!

Usage

In your parent model:

  1. Import stained-by-children/clean-embedded-children.
  2. Extend your model with the mixin.
  3. Apply embeddedChild: true to the relationships who should become clean when this record is saved.

The example below combines both mixins:

// app/models/foo.js

import DS from 'ember-data';
import StainedByChildrenMixin from 'stained-by-children/stained-by-children';
import CleanEmbeddedChildrenMixin from 'stained-by-children/clean-embedded-children';

Foo = DS.Model.extend( StainedByChildrenMixin, CleanEmbeddedChildrenMixin, {
  bars: DS.hasMany   ( 'bar',  {stains: true, embeddedChild: true} )
  baz:  DS.belongsTo ( 'baz'                                       )
  quux: DS.belongsTo ( 'quux', {stains: true, embeddedChild: true} )
});

export default Foo;

Cleaning children can be done recursively. Just user the mixin on child model as well as parent model, and saving a parent record will result in cleaning both its children and children's children.

License

MIT

Credit

Snapped together by Andrey Mikhaylov (lolmaus) https://github.com/lolmaus

About

Ember CLI addon: a mixin that makes the `isDirty` property of a record respect the dirtiness of its belongsTo/hasMany children

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 87.0%
  • HTML 9.3%
  • Handlebars 3.5%
  • CSS 0.2%