PDA

View Full Version : How to make a store that inherits from a common store?



delebash
4 May 2013, 8:58 AM
I followed this code (http://skirtlesden.com/articles/custom-proxies)to make a common proxy and it worked great, except that the store properties where not inherited such as auto sync. So I am trying to follow the same principle but instead of defining proxy as type I am using store instead but I can't get it to work. It says users is undefined when I try to load the store in my controller.

Here is my common store definition

var crudServiceBaseUrl = "http://127.0.0.1:8081/cors/";

Ext.define('SimplyFundraising.store.SFStore', {
alias: 'SFStore',
extend : 'Ext.data.Store',
autoLoad : false,
autoSync : true,


proxy : {
directionParam : null,
filterParam : null,
groupDirectionParam : null,
groupParam : null,
limitParam : null,
pageParam : null,
sortParam : null,
startParam : null,

api : {
create : crudServiceBaseUrl + '/{entity}/?$method=update',
read : crudServiceBaseUrl + '/{entity}',
update : crudServiceBaseUrl + '/{entity}/?$method=update',
destroy : crudServiceBaseUrl + '/{entity}({id})/?$method=delete',
},
reader : {
type : 'json',
root : '__ENTITIES',
successProperty : false,
getResponseData : function(response) {
var findme = "__ENTITIES";
if (response.responseText.indexOf(findme) > -1) {
//Already an array of __ENTITIES
} else {
//Single result turn into array __ENTITIES
response.responseText = '{"__ENTITIES":[' + response.responseText + ']}';
}
var data = Ext.data.reader.Json.prototype.getResponseData.call(this, response);
return data;
}
},
writer : {
type : 'json',
writeAllFields : false,
getRecordData : function(record, operation) {
switch(operation.action) {
case 'create':
console.log('INFO', 'Create');
delete record.data.__KEY
delete record.data.__STAMP
return record.data
break;
case 'update':
console.log('INFO', 'Updating');
var myrecord = record.getChanges();
myrecord.__KEY = record.data.__KEY
myrecord.__STAMP = record.data.__STAMP
return myrecord
break;
}
}
}
},

buildUrl : function(request) {
var url = this.callParent(arguments);
return this.replaceTokens(url, request);
},

replaceTokens : function(str, request) {
var me = this;

return str.replace(/{(.*?)}/g, function(full, token) {
// We read the id from the request params, the category is read from the proxy itself
return encodeURIComponent(request.params[token] || me[token]);
});
}
});



Here is the user specific store

Ext.define('SimplyFundraising.store.Users', {
type: 'store.SFStore',
model : 'SimplyFundraising.model.User',
proxy: {
entity: 'Users'
}
});



Any ideas on what I am doing wrong?

Thanks,
Dan

slemmon
6 May 2013, 7:48 PM
Have your Users store extend: 'SimplyFundraising.store.SFStore'.

Also, when you set an alias on a store you should use the syntax store.alias (alias: 'store.SFStore').

delebash
7 May 2013, 8:25 AM
Thank you, that worked!