How to bind child view to parent viewmodel in ExtJs 5?

5.9k Views Asked by At

I need to bind child view to viewmodel of parent view. my structure is -

parentView{
    items : [{
        xtype : 'childview'
    }]
}

parentViewModel{data : NAME}

childview : {
    items : {[
        xtype : 'label',
        bind : {value : '{NAME}'}
    ]}
}
2

There are 2 best solutions below

1
On

Actually, you don't need to use the viewModel for the children at all. All the child views in the component chain do have a natural access to the parent viewModel.

Ext.define('APP.view.Main', {
    extend: 'Ext.panel.Panel',
    alias: 'widget.main',
    viewModel: {
        data: { 
            title: 'TITLE',
            name: 'NAME'
        }
    },
    bind: { title: '{title}' },
    items: [{ xtype: 'child' }]
});

Ext.define('APP.view.Child', {
    extend: 'Ext.container.Container',
    alias: 'widget.child',
    items: [{
        xtype: 'label',
        bind: { value: '{name}' }
    }]
});

See the ExtJS guide for further explanation. (I sincerely recommend reading all of it :) ).

Hope this helps a little!

0
On

OK, for starters the syntax for your bind is wrong and also you can use the lighter component.html if you don't need 'label' xtype for form.

parentViewModel: {
   data: {
      name: 'Igor'
   }
}

xtype: 'component',
bind: {
   html: '{name}'
}