【问题标题】:extjs 6 global store in packagesextjs 6 全局存储包
【发布时间】:2018-02-04 02:55:50
【问题描述】:

我正在使用 extjs 6,我需要为一个包创建一个全局存储。我通常在

中为我的应用程序创建全局商店
app/store/mystore.js

然后在Application.js中注册

stores:[
    'mystore'
] 

我已经尝试在

中创建它
packages/mypkg/src/store/mypkgstore.js

但它不起作用...当应用程序运行时,mypkgstore.js 没有被下载。

我应该将它作为应用程序的全局存储添加到任何文件中吗?

我已尝试将其添加到其他类的必需子句中,文件已下载,但商店未加载数据...

【问题讨论】:

    标签: javascript extjs package store extjs6


    【解决方案1】:

    在 Application.js 中定义 store 如下:

    Ext.define('MyApp.Application', {
    extend: 'Ext.app.Application',
    
    name: 'MyApp',
    
    stores: [
        // TODO: add global / shared stores here
        'mystore'
    ]  
    });
    

    在 mypkgstore.js 中,确保将其指定为:

    Ext.define('APP.store.MyPkgStore', {
    extend: 'Ext.data.Store',
    alias: 'store.mystore',
    storeId: 'MyPkgStore'
    });
    

    【讨论】:

    • 但是这样做,包不是一个独立的包。我尝试创建一个模块并在不同的应用程序中使用它,所以我无法在 Application.js 中定义它,因为您必须更改每个使用该包的应用程序中的代码
    【解决方案2】:

    目前不确定这是否有帮助。无论如何...您可能知道,包没有任何像 Application.js 这样的初始化文件,在这种情况下,您需要手动实例化每个商店。就我而言,我做到了:

    主视图:

    Ext.define('Activities.view.Activities', {
       extend: 'Ext.grid.Panel',
       xtype: 'activities',
    
       requires: [
           'Activities.store.Activities'
       ],
       store: Ext.create('Activities.store.Activities'),
    
       columns: [...]
    });
    

    商店:

    Ext.define('Activities.store.Activities', {
       extend: 'Ext.data.Store',
       alias: 'store.activities',
       storeId: 'activities',
    });
    

    或者如果你有很多 store,你可以在主视图的 viewController 的 init() 方法中将它们全部实例化,比如:

    Ext.define('Activities.view.ActivitiesController', {
    
       extend: 'Ext.app.ViewController',
       alias: 'controller.activities',
       id: 'activities',
    
       requires: [
           'Activities.store.Activities',
           'Activities.store.Countries',
    
           ...
       ],
    
       /**
        * Called when the view is created
        */
       init: function (view) {
           Ext.create('Activities.store.Activities');
           Ext.create('Activities.store.Countries');
    
           ...
       }
    
    });
    

    我希望这对你有帮助。 :)

    【讨论】:

    • 这不是一个坏主意,但您总是需要一个主屏幕来加载它。在我的情况下,我有一个作为容器工作的应用程序,然后我有一些包在主应用程序中工作,问题是主屏幕在主应用程序中并且包加载新的不同屏幕,所以我没有主屏幕包裹。也许我可以在每个包的 app.js 中做到这一点。谢谢!!
    • 想一想,这些包没有任何app.js。我认为最好的解决方案是在我加载包屏幕时加载一个通用类,并且仅在未创建商店时才创建商店。我会告诉你它是否有效。谢谢!
    【解决方案3】:

    最好的答案是使用 MVVM 的概念创建一个 ViewModel

    您在其中声明 Grid 的视图:

    Ext.define('YourProject.View',{
    requires : ['YourProject.Controller','YourProject.ViewModel'],
    extend : 'Ext.form.Panel',
    controller : 'MyController',
    viewModel : 'MyViewModel',
    
    initComponent : function(){
    var me = this;
        Ext.apply(me,{
    
        xtype : 'grid',
        itemId : 'myGrid',
        });
    };
    
    });
    

    ViewModel 中的 Store 示例:

    Ext.define('YourProject.ViewModel',{
    extend : 'Ext.app.ViewModel',
    alias : 'MyViewModel',
    
    //declare you stores here
    
    stores : {
       myStore : {
        autoLoad : true,
        fields : [{name : 'id', type : 'string'},
                  {name : 'value', type : 'string'}],
        proxy : {
        url  : 'you/actionHere.do',
        extraParams : {
          yourParameter1 = 'paramVal',
          yourParameter2 = 'paramVal2'
          },
          reader : {
             type : 'json',
             rootProperty : 'data',
             totalPropert : 'total'
          }
        }
      }
    }
    

    });

    在控制器中使用存储:

    Ext.define('YourProject.Controller',{
    extend : 'Ext.app.ViewModel',
    alias : 'myController',
    
    applyState : function(){
    
    var me = this;
    
    //Get you store from ViewModel by doing this
    var store = me.getView().getViewModel().getStore('dmlshFeeAmt');
    me.getView().down(#'myGrid').setStore(store);
    
    }
    });
    

    希望这可以帮助您的项目:)

    【讨论】:

    • 这样做您只能在该上下文中使用商店。如果你需要在另一个拥有自己viewmodel的组件中使用它,你不能访问。这是一个很好的解决方案,只适用于一个屏幕,但对于一个包无效。谢谢!
    • 嗯,MVVM 的概念是一个 ViewModel 对一个 View。我曾使用 extjs 处理过大型项目,但我从未有过使用单个 .js 访问多个商店的经验。祝你好运
    猜你喜欢
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 2011-04-12
    • 1970-01-01
    相关资源
    最近更新 更多