Sencha view layout에서 viewModel 접근하기

2025. 1. 11. 01:32Javascript/Sencha

선언

app.view = Ext.create('MyApp.view.MyView', {
    viewModel: {
        data: {
            searchParams: {
                DATA1: null,
                DATA2: null,
                DATA3: null
            }
        }
    }
});

접근

{
    xtype: 'button',
    text: 'Reset',
    handler: function() {
        const viewModel = app.view ? app.view.getViewModel() : null;
        if (viewModel) {
            viewModel.set('searchParams.DATA1', null);
            viewModel.set('searchParams.DATA2', null);
            viewModel.set('searchParams.DATA3', null);
        } else {
            console.error('ViewModel is not available.');
        }
    }
}

잘못된 접근: 오류 발생

handler: function() {
    const viewModel = this.getViewModel();
    console.log('ViewModel:', viewModel); // Debugging log
    if (!viewModel) {
        console.error('ViewModel is not defined or accessible.');
        return;
    }
    viewModel.set('searchParams.DATA1', null);
    viewModel.set('searchParams.DATA2', null);
    viewModel.set('searchParams.DATA3', null);
}