【问题标题】:Sencha: Use store in other viewSencha:在其他视图中使用商店
【发布时间】:2013-08-14 07:45:15
【问题描述】:

我有一个商店,它在登录成功时在控制器中创建。代码如下所示:

控制器是 login.js

    //////////// create a JSONstore and load companies into it ////////////// 
var companies_data = new Ext.data.JsonReader({}, [ 'companyame']);      
    var storeCompanies = new Ext.data.JsonStore({
    storeId: 'storeCompanies', 
          proxy: new Ext.data.HttpProxy({
              type: 'GET',
            url: url+'dashboard/?Uid='+uid+'&Ude='+ude,
            reader: {
                type: 'json',
                root: 'root',
                totalProperty: 'total'
            },
            headers: {
               'Accept' : 'application/json;application/x-www-form-urlencoded',
               'Content-Type' : 'application/x-www-form-urlencoded',
            },
             params: {
                Uid: localStorage.uid,
                Ude: localStorage.ude,
            },
          }),
          reader: companies_data,  
          root: 'd',
          type: 'localstorage',
          autoLoad : true,
          id: 'company_Id',
          scope : this,
          fields: ['companyname']
        });

代码是一个函数,在成功登录时调用,并将值传递给它。

这可以正常工作,并且商店可用于登录视图

我需要商店可用于我的主视图。

在我的 login.js 控制器中,我这样引用主视图:

Ext.define('axis3.controller.Login', {
extend: 'Ext.app.Controller',
config: {
    refs: {
        loginView: loginview,
        mainView: 'mainview',
        chartView: 'chartview'
    },
    control: {
        loginView: {
            signInCommand: 'onSignInCommand'
        },
        mainMenuView: {
            onSignOffCommand: 'onSignOffCommand'
        }
    }
},

但这似乎没有帮助。

当我在主视图中使用以下内容时

var companyStore = Ext.getStore('storeCompanies'); // 
console.log('1-Number : ' + companyStore.getCount()); // Using getCount method.
var anotherCompany = { companyname: 'North Wells'};
companyStore.add(anotherCompany);  // 
console.log('2-Number : ' + companyStore.getCount()); // Using getCount method.
//////////////

Ext.define('axis3.view.Main', {
extend: 'Ext.form.Panel',
requires: ['Ext.TitleBar','Ext.data.Store'],
alias: 'widget.mainview',......................

我得到这个错误:

TypeError: 'undefined' 不是一个对象(评估 'companyStore.getCount')

如何在不同的视图中使用我的商店?

【问题讨论】:

    标签: extjs sencha-touch datastore


    【解决方案1】:

    storeId,这样试试

    var storeCompanies = Ext.create("Ext.data.JsonStore",{
              storeId: 'storeCompanies', // add this line
              proxy: new Ext.data.HttpProxy({
                type: 'GET',
                url: url+'dashboard/?Uid='+uid+'&Ude='+ude,
                reader: {
                    type: 'json',
                    root: 'root',
                    totalProperty: 'total'
                },
                headers: {
                   'Accept' : 'application/json;application/x-www-form-urlencoded',
                   'Content-Type' : 'application/x-www-form-urlencoded',
                },
                 params: {
                    Uid: localStorage.uid,
                    Ude: localStorage.ude,
                },
              }),
              reader: companies_data,  
              root: 'd',
              type: 'localstorage',
              autoLoad : true,
              id: 'company_Id',
              scope : this,
              fields: ['companyname']
            });
    

    现在用Ext.getStore('storeCompanies');重试

    【讨论】:

    • 感谢您的建议。我添加了 storeID 并使用了 Ext.getStore('storeCompanies');在我需要使用商店的视图中,但仍然出现相同的错误。我已经编辑了我的问题以显示更多视图代码
    • @JezD 你添加了 storeID 还是 StoreId ?
    • storeId: 'storeCompanies': 完全按照您的建议
    • 我也在使用 Ext.Create。你注意到了吗?
    • Viswa:请看我的回答。感谢您的帮助。
    【解决方案2】:

    如果发现问题。该视图正在尝试在商店存在之前使用它。商店仅在用户登录时创建。

    我决定使用的解决方案是创建一个空商店,然后在成功登录时向其中添加数据。

    var anotherCompany = {companyname: 'Keep Groom'};
    companiesStore2.add(anotherCompany);  // Use the add function to add records or model instances.
    

    这似乎有效

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-30
      • 2021-02-09
      • 2016-01-19
      • 2012-05-29
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      相关资源
      最近更新 更多