PDA

View Full Version : Dynamic URL in store - near a solution



digeridoo
29 Nov 2011, 1:14 PM
Hi,


I am nearly there with my solution of making a store url dynamic depending on which item is pressed but so far it only works when I type the ID number into the store manually.

At the moment, it works well grabbing a file like this:


var store = new Ext.data.Store({
model: 'One',
proxy: {
type: 'ajax',
url: '/carousel2store/Carousel 2_files/get-album.php',
reader: {
type: 'json',
root: 'albums'
}
},....etc

When I change it to this and manually type the ID number, it also works:


var store = new Ext.data.Store({
model: 'One',
proxy: {
type: 'ajax',
url: '/carousel2store/Carousel 2_files/get-album' + '2' +'.php',
reader: {
type: 'json',
root: 'albums'
}
},


But I am trying to make the number '2' dymamic relating to the item pressed.

I have tried variations like this:


url: '/carousel2store/Carousel 2_files/get-album' + '{itemid}' +'.php',

url: '/carousel2store/Carousel 2_files/get-album' + '{data.itemid}' +'.php',



...but I'm not having much luck so far. My goal is to grab the itemid from a carousel item, and pass that to the store URL above.

Here's a snippet of the carousel, notice the id number on each item (I'm not actually using this carousel in my final example as it is inefficient, but it will do as an example):


var carousel = new Ext.Carousel({
defaults: {
cls: 'card'
},
items: [{
layout: {
type: 'vbox',
align: 'stretch'
},
items: [{
flex: 1,
layout: {
type: 'hbox',
align: 'stretch'
},
items: [{
flex: 1,
html: '<img src="resources/img/1.jpg" />',
itemid: '1',
listeners: {afterrender: function(c)
{c.body.select('img').on('click', function(e, t)
{popup1.show('pop');});
}}
},{
flex: 1,
html: '<img src="resources/img/2.jpg" />',
itemid: '2',
listeners: {afterrender: function(c)
{c.body.select('img').on('click', function(e, t)
{popup1.show('pop');});
}}
},


Hope someone can help!


Digeridoopoo

mitchellsimoens
29 Nov 2011, 1:44 PM
Take a look at this example:


var store = new Ext.data.Store({
model: 'One',
proxy: {
type: 'ajax',
url: '/carousel2store/Carousel 2_files/get-album{itemId}.php',
reader: {
type: 'json',
root: 'albums'
}
}
});

store.proxy.url = store.proxy.url.replace('{itemId}', 1);

console.log(store);

digeridoo
29 Nov 2011, 11:58 PM
Thanks Mitchell, but it didn't work for me. I got the following errors in the console:


GET http://localhost:8888/carousel2store/Carousel 2_files/get-album{itemId}.php?_dc=1322636880377&limit=25 404 (Not Found)
TypeError: 'undefined' is not an object (evaluating 'a.length')


It didn't seem to be replacing the itemid. I tried changing the itemid to a number in the store.proxy.url.replace part but it still said the error above.

Do you think it would be better to force the store.proxy.url.replace in the listener part? So that when you click on something it changes the store id then?


App.views.HomeIndex = Ext.extend(Ext.Panel, {
fullscreen: true,
layout: "fit",
dockedItems: [{
xtype:"toolbar",title: "Test"
}],
items: [carousel],
listeners: {
afterrender: function(c){
c.body.select('img').on('click', function(e, t){
popup6.show('pop')
;});}}


I found this code from another forum, I tried adapting it with no luck:


nestedList.on('itemtap', function(subList, subIdx, el, e){
store.proxy.url = '/cgi-bin/nested.cgi?id=abc';
store.read();
});

I have also read these posts:

http://www.sencha.com/forum/showthread.php?110734-Add-parameters-to-Ajax-proxy

http://www.sencha.com/forum/showthread.php?133459-Dynamicaly-change-store-url

http://www.sencha.com/forum/showthread.php?154834-onload-function-issues

http://www.sencha.com/forum/showthread.php?112399-NestedList-change-Proxy-URL-store-loaded-twice

Can I also say how great the support is here, it really makes a difference. I would like to support you guys in the future for the help you have given me.


Digeridoopoo

mitchellsimoens
30 Nov 2011, 7:18 AM
Of course it ended up in 404... you didn't do the replace() on it.

digeridoo
30 Nov 2011, 8:25 AM
OK thanks, I will try again later tonight. It didn't seem to append a number or change the url in my testing in the console so I thought it was not workin . I thought perhaps I had missed a command to update/refresh the store after click...

:-)