Krishna Agrawal
Krishna Agrawal

Reputation: 44

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

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}'}
    ]}
}

Upvotes: -1

Views: 6006

Answers (2)

Heehaaw
Heehaaw

Reputation: 2817

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!

Upvotes: 2

Dreculah
Dreculah

Reputation: 96

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}'
}

Upvotes: 0

Related Questions