对象没有方法setStore() - Sencha Touch 2

时间:2013-09-02 17:03:55

标签: sencha-touch sencha-touch-2

尝试在Sencha Touch 2应用程序中实现本地存储时出现轻微问题。我从外部存储中找到数据,但之后我的控制器中的函数应该将视图存储切换到我最初找到并存储数据后创建的本地存储库。

我有一个对我要更新商店的视图的引用,但是当我尝试使用setStore更新它时,它给出了以下错误:

未捕获TypeError:对象[object Object]没有方法'setStore'

非常感谢任何帮助。这是代码:

控制器:     extend:'Ext.app.Controller',

config: {
    refs: {
        buyTicketsView: 'buyticketspanel'
    },
    control: {
        'buyticketspanel list': {
            itemsingletap: 'buytickets'
        },
        buyTicketsView : {
            show: 'loadTickets'
        }
    }
},

buytickets: function(list, idx, el, record) {
    Ext.Msg.confirm('External Link', 'Open external link?', function(result){
        if (result == 'yes') {
            //window.location = 'http://www.youtube.com/watch?v=' + record.get('id');
            window.location = 'http://' + record.get('link');
        }
    });
},

loadTickets: function() {
    //set up refs to the two stores
    var ticketStore = Ext.getStore('ticketStore');
    var ticketStoreLocalStorage= Ext.getStore('buyTicketsLocalStorage');

    //load the localStorage store
    ticketStoreLocalStorage.load();

    // check if localStorage contains data
    if ((ticketStoreLocalStorage.getCount()) == 0) {
        // nothing found so  we need to load the data from external source
        console.log('localStorage data not found');
        //hand off to onMarkerStoreLoad function (below)
        ticketStore.on({
            load: 'onTicketStoreLoad',
            scope: this
        });
        //call load to trigger above
        ticketStore.load();
    } else {
        // we are ok, just print some debug
        console.log('localStorage data found');
        console.log('localStorage count:' + ticketStoreLocalStorage.getCount());
    }
    //finally set the list's store to localStorage
    console.log(this.getBuyTicketsView());
    //THIS LINE IS THE ONE THAT IS FAILING
    this.getBuyTicketsView().setStore(ticketStoreLocalStorage);

},
onTicketStoreLoad: function() {
    //set up refs
    var ticketStoreLocalStorage= Ext.getStore('buyTicketsLocalStorage');
    var ticketStore = Ext.getStore('ticketStore');
        //loop through each data item and add to localStorage
    ticketStore.each(function(item){
        ticketStoreLocalStorage.add(item);
    });
    ticketStoreLocalStorage.sync();
 }

查看:

extend: 'Ext.navigation.View',
xtype: 'buyticketspanel',

config: {
    navigationBar: {
       hidden: true
    },
    title: 'Buy Tickets',
    iconCls: 'MyScheduleIcon nav-schedule',
    items: [
        {
            docked: 'top',
            xtype: 'toolbar',
            title: '<span class="logo"></span>'
        },
        {
          xtype: 'list',

          itemTpl: '<div class="date-circle">{date}</div><div class="event-title">{title} - {location}</div><div class="buy-tickets-btn">Buy Tickets</div>'
       }
    ]

}

编辑:在切换到localstorage商店后,它也没有显示视图上的数据,这让我觉得它实际上没有切换。

1 个答案:

答案 0 :(得分:3)

buyticketspanel是一个导航视图,因为它扩展了Ext.navigation.View,导航没有setStore方法,该错误说的完全相同。

请注意,您正尝试将商店设置为列表,而不是导航视图。

这是你在控制器中完成的事情

this.getBuyTicketsView().setStore(ticketStoreLocalStorage);

this.getBuyTicketsView() - 为您提供导航视图,而不是列表。

你可以通过多种方式解决这个问题。

解决方案1 ​​

this.getBuyTicketsView().down('list').setStore(ticketStoreLocalStorage);

解决方案2

将itemId提供给您的列表。

{
  xtype: 'list',
  itemId : 'buytick',  
  itemTpl: ['<div class="date-circle">{date}</div>'+
            '<div class="event-title">{title} - {location}</div>'+
            '<div class="buy-tickets-btn">Buy Tickets</div>'].join()
}

在控制器中执行此操作

this.getBuyTicketsView().getComponent('buytick').setStore(ticketStoreLocalStorage);

注意

我将buytick作为itemId添加到列表中,因此我在buytick

中通过了getComponent()

<强>参考

navigation

list setStore

getComponent