2
votes

I'm trying to create a store and inside access the data from another store to construct the proxy url.

Something like this:

Ext.define('MyApp.store.Post',{
extend:'Ext.data.Store', 
config: {
    model: 'MyApp.model.Post',
    proxy: {
        type: 'ajax',
        url: 'http://mywebsite.com/get?userid=' + Ext.getStore('UserData').getAt(0).data.userid,
        reader: {
            type: 'json'
        }
    }
}
});

So, I'm basically trying to get the userid from another store to be able to construct the correct url. This doesn't work, I get:

Uncaught TypeError: Object #<Object> has no method 'getStore' 

What is the correct way to do this?

EDIT: Okay I put in a dummy URL and trying to change it with a listener, this is my store now:

Ext.define('MyApp.store.Post',{   extend:'Ext.data.Store', 
config: {
      fields: [
                        'title', 'link', 'author', 'contentSnippet', 'content'
                    ],
    proxy: {
        type: 'jsonp',
        url: 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://feeds.feedburner.com/SenchaBlog',
        reader: {
            type: 'json',
            rootProperty: 'responseData.feed.entries'
        }
    },
    listeners: [
        {
            beforeload: function(){
               console.log("store loaded");   //I DON'T SEE THIS IN CONSOLE
               return true;
            }    
        }
    ]
},

});
3

3 Answers

3
votes

Basically you did nothing wrong, but the reason is sencha touch uses asynchronous loading and it seems that Ext.getStore() is not instantiated at the time your store is defined.

Let's try this method instead:

First, add a listener for beforeload event inside your store config:

Ext.define('MyApp.store.Post',{
extend:'Ext.data.Store', 
config: {
    model: 'MyApp.model.Post',
    proxy: {
        type: 'ajax',
        //url: you don't even need to set url config here, simply ignore it
        reader: {
            type: 'json'
        }
    }
},
listeners: [
    {
        fn: 'setUrl',
        event: 'beforeload'
    }
]
});

then declare a function like this, in the same file:

setUrl: function(){
    this.getProxy().setUrl(Ext.getStore('UserData').getAt(0).data.userid);
    return true;
}

This way, it's ensured to set the url for your store's proxy right before it's loaded. And basically at the time, all core methods are instantiated.

Update: please try this with your Post store:

Ext.define('MyApp.store.Post',{
   extend:'Ext.data.Store', 
   config: {
       //autoLoad: true,
          fields: ['title', 'link', 'author', 'contentSnippet', 'content'],
        proxy: {
            type: 'jsonp',
            url: 'dummy url',
            reader: {
                type: 'json',
                rootProperty: 'responseData.feed.entries'
            }
        },
   },

    initialize: function(){
        console.log("loaded!");
        this.getProxy().setUrl('https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://feeds.feedburner.com/SenchaBlog');
        //return true;
    }    
});

After reading the source code of the pull-to-refresh plugin, I see that Sencha Touch use an Ext.data.Operation instead of Ext.data.Store.load() function. So you will have to put it into the initialize method instead.

0
votes

Use Ext.data.StoreManager.lookup('UserData') to get the store instance.

But in your case, I would use this somewhere, where you work with the userid:

var postsStoreInstance = ...;
postsStoreInstancegetProxy()._extraParams.userid = userid;

It adds a query paremetry to the store's proxy url

0
votes

The way you do it is the correct way. What is the code of your store definition?

Make sure your store has storeId: 'UserData'.

See this working example:

Ext.define('User', {
   extend: 'Ext.data.Model',
   fields: [
       {name: 'id'}
   ]
});

Ext.create( 'Ext.data.Store', {
   model: 'User',
   storeId: 'UserData'
});

Ext.define('MyApp.store.Post',{
   extend:'Ext.data.Store', 
   config: {
       model: 'MyApp.model.Post',
       proxy: {
           type: 'ajax',
           url: 'http://mywebsite.com/get?userid=' + Ext.getStore('UserData'),
           reader: {
               type: 'json'
           }
       }
   }
});