【问题标题】:chained store source from another viewmodel来自另一个视图模型的链式存储源
【发布时间】:2015-06-18 10:22:11
【问题描述】:

我想实现链式存储,其源存储放置在另一个 ViewModel 中。如果我像往常一样写,chained store 是空的,因为 View Model 无法通过这种方式从另一个 ViewModel 看到 store。

sport_chained: {
    source: 'sport' // sport is in another viewmodel
} 

我找到了这个解决方案:

// in ViewModel
constructor: function () {
    this.callParent(arguments);
    Ext.defer(function () {
        this.setStores({
            sport_chained: {
                source: Ext.data.StoreManager.lookup('sport')
            }
        });
    },10);
} 

但这并不方便,从那时起我必须将这个 ViewModel 中的所有其他商店都放在构造函数中。也许有人做了这样的事情并且知道如何以更方便的方式做到这一点? https://fiddle.sencha.com/#fiddle/otn

【问题讨论】:

    标签: extjs mvvm viewmodel


    【解决方案1】:

    也许对最终来到这里的其他人有所帮助。我能够做到这一点......但你必须考虑不同......

    所以,我在某处的父组件上有一个 viewModel,我希望子组件能够将商店链接到父组件...

    ParentViewModel.js

    alias: 'viewmodel.parent',
    stores: {
        parentStore: {
            model: 'MyApp.model.CoolModel'
        }
    }
    

    ChildViewModel.js

    alias: 'viewmodel.child',
    stores: {
        childStore: {
            model: 'MyApp.model.CoolModel'
        }
    }
    

    ChildView.js

    extend: 'Ext.grid.Panel',
    alias: 'wiget.child',
    {
        viewModel: {
            type: 'child'
        }
    }
    

    ParentView.js

    alias: 'wiget.parent',
    {
        viewModel: {
            type: 'parent'
        }
    },
    
    items: [
        {
            xtype: 'child',
            viewModel: {
                stores: {
                    childStore: {
                        // Notice the binding to the parent store without the bind wrapper - no idea why it's done that way
                        source: '{parentStore}'
                    }
                }
            },
    
            // So we chained the childStore to the parentStore above using the source..then we can bind to the chained store below like you would expect
            bind: {
                store: '{childStore}'
            }
        }
    ]
    

    这些只是简短的 sn-ps,但希望它能说明如何以声明方式进行链接,而不必通过控制器强制解决问题。需要注意的另一件事 - 我扩展了一个网格面板,以便轻松说明绑定商店。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多