Extending Ext.data.Store - extjs

I am trying to centralize my configuration of EXTJS stores within my application, however, I cannot seem to figure out how to make this happen. I am using ExtJS 4.1.
I have a base store, which I want to hold all of the repetitive configuration stuff, and then my more specific stores to hold what's actually different.
Ext.define('My.store.Abstract', {
extend: 'Ext.data.Store',
autoload:false,
proxy: {
type: 'ajax',
reader: {
type: 'json',
root: 'data',
totalProperty: 'total',
successProperty: 'success',
messageProperty: 'message'
},
writer: {
type: 'json',
encode: true,
writeAllFields: true,
root: 'data',
allowSingle: false
},
simpleSortMode: true
}
});
Then I would like to provide the store specific stuff on a store by store basis --
Ext.define('My.store.Products', {
extend: 'My.store.Abstract',
storeId: 'Products',
model: 'My.model.Product',
proxy: {
api: {
create: '/myurl/create',
read: '/myurl/index',
update: '/myurl/update',
destroy: '/myurl/delete'
}
}
});
What I am finding is that it just doesnt behave at all. I believe it has something to do with the proxy, but I just can't track it down.
What is the correct way to do this? I would prefer not to replicate the same configuration stuff (from my abstract store) across the 350+ stores in my application. As of now, that it what I have, and I thought I was trying to implement a pretty basic concept .. to no avail.
I know things are not working, as basic as the pageSize, or even the autoLoad .. because they are not being respected at all.
I've played around with constructors, and calling the parent.
Any help would be greatly appreciated.

You can't do it that way because you're expecting to to merge the objects which it just won't do.
Instead, you'll want to look at something like this (untested):
Ext.define('Base', {
extend: 'Ext.data.Store',
autoLoad: false,
constructor: function(config) {
// applyIf means only copy if it doesn't exist
Ext.applyIf(config, {
proxy: this.createProxy()
});
this.callParent([config]);
},
createProxy: function() {
return {
reader: {
type: 'json',
root: 'data',
totalProperty: 'total',
successProperty: 'success',
messageProperty: 'message'
},
writer: {
type: 'json',
encode: true,
writeAllFields: true,
root: 'data',
allowSingle: false
},
simpleSortMode: true
}
}
});
Ext.define('Sub', {
extend: 'Base',
createProxy: function(){
var proxy = this.callParent();
proxy.api = {
create: 'create',
update: 'update'
};
return proxy;
}
});

Here is another way:
Base store (app/store/Base.js):
Ext.define('Admin3.store.Base', {
extend: 'Ext.data.Store',
autoLoad: true,
autoSync: true
});
Base proxy (app/proxy/Base.js):
Ext.define('Admin3.proxy.Base', {
extend: 'Ext.data.proxy.Ajax',
alias: 'proxy.base',
reader: {
type: 'json',
root: 'items',
successProperty: 'success',
messageProperty: 'message'
},
listeners: {
exception: function(proxy, response, operation){
console.log(response, operation);
Ext.Msg.show({
title: 'Remote Exception',
msg: typeof operation.getError() === 'string' ? operation.getError() : operation.getError().statusText,
icon: Ext.Msg.ERROR,
buttons: Ext.Msg.OK
});
}
}
});
Concrete store (app/store/Users.js):
Ext.define('Admin3.store.Users', {
extend: 'Admin3.store.Base',
model: 'Admin3.model.User',
proxy: Ext.create('Admin3.proxy.Base', {
api: {
read: 'data/read.php',
update: 'data/update.php'
}
})
});

