We are using Extjs 5.x for building browser based single page application. Everything is working fine if simple model & store used by us. If we are trying to use any wrapper for proxy it's generate error during runtime after having sencha cmd build.
Here is the simple Model of Article:
Ext.define('Pos.model.Article', {
extend : 'Ext.data.Model',
alias : 'widget.Article',
idProperty : 'id',
fields : [
{ name : 'id', mapping : 'id', type : 'int' },
{ name : 'code', mapping : 'code', type : 'string'},
{ name : 'name', mapping : 'name', type : 'string'},
{ name : 'rate', mapping : 'rate', type : 'float' },
{ name : 'expireDate', mapping : 'expireDate', type : 'date' }
],
proxy : {
type : 'rest',
noCache : true,
limitParam : 'limit',
startParam : 'start',
url : '/pos/article',
reader : {
type : 'json',
rootProperty : 'data',
totalProperty : 'total',
successProperty : 'success',
messageProperty : 'message',
implicitIncludes: true
},
writer :{
type : 'json',
writeAllFields : true
},
simpleSortMode : true
}
});
Here is the simple Store of Article:
Ext.define('Pos.store.Articles', {
extend : 'Ext.data.Store',
model : 'Pos.model.Article',
idProperty : 'id',
autoLoad : false,
autoSync : false,
remoteSort : true,
remoteFilter : true,
pageSize : 25,
proxy : {
type : 'rest',
noCache : true,
limitParam : 'limit',
startParam : 'start',
url : '/pos/article/store',
reader : {
type : 'json',
rootProperty : 'data',
totalProperty : 'total',
successProperty : 'success',
messageProperty : 'message',
implicitIncludes: true
},
writer :{
type : 'json',
writeAllFields : true
},
simpleSortMode : true
},
sorters : [{
property : 'name',
direction: 'ASC'
}]
});
When we are using simple model and store it working fine. But our goal was removing boiler plate of code for this reason we are trying to build some wrapper method to remove the redundant codes. Here is the simple wrapper of proxy builder for your consideration:
var Cki=Cki||{};
;Cki.proxy||(function($){
var proxy = {
$package : 'Cki',
$class : 'Cki.proxy',
resolveUrl : function(action){
var ctxPath = '/pos/',
url = ctxPath + action;
return url;
},
getReader : function(){
var reader = {
type : 'json',
rootProperty : 'data',
totalProperty : 'total',
successProperty : 'success',
messageProperty : 'message',
implicitIncludes: true
};
return reader;
},
getWriter : function(){
var writer = {
type : 'json',
writeAllFields : true
};
return writer;
},
getRestProxy : function(url, noCache){
url = (typeof url === 'undefined') ? '' : url;
noCache = (typeof noCache === 'undefined') ? false : noCache;
var restProxy = {
type : 'rest',
noCache : noCache,
limitParam : 'limit',
startParam : 'start',
url : proxy.resolveUrl(url),
reader : proxy.getReader(),
writer : proxy.getWriter(),
simpleSortMode : true
};
return restProxy;
}
};
$.proxy = proxy;
}(Cki));
Once the wrapper of proxy builder method is ready, we could use it for wrapping the proxy inside model and store. Here is the wrapped Proxy Model:
Ext.define('Pos.model.Article', {
extend : 'Ext.data.Model',
alias : 'widget.Article',
idProperty : 'id',
fields : [
{ name : 'id', mapping : 'id', type : 'int' },
{ name : 'code', mapping : 'code', type : 'string'},
{ name : 'name', mapping : 'name', type : 'string'},
{ name : 'rate', mapping : 'rate', type : 'float' },
{ name : 'expireDate', mapping : 'expireDate', type : 'date' }
],
proxy : Cki.proxy.getRestProxy('article')
});
Here is the wrapped Proxy Store:
Ext.define('Pos.store.Articles', {
extend : 'Ext.data.Store',
model : 'Pos.model.Article',
idProperty : 'id',
autoLoad : false,
autoSync : false,
remoteSort : true,
remoteFilter : true,
pageSize : 25,
proxy : Cki.proxy.getRestProxy('article/store'),
sorters : [{
property : 'name',
direction: 'ASC'
}]
});
It's generate following error during runtime:
- Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.
- http://localhost/pos/static/Ext-all.js Failed to load resource: the server responded with a status of 404 (Not Found)fetch @ Ext-all.js:22
- VM42:3 Uncaught TypeError: c is not a constructor