Sencha TOUCH ListPaging send Param from an existing store - extjs

Hello, I have a list with paging I want to send a param on load, this param is from another store.
Ext.define('WE3Chamados.store.Chamados', {
extend: 'Ext.data.Store',
config: {
model: 'WE3Chamados.model.Chamado',
autoLoad: false,
proxy: {
type: 'jsonp',
url: 'http://XXX/chamados.php',
callbackKey: 'callback',
reader: {
type: 'json',
rootProperty : 'chamados',
successProperty: 'success'
}
}
}
});
Like this
login_store = Ext.getStore('Login');
param_to_send_default = login_store.getAt(0).data.cod_usuario;
and send it as a default param.

You can use the extraParams config for the proxy object:
proxy: {
type: 'jsonp',
...
extraParams: {
param_to_send_default: Ext.getStore('Login').getAt(0).get('cod_usuario)
}
}

Related

How to pass parameters to store in ExtJs

I am working on a display where I need to bind a combobox but I am unable to pass parameters. Below is my code please provide me the way to pass parameters.
//store
Ext.define('NetworkStore', {
extend: 'Ext.data.Store',
alias: 'NetworkStore',
fields: ['Id', 'value'],
storeId: 'NetworkStore',
autoLoad: true,
proxy: {
type: 'ajax',
useDefaultXhrHeader: false,
actionMethods: { create: "POST", read: "GET", update: "POST", destroy: "POST" },
headers: { 'Content-Type': 'application/x-www-form-urlencode' },
limitParam: false,
startParam: false,
pageParam: false,
extraParams: {
Style: 1
},
url: 'url',
reader: {
type: 'json'
}
}
});
xtype: 'combo',
name: 'NetworkIDList',
store: {
type: 'NetworkStore',
'Style': 3 //parameter
//params: { // tried this way as well but did not work for me.
// Style: 3
//}
}
Note: Store is defined in a separate file.
The store should be passed like below format.
store: {
type: 'NetworkStore',
proxy: {
extraParams: {
Style: 3
}
}
}
You can change the params as follows:
let store = Ext.data.StoreManager.lookup('NetworkStore'),
// params are part of the proxy, not the store
proxy = store.getProxy(),
// make sure you keep the other params
newParams = Ext.apply(proxy.getExtraParams(), {style: '3'});
// set the params to the proxy
proxy.setExtraParams(newParams);
// do whatever you plan to do with the store
store.load();
If you want to bind a value to store proxy params, take a look at this fiddle.

Extjs how to parametrize the store proxy url

I'm new to extjs and I'm trying to parametrize the store proxy url.
In my view I'm building the store like this:
store: Ext.create('mystore', {
partofurl: 'url'
})
And my store:
Ext.define('mystore', {
extend: 'Ext.data.Store',
alias: 'store.mystore',
model: 'mymodel',
restful: true,
autoLoad: true,
proxy: {
type: 'ajax',
headers: {
'Accept': '*/*',
'Cache-Control': 'no-cache',
'Content-Type': 'application/json',
'Authorization': localStorage.token
},
reader: {
type: 'json',
rootProperty: 'data',
successProperty: 'success'
},
writer: {
type: 'json',
writeAllFields: true,
encode: true,
rootProperty: 'data'
},
actionMethods: {
read: 'GET'
},
api: {
read: 'http://url' + this.partofurl,
create: 'http://url' + this.partofurl,
update: 'http://url' + this.partofurl,
destroy: 'http://url' + this.partofurl,
},
autoSave: true
}
});
I tried this also:
store: Ext.create('mystore', {
proxy.api.read = 'http://url' + partofurl
})
and so... but it keeps telling me:
Uncaught Error: You are using a ServerProxy but have not supplied it with a url.
How can I solve this?
Check out the example on fiddle.
You can't use this in Ext.define.
You need to set url in constructor
or override the buildUrl method.
On your second attempt you just did not create the store config object properly.
Try this:
var store = Ext.create('mystore', {
proxy: {
api: {
read: 'http://url' + partofurl
}
}
});
Check out this fiddle (line 22).

Accessing a store from another store - Sencha Touch

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;
}
}
]
},
});
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.
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
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'
}
}
}
});

Send all records of a Extjs store to server at once

How can I send my entire store data to the server in one POST call?
It could be in json format.
thanks.
Update:
this is my store code:
Ext.define('App.store.consultorio.Receita', {
extend: 'Ext.data.Store',
model: 'App.model.consultorio.Receita',
autoLoad: false,
proxy: {
type: 'rest',
reader: {
type: 'json'
},
writer: {
type: 'json'
},
url: 'consultas/receita.json'
}
});
You could set every record in the store dirty, then call sync()
store.each(function(record){
record.setDirty();
});
store.sync();
Also, your store is using a RESTful proxy, which by default does not batch actions. See http://docs.sencha.com/ext-js/4-2/#!/api/Ext.data.proxy.Rest-cfg-batchActions
Your store should look like:
Ext.define('App.store.consultorio.Receita', {
extend: 'Ext.data.Store',
model: 'App.model.consultorio.Receita',
autoLoad: false,
proxy: {
type: 'rest',
batchActions: true, //<------
reader: {
type: 'json'
},
writer: {
type: 'json'
},
url: 'consultas/receita.json'
}
});

Extjs - How do you tell proxy in store to send data just as ordinary http parameters?

I do not understand how I can tell the proxy to simply submit the data as ordinary http parameters to the post-request. You could think that would be the simplest thing to do, but I just cant get my store to send anything other then xml or json to the server when CRUDing.
Please tell me I'm missing something really simple.
Ext.define('ObjectManager.store.Object', {
extend: 'Ext.data.Store',
model: 'ObjectManager.model.Object',
autoLoad: true,
proxy: {
type: 'ajax',
api:{
read: 'http://localhost/Get/',
update: 'http://localhost/Edit/',
create: 'http://localhost/Add/',
delete: 'http://localhost/Delete/'
},
reader: {
type: 'xml',
root: 'objects',
record: 'object'
}
}
});
For the record, the method used by the Proxy per CRUD action can be configured with the following config:
actionMethods: {
create : 'POST',
read : 'POST',
update : 'POST',
destroy: 'POST'
}
Here's a store configuration that reads JSON and sends the data as POST. I believe the key is in the writer config: "encode: true"
/* Data store */
Ext.create('Ext.data.Store', {
storeId:'categoryStore',
model: 'Model.Category',
autoLoad: true,
autoSync: true,
groupField: 'CategoryParent',
proxy: {
type: 'ajax',
api: {
read: '',
create: '',
update: '',
destroy: ''
},
reader: {
type: 'json',
root: 'data'
},
writer: {
root: 'data',
encode: true,
allowSingle: false
}
}
});

Resources