I think the other answers here might be a bit more complicated than they need to be. As of version 4.0.0, ExtJS has an Ext.Object.merge() method that will allow an approach very close to what the asker was attempting.
Using the values that the asker has, I'd define my "abstract" store like this:
Ext.define("Ext.ux.data.Store", {
extend: "Ext.data.Store",
constructor: function(config) {
var defaults = {
autoload: false,
proxy: {
type: "ajax",
reader: {
type: "json",
root: "data",
totalProperty: "total",
successProperty: "success",
messageProperty: "message"
},
writer: {
type: "json",
encode: true,
writeAllFields: true,
root: "data",
allowSingle: false
},
simpleSortMode: true
}
};
this.callParent([Ext.Object.merge({}, defaults, config)]);
}
});
I'd then create my concrete stores like this:
Ext.create("Ext.ux.data.Store", {
storeId: "ExampleStore",
model: "ExampleModel",
autoLoad: true, // This overrides the defaults
proxy: {
api: {
read: "/example/read" // This overrides the defaults
}
}
});
This approach will also work for multiple levels of components. You could, for instance, model a read-only store:
Ext.define("Ext.ux.data.ReadonlyStore", {
extend: "Ext.ux.data.Store",
constructor: function(config) {
var overrides = {
proxy: {
api: {
create: undefined,
update: undefined,
destroy: undefined
}
}
}
this.callParent([Ext.Object.merge({}, config, overrides)]); // Note that the order of parameters changes here
}
});

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.

Filter on a ExtJS store using an array

I have a remote store using a MySQL database.
here is the store definition
Ext.define('rgpd.store.sOutil', {
extend: 'Ext.data.Store',
requires: [
'rgpd.model.mOutil'
],
model: 'rgpd.model.mOutil',
autoLoad: true,
autoSync: true,
pageSize: 0,
remoteSort: true,
remoteFilter: true,
proxy: {
type: 'ajax',
api: {
create: 'data/app.php?class=Outil&action=create',
read: 'data/app.php?class=Outil&action=read',
update: 'data/app.php?class=Outil&action=update',
destroy: 'data/app.php?class=Outil&action=destroy',
},
reader: {
type: 'json',
root: 'data',
totalProperty: 'totalCount',
successProperty: 'success'
},
writer: {
type: 'json',
writeAllFields: false,
successProperty: 'success',
encode: true,
extraParams: "id",
idProperty: "id",
rootProperty: 'data'
}
}
});
I have an array of ids, those i want to keep so i need to apply a filter.
I found 2 different solutions but none worked. The first was using filtreBy method
Ext.getStore('sOutil').filterBy(function(record, id) {
return Ext.Array.contains(ids_intervenant_outils, record.get('id'));
});
I did this bug i get an error in Firefox "too much recursion". So i tried an other way to do it and found this on an other stack-overflow post (here) but I ge Ext.escapeRe which according to firefox "is not a function". I tried to find an other function i found Ext.String.escapeRegex() but I got one more error in Firefox "p.replace is not a function"
var filterValue = Ext.isArray(ids_intervenant_outils)
? new RegExp('^(?:' + Ext.Array.map(ids_intervenant_outils, function(value){return Ext.String.escapeRegex(value)}).join('|') + ')$')
: values;
Ext.getStore('sOutil').clearFilter(false);
Ext.getStore('sOutil').filter('id', filterValue);
here is the too much recursion calls trace
RegExpGlobalReplaceOptFunc self-hosted:4702:22
[Symbol.replace] self-hosted:4499:24
replace self-hosted:5248:13
encodeString http://localhost/rgpd/extjs6/ext-all.js:22:383387
doEncode http://localhost/rgpd/extjs6/ext-all.js:22:382867
encodeObject http://localhost/rgpd/extjs6/ext-all.js:22:384535
doEncode http://localhost/rgpd/extjs6/ext-all.js:22:383117
encodeObject ...
found the issue for "too much recursion" error. remoteFilter: true must be remoteFilter: false, in the store definition.

Extjs 4 grid store MVC with Ext direct url is undefined

