5
votes

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 ?

2

2 Answers

0
votes

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);
    }
}),
0
votes

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