vendredi 19 août 2016

How to invalidate ` `s internal state?

Assume I have a simple input in ember.js:

<input value= />

And then enter text there. How can I enforce ember to reset the <input> to the same value with code?

A simple this.notifyPropertyChange('val'); or set(this, 'val', get(this, 'val')); does not work.

You have to do something like this:

let oldVal = get(this, 'val');
set(this, 'val', 'some dummy value');
Ember.run.later(() => set(this, 'val', oldVal));

But thats really hacky.


Let me explain why I need this. One example is an input where the user may only enter the letters A and B, but not C. It would be nice to do something like this:

hbs:

<input value= oninput= />

js:

val: '',
actions: {
    update(newVal) {
        if(!newVal.includes('C')) {
            set(this, 'val', newVal);
        } else {
            // reset the input to the last valid value.
            // so basically do
            set(this, 'val', get(this, 'val'));
            // or
            this.notifyPropertyChange('val');
            // but neither does update the <input>.
            // the only way is this:
            let oldVal = get(this, 'val');
            set(this, 'val', 'some dummy value');
            Ember.run.later(() => set(this, 'val', oldVal));
            // because this enforces the <input> to forget its internal state.
            // is there any other way to do this?
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire