5

I have documents list. Every document has client attribute defined by belongsTo.

When user change client in one of documents i want to show in the counter how many documents are changed. And when user decide he will press "publish" button which will save documents client changes to api.

DS.Model in ember 2.13 has parameters (https://emberjs.com/api/data/classes/DS.Model.html):

hasDirtyAttributes, dirtyType

Both of them does not react on belongsTo/HasMany changes by Ember design. I saw many answers to this topic, but I do not see any isDirty() method for model in 2.13 documentation, nor any .send("becomeDirty") method to manually set document model in dirty status ? I also saw several plugins/mixins for older Ember versions.

But my question is, how do Ember creators "advise/advice/best practice" to deal with this. Is there some basic way/manual solution that does not require any third party addon ? Like with maybe onchange observer for every relation in model?, or computed property with @each.dirtyType for child related models (or even set children will not be flagged as dirty itself?) ?

What is sandbox solution for this in Ember 2.13 ?

Jiro Matchonson
  • 875
  • 1
  • 17
  • 24

2 Answers2

0

It's been some time. For has-many I use this solution. 'isTasksDirty' will get back to false if user change has-many relation to the same group of items as before:

/* RELATIONS DIRTINESS */
    isRelationDirty: Ember.computed(
        'isDepartmentsDirty',
        'isTasksDirty'
        {
            get(key) {
                return this.get("isDepartmentsDirty") ||
                       this.get("isTasksDirty");
            },
            set(key, value) {
                this.set("isDepartmentsDirty", value);
                this.set("isTasksDirty", value);
            }
        }
    ),

    isTasksDirty:false,
    tasksChanged: Ember.observer('tasks.[]', function() {
        if(!arraysEqual(this.get("tasks").content.currentState, this.get("tasks").content.canonicalState)){
        this.set("isTasksDirty", true);
    } else {
        this.set("isTasksDirty", false);
    }
}),
Jiro Matchonson
  • 875
  • 1
  • 17
  • 24
0

Ember (2.x) does not track relations (e.g. hasMany) but it is possible to use ember-addon ember-data-change-tracker that can almost do it. It allows you to (auto-)save the current state of relations and afterwards you can compare this 'saved' (=old state) with the current state. You have to find a difference by yourself. A simple example from my adapter:

snapshot.hasMany('users').length <-- current count of relations
snapshot.record.savedTrackerValue('users').length <-- old count of relations
Marek Grác
  • 743
  • 9
  • 24
  • 1
    Hello tnx for info, actually hasMany is easy to implement, and its actually stored in ember2 by default, all you need is in .currentState and .cannonicalState, as i posted in another post before, but had a bigger trouble with BelongsTo, have solved some way that work for my project but not so solid as .hassMany one. I tried several plugins my main issue was that those required from me that i rewrite whole application outside of models, instead of solving this issue just by models alone, since i think this should be done by models itself just like dirtiness of classic non relational fields. – Jiro Matchonson Nov 30 '17 at 13:49