jeudi 18 avril 2019

What is the proper ember.js-way to react to a changing checkbox

I have a checkbox like this:



When I click the checkbox the property is changed. Now I like to call a function to persist the property. But what is the best way to do this?

  1. I could bind an observer to property, but this is consindered bad.
  2. I could bind an action to input oder click.

    
    
    

    This doesn't work because the property has still the old value when the action is called.

  3. Extending the class Checkbox:

    import Checkbox from '@ember/component/checkbox';
    Checkbox.reopen({
        onChange: null,
        change() {
            this._super();
            if (this.onChange) this.onChange();
        }
    });
    ...
    
    
    

    But the function change is not documented, even though the source code indicates that it is feasable to override.

  4. I could bind an action to didUpdate or didUpdateAttrs, but the action is called twice. In my case this wouldn't be a problem, because the property is part of a model, so I could call model.get('hasDirtyAttributes') in the action. [Update] In my test case the action was called twice but in my real code it is only called once, so this seems to be the best solution?[/Update]

So what is the proper ember.js-way to do this?




Aucun commentaire:

Enregistrer un commentaire