cachius
cachius

Reputation: 1907

ExtJS: Prevent ViewModel bindings update on null

Is it possible to prevent a ViewModel from updating its bindings when the value is null? Or more generally any other specific value.

Upvotes: 0

Views: 274

Answers (1)

Istvan Tabanyi
Istvan Tabanyi

Reputation: 866

Not really sure what you really want and this is definitely not the best solution, but may give you some idea:

const viewModel = new Ext.app.ViewModel({
    formulas: {
        notifyOnlyWhenNotNull: {
            bind: '{name}',
            get: function(value) {
                if (value) {
                    this.storedNotNullValue = value;
                }
                return value?value:this.storedNotNullValue;
            }
        }
    },
});

viewModel.bind('{notifyOnlyWhenNotNull}', function(v) {
    console.log('Changed to',v);
});


viewModel.set('name','Something not null');
viewModel.notify();
viewModel.set('name',null);
viewModel.notify();
console.log('We still have the last non-null value', viewModel.get('notifyOnlyWhenNotNull'));
console.log('But we also have the real "null" value', viewModel.get('name'));

The idea here is pretty simply, wrap your binded data in a formula and a custom getter can decide if you already stored some real value, and return that if the underlying data changed to null. When you need this conditional binding, bind to formula, which is binded to your data.

Upvotes: 1

Related Questions