简体   繁体   English

ExtJS 5:根据绑定值设置子视图模型数据

[英]ExtJS 5: Set child View Model data from bound value

I have a class that has its own view model, and I create 2 instances of this class in my main view. 我有一个具有自己的视图模型的类,并在主视图中创建了该类的2个实例。 In the main view, I want to pass down values for my 2 class instances, but I can't seem to get this working... I think I'm just not understanding some very simple concept. 在主视图中,我想传递2个类实例的值,但似乎无法正常工作……我想我只是不了解一些非常简单的概念。

The expected result is the value1 + value2 field has the concatenation of value1 and value2, the first myValue shows value1, and the 2nd myValue shows value2. 预期的结果是value1 + value2字段具有value1和value2的串联,第一个myValue显示value1,第二个myValue显示value2。 Here's my code and example : 这是我的代码和示例

Ext.application({
  name: 'Fiddle',

  launch: function() {
    Ext.define('MyViewModel', {
      extend: 'Ext.app.ViewModel',
      alias: 'viewmodel.myView',
      formulas: {
        doSomething: function(getter) {
          console.log(getter('value1'), getter('value2'));
          return getter('value1') + getter('value2');
        }
      }
    });

    Ext.define('MyView', {
      extend: 'Ext.panel.Panel',
      xtype: 'myView',
      viewModel: {
        type: 'myView'
      },
      config: {
        myValue: null
      },
      publishes: {
        myValue: true
      },
      items: [
        {
          xtype: 'displayfield',
          fieldLabel: 'myValue',
          bind: {
            value: '{myValue}'
          }
        }
      ]
    });

    Ext.create('Ext.container.Container', {
      renderTo: Ext.getBody(),
      items: [
        {
          xtype: 'displayfield',
          fieldLabel: 'display',
          bind: {
            value: '{doSomething}'
          }
        },
        {
          xtype: 'myView',
          reference: 'view1',
          title: 'View1',
          bind: {
            myValue: '{value1}'
          }
        },
        {
          xtype: 'myView',
          reference: 'view2',
          title: 'View2',
          bind: {
            myValue: '{value2}'
          }
        }
      ],
      viewModel: {
        data: {
          value1: 'Something',
          value2: 'something else'
        }
      }
    })
  }
});

Your first displayField will never "see" the doSomething formula, because that formula is not part of it's parent, so you will need to move the formula from MyViewModel to your Ext.container.Container viewModel. 您的第一个displayField永远不会“看到” doSomething公式,因为该公式不是其父级的一部分,因此您需要将公式从MyViewModel移至Ext.container.Container viewModel。

Also, when you publish a custom value, it will have reference.publishedvalue format. 另外,发布自定义值时,它将具有reference.publishedvalue格式。 This should fix your panel: 这应该可以修复您的面板:

Ext.application({
    name: 'Fiddle',

    launch: function() {
        Ext.define('MyViewModel', {
            extend: 'Ext.app.ViewModel',
            alias: 'viewmodel.myView'
        });

        Ext.define('MyView', {
            extend: 'Ext.panel.Panel',
            xtype: 'myView',
            viewModel: {
                type: 'myView'
            },
            config : {
                myValue : null
            },
            publishes : ['myValue'], 
            items: [{
                xtype: 'displayfield',
                fieldLabel: 'myValue',
                initComponent : function() {
                    var me = this,
                        owner = me.$initParent || me.initOwnerCt;

                    this.setBind({
                        value: '{' + owner.reference + '.myValue}'
                    });
                    this.callParent();
                }
            }]
        });

        Ext.create('Ext.container.Container', {
            renderTo: Ext.getBody(),
            viewModel: {
                data: {
                    value1: 'Something',
                    value2: 'something else'
                },
                formulas: {
                    doSomething: function(getter) {
                        console.log(getter('value1'), getter('value2'));
                        return getter('value1') + getter('value2');
                    }
                }
            },
            items: [{
                xtype: 'displayfield',
                fieldLabel: 'display',
                bind: {
                    value: '{doSomething}'
                }
            },{
                xtype: 'myView',
                reference: 'view1',
                title: 'View1',
                bind: {
                    myValue: '{value1}'
                }
            },{
                xtype: 'myView',
                reference: 'view2',
                title: 'View2',
                bind: {
                    myValue: '{value2}'
                }
            }]
        })
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM