jeudi 19 novembre 2015

Rollback hasMany relationship to canonicalState on model rollback

ember-cli: 1.13.8
ember: 1.13.9
ember-data: 1.13.14

currently, I'm using a observer that watches a hasMany relationship on model to set the state to 'isDirty'

model.addObserver('groups', (key, value) ->
    canonicalIds = @get("groups.canonicalState").mapBy('id')
    currentIds = @get("groups.currentState").mapBy('id')
    unless canonicalIds.equals(currentIds)
        @send('becomeDirty')
)

This works fine for what I need.

Currently to reset the 'groups' collection back to the original state, I'm having to do some hacky code that looks like this

ids = @get('model.groups.canonicalState').mapBy('id')
@store.findByIds('group', ids).then((results) =>
    @set('model.groups', results)
    @get('model').rollback()
)

While this works, I can't help but think there is should be a better way to do it. I'd like to clean this up and make it less hacky.

What is the best way to do that so it's less hacky?




Aucun commentaire:

Enregistrer un commentaire