I have a grid with store, and I want to load on render or click a button, but when I try to load the grid, got an url is undefined error. I need to use Ext direct, so no url. What should I do?
Ext.define('My.view.Grid' ,{
extend: 'Ext.grid.Panel',
//...
store: 'MyStore',
//...
}
Store:
Ext.define('My.store.MyStore', {
extend: 'Ext.data.JsonStore',
//...
model: 'My.model.MyModel',
proxy: {
type: 'direct',
directFn: Ext.direct.Class.function,
paramOrder: ['start', 'limit', 'sort', 'active'],
reader: {
type: 'json',
root: "data",
idProperty: 'id',
totalProperty: "all"
},
extraParams: {
active: 1
}
},
remoteSort: true,
sorters: ['name']
//...
Extend your store from Ext.data.Store:
Ext.define('My.store.MyStore', {
extend: 'Ext.data.Store',
// ...
});
If you see the source code of Ext.data.JsonStore, you will see that there is predefined an ajax proxy:
constructor: function(config) {
config = Ext.apply({
proxy: {
type : 'ajax',
reader: 'json',
writer: 'json'
}
}, config);
this.callParent([config]);
}

How to access Extjs 4 store base properties

How can I access custom root property from the store like built in properties such as idProperty, totalProperty, messageProperty etc. Please check the code for ref.
Ext.define('app.store.Reviews', {
extend: 'Ext.data.Store',
model: 'app.model.Review',
pageSize: 200,
remoteSort: true,
// allow the grid to interact with the paging scroller by buffering
buffered: true,
proxy: {
type: 'ajax',
url: 'review/list',
reader: {
type: 'array',
root: 'list',
totalProperty: 'count',
myCustomproperty: 'fieldInJson' // Somewhat like this
},
filterParam: 'query'
}
});
update: also I could be able to access the property via store
The jsonData object is available from the proxy.reader. You could access this data in the store load event by adding listener:
Ext.define('app.store.Reviews', {
extend: 'Ext.data.Store',
model: 'app.model.Review',
pageSize: 200,
remoteSort: true,
// allow the grid to interact with the paging scroller by buffering
buffered: true,
proxy: {
type: 'ajax',
url: 'review/list',
reader: {
type: 'array',
root: 'list',
totalProperty: 'count'
},
filterParam: 'query'
},
listeners: {
load: function (store,records,successful,eOpts) {
//older
console.log(store.proxy.reader.jsonData);
//4.2
console.log(store.getProxy().getReader().jsonData);
}
}
});

How to get store object from existing store object in ExtJs

I am using ExjJs 4.2 .I have two grids displaying different data. In one javascript file i have to include both the grids. I am loading the store for the first grid like
var store = Ext.create('Ext.data.Store', {
model: 'Writer.Person',
autoLoad: true,
proxy: {
type: 'ajax',
url : 'findPatientRecordAction',
reader: {
type: 'json',
successProperty: 'success',
root: 'data',
messageProperty: 'message'
},
writer: {
type: 'json',
encode: true,
root: 'data'
},
fields: ['id','notes', 'startDate', 'endDate'],
},
});
The JSON object store looks like this :
"data":[{"id":35,"notes":"hiii","startDate":"2013-04-30","endDate":"2013-05-02"}]} , "details":[{"id":14,"document":"docpath12345","date":"2013-04-28"}]
Actually the first grid will display only the 'data' part of JSON and the second grid will display the 'details' part of JSON. So ideally i should not load the store object again for the second grid(what i am doing now).I want to declare one more store type variable and reuse value of 'store' (the 'documents' part of store) to become the store for second grid as
I am loading the second store as of now like this
var store2 = Ext.create('Ext.data.Store', {
model: 'Writer.Document',
autoLoad: true,
proxy: {
type: 'ajax',
url : 'findPatientRecordAction',
reader: {
type: 'json',
successProperty: 'success',
root: 'details',
messageProperty: 'message'
},
writer: {
type: 'json',
encode: true,
root: 'details'
},
fields: ['id','document', 'date'],
},
});`.
In both the cases i am calling the same action and getting the same JSON.
Can anyone help me out here that how can I declare another store object and get the value from the existing 'store' so that i should not have to load the same data for 2 times.
Regards : Dev
You can make the first store have autoload set to false. Then in the second store use the load event to inject the records into the first using that stores active proxy data:
var store2 = Ext.create('Ext.data.Store', {
model: 'Writer.Document',
autoLoad: true,
proxy: {
type: 'ajax',
url : 'findPatientRecordAction',
reader: {
type: 'json',
successProperty: 'success',
root: 'details',
messageProperty: 'message'
},
writer: {
type: 'json',
encode: true,
root: 'details'
},
fields: ['id','document', 'date'],
},
listeners: {
load: function(store, records, successful, eOpts){
if(successful)
{
store1.loadData(store.proxy.reader.rawData.data);
}
}
}
});
I'll replace:
listeners: {
load: function(store, records, successful, eOpts){
if(successful)
{
store1.loadData(**store.getRange()**);
}
}
}
This is the same, I think:
store.proxy.reader.rawData.data <--> store.getRange()

Resources