0

I've got an ExtJs ViewModel where I try to use fomulas. The following code is working as expected:

viewModel: {
    formulas: {
        isPressed: function (get) {
            return get('state.attribute');
        }
    }
}

The debugger pauses two times in this formula. Once when opening the view and once when the state and property is initialized.

But if I try this:

viewModel: {
    formulas: {
        isPressed: function (get) {
            var x = 'state.attribute';
            return get(x);
        }
    }
}

The debugger only stops when opening the view but not the second time, when everything is initialized.

Edit

I tried to do the following. In my component I've got this config:

config: {
    target: null
}

target contains a string like 'state.property' from my parent view which contains the component. Now in this component I want a binding to the value of target but don't want to write:

formulas: {
    isPressed: {
        bind: '{state.property'},
        get: function(property) { ... }
    }
}

because the value to bind to should be dynamic. I want to reuse the component in different places. So I tried this, but didn't work:

viewModel: {
    formulas: {
        isPressed: function (get) {
            return get(this.getView().getTarget());
        }
    }
}
Mark
  • 103
  • 1
  • 11

1 Answers1

1

The reason this occurs is because it parses the contents of the function to figure out the dependencies. However it only uses a really naive parser, so things like you described won't be picked up. You can explicitly specify dependencies using bindTo:

const viewModel = new Ext.app.ViewModel({
    formulas: {
        isNameModified: {
            bind: {
                bindTo: '{theUser}',
                deep: true
            },
            get: user => user.foo
        }
    },
    data: {
        theUser: {
            foo: 1
        }
    }
});
Evan Trimboli
  • 29,900
  • 6
  • 45
  • 66
  • Thanks for your answer. I've updated my question to clarify my problem. – Mark Nov 30 '18 at 12:25
  • 1
    It doesn't get processed for exactly the same reason. The parser expects `get('some.vm.prop');`. Since you include a variable it can't parse it. – Evan Trimboli Nov 30 '18 at 12:41