store.sync after pressed OK button only reload the store - extjs

Ext.define('RouteSeqModel', {
extend: 'Ext.data.Model',
fields: [{name: '_id', type: 'number'}, {name: 'Route_Seq' , type: 'int'},'Location_Name','Route_LocationID','Route_ID']
});
var RouteSeqStore = Ext.create('Ext.data.JsonStore', {
model: 'RouteSeqModel',
storeId: 'RouteSeqStore',
autoLoad: false,
sorters: [{
property: 'Route_Seq',
direction: 'ASC'
}],
proxy: {
type: 'ajax',
url: 'get-routeseq.php',
api: {
create: 'insert-routeseq.php',
//read: 'http://visual04/ModuleGestion/php/Pays.php?action=read',
update: 'update-routeseq.php',
//destroy: 'http://visual04/ModuleGestion/php/Pays.php?action=destroy'
},
actionMethods: 'POST',
baseParams: {
_id : 0,
},
reader: {
type: 'json',
idProperty: '_id'
},
writer: {
type: 'json',
id: '_id'
}
}
});
RouteSeqStore.sync({
success: function(batch) {
//var button = Ext.getCmp('BtnRouteSeqRefresh');
//button.fireEvent('click', button); //need at here , if not too fast refresh will get the old data
grid.setLoading(false);
Ext.MessageBox.show({
title: "Information",
msg: batch.operations[0].request.scope.reader.jsonData["message"],
icon: Ext.MessageBox.INFO,
buttons: Ext.MessageBox.OK,
fn: function(buttonId) {
if (buttonId === "ok") {
//load store at here
}
}
});
},
failure: function(batch){
//var button = Ext.getCmp('BtnRouteSeqRefresh');
//button.fireEvent('click', button); //need at here , if not too fast refresh will get the old data
grid.setLoading(false);
RouteSeqStore.rejectChanges();
Ext.MessageBox.show({
title: "Error",
msg: batch.operations[0].request.scope.reader.jsonData["message"],
icon: Ext.MessageBox.ERROR,
buttons: Ext.MessageBox.OK,
fn: function(buttonId) {
if (buttonId === "ok") {
//load store at here
}
}
});
}
});
whenever called .sync function will automatically load the datastore with get-routeseq.php ,
is that have any way after pressing OK button only load the datastore(after pressed OK only call the get-routeseq.php)?
my autoLoad is false, but still automatically load the datastore.

I think you should use type: 'rest' and avoid url config in proxy.
Something like this:
var RouteSeqStore = Ext.create('Ext.data.JsonStore', {
model: 'RouteSeqModel',
storeId: 'RouteSeqStore',
autoLoad: false,
sorters: [{
property: 'Route_Seq',
direction: 'ASC'
}],
proxy: {
type: 'rest',
api: {
create: 'insert-routeseq.php',
read: 'get-routeseq.php',
update: 'update-routeseq.php',
//destroy: 'http://visual04/ModuleGestion/php/Pays.php?action=destroy'
},
actionMethods: 'POST',
baseParams: {
_id : 0,
},
reader: {
type: 'json',
idProperty: '_id'
},
writer: {
type: 'json',
id: '_id'
}
}
});
And if you want to just load store, use RouteSeqStore.load(). After you have made changes in store, use RouteSeqStore.sync().

Related

extjs how to pass data from view to store using proxy extraParams

how can I pass parameters from the view to the store in extjs?
this is my store:
Ext.define('mycomponents.store.mystore', {
extend: 'Ext.data.Store',
alias: 'store.levels',
model: 'mycomponents.model.mymodel',
storeId: 'levels',
restful: true,
autoLoad: true,
proxy: {
type: 'ajax',
headers: {
'Accept': '*/*',
'Cache-Control': 'no-cache',
'Content-Type': 'application/json',
'Authorization': localStorage.token
},
extraParams: {
sort: 'levelid',
'filter[active]': true,
'filter[idparent]': 0
},
reader: {
type: 'json',
root: 'data',
successProperty: 'success'
},
writer: {
type: 'json',
writeAllFields: true,
encode: true,
root: 'data'
},
listeners: {
exception: function (proxy, response, operation) {
var error = Ext.decode(response.responseText);
Ext.MessageBox.show(
{
title: 'REMOTE EXCEPTION',
msg: error.message,
icon: Ext.MessageBox.ERROR,
buttons: Ext.Msg.OK
}
);
}
},
actionMethods: {
read: 'GET'
},
api: {
read: 'http://url...',
create: 'http://url...',
update: 'http://url...',
destroy: 'http://url...'
},
autoSave: true
},
constructor: function (config) {
this.callParent([config]);
}
});
My view:
var store = Ext.create('mycomponents.store.mystore', {});
Ext.define('mycomponents.view.myview', {
extend: 'Ext.container.Container',
id: 'leves',
alias: 'widget.levels',
xtype: 'levels',
items: [
{
xtype: 'combobox',
...
}
]
...
}
I need from the view send 'filter[idparent]': 1, 'filter[idparent]': 2, or whatever on combobox change. How can I achieve this?
You need to attach on change listener in combobox and use method setExtraParam in store.
Example:
Ext.define('mycomponents.view.myview', {
extend: 'Ext.container.Container',
id: 'leves',
alias: 'widget.levels',
xtype: 'levels',
items: [
{
xtype: 'combobox',
listeners:{
change: function (cmp, newV, oldV, opt) {
Ext.getStore('levels').getProxy().setExtraParam('filter[idparent]', newV);
Ext.getStore('levels').load();
}
}
...
}
]
...
}

ExtJs-> Ext.Store.proxy.writer error: No type specified for writer.create

So I have upgraded from extjs4 to extjs6 and this store always have this error:
No type specified for writer.create
This code has two uses:
For getting the data for the initial page
For getting the data for whenever a button is clicked.
Whenever I comment out the proxy.writer code portion, it will produce the data for the initial page.
But whenever I won't comment it out, it will not get the data for the initial page.
And it will also return this error whenever I clicked a button:
Uncaught TypeError: items.slice is not a function
So my question is, is the writer portion have wrong syntax or something since it is updated to extjs6?
P.S. I tried to change the
this.callOverridden to this.callParent since it states that the this.callOverridden is already deprecated, still has the same error.
Ext.define('Stm.store.stmpdate', {
extend: 'Extends.data.Store',
requires: [
'Cstm.Setting',
'Stm.model.stmpdate'
],
model: 'Stm.model.stmpdate',
pageSize: Stm.Const.controller.dataCountLimit,
remoteSort: true,
proxy: {
type: 'ajax',
url: Cstm.Setting.getEntryUrl() + '/stm/stm-update/stm-update',
reader: {
type: 'json',
rootProperty: 'data'
},
writer: Ext.data.writer.Json.override({
getRecordData: function() {
var data = this.callOverridden(arguments);
var record = arguments[0];
if ( record.associations.length > 0 ) {
Ext.Array.each(record.associations.keys, function(key) {
data[key] = [];
var assocStore = record[key]();
Ext.Array.each(assocStore.data.items, function(assocItem) {
data[key].push(assocItem.data);
});
});
}
return data;
}
}),
api: {
create: Cstm.Setting.getEntryUrl() + '/stm/stm-update/application',
update: Cstm.Setting.getEntryUrl() + '/stm/stm-update/approval'
},
actionMethods: {
create: 'POST',
read: 'POST',
update: 'POST',
destroy: 'POST'
}
},
sorters: [
{property: 'aplDatetime', direction: 'DESC'},
{property: 'siteDomain', direction: 'ASC'},
{property: 'pageName', direction: 'ASC'}
]
});
Model:
Ext.define('Stm.model.stmpdate', {
extend: 'Ext.data.Model',
fields: [
{name: 'siteId', type: 'integer'},
{name: 'siteName', type: 'string'},
{name: 'siteUrl', type: 'string'},
{name: 'tmpId', type: 'integer', defaultValue: 1},
{name: 'updType', type: 'string'}
],
hasMany: [{
model: 'Stm.model.ServerInfo',
name: 'servers',
associationKey: 'servers',
reference: 'tmpId'
}]
});
Yes, you are using override in a way that may have worked in ExtJS 4 but is AFAIK unsupported across all versions of ExtJS.
What you want to do is define your custom writer as a new class:
Ext.define('MyApp.app.data.MyCustomJsonWriter', {
extend: 'Ext.data.writer.Json',
alias: 'writer.mycustomjson',
getRecordData: function() {
var data = this.callParent(arguments);
var record = arguments[0];
if ( record.associations.length > 0 ) {
Ext.Array.each(record.associations.keys, function(key) {
data[key] = [];
var assocStore = record[key]();
Ext.Array.each(assocStore.data.items, function(assocItem) {
data[key].push(assocItem.data);
});
});
}
return data;
}
});
require that class from your store:
requires: [
'MyApp.app.data.MyCustomJsonWriter'
]
and then instantiate it by alias:
writer: {
type: 'mycustomjson'
}

store sync data does not work

my store's code
Ext.define('Console.store.SubjectMaterial.DetailStore', {
extend: 'Ext.data.Store',
model: 'Console.model.SubjectMaterial.DetailModel',
proxy: {
actionMethods: 'POST',
type: 'ajax',
api: {
create: './modules/source/controller/SubjectMaterial/insertDetail.php',
read: './modules/source/store/SubjectMaterial/selectDetail.php',
destroy: './modules/source/controller/SubjectMaterial/deleteDetail.php'
},
reader: {
type: 'json',
root: 'result'
},
writer: {
type: 'json',
root: 'data',
encode: true
}
},
sorters: {
direction: 'ASC',
property: 'material_name'
},
remoteSort: true,
autoLoad: false,
autoSync: true
});
my model's code
Ext.define('Console.model.SubjectMaterial.DetailModel',{
extend:'Ext.data.Model',
fields:[
{
name:'material_id',
type:'string'
},{
name:'material_name',
type:'string'
},{
name:'material_version',
type:'string'
},{
name:'material_picture',
type:'string'
},{
name:'material_detail',
type:'string'
},{
name:'material_url',
type:'string'
},{
name: 'material_size',
type: 'string'
},{
name: 'material_author',
type: 'string'
},{
name: 'subject_share',
type: 'string'
},{
name: 'subject_secure',
type: 'string'
},{
name: 'material_create_date',
type: 'string'
},{
name: 'material_update_date',
type: 'string'
}]
});
my controller's code
detailInsertConfirm: function(obj, e, eOpts) {
var masterSelectedRecord = Ext.getCmp('sumagridmaster').getSelectionModel().getSelection()[0];
var detailStore = Ext.getCmp('sumagriddetail').store;
var actionPanel = obj.up('sumaactionpanel');
var gridPanel = obj.up('sumaformdetailinsert');
// console.log(gridPanel);
// var formPanel = gridPanel.getForm();
//insert
detailStore.add(gridPanel.getSelectionModel().getSelection());
// detailStore.add(masterSelectedRecord);
console.log(detailStore.data);
// console.log(gridPanel.getSelectionModel().getSelection());
detailStore.sync({
callback: function(batch, options) {
// body...
console.log('this is callback.');
},
success: function(batch, options) {
console.log('this is success.');
Ext.MessageBox.show({
title: MSG['universal_msg_box_header_text'],
icon: Ext.MessageBox.INFO,
msg: MSG['universal_msg_box_content_insert_success'],
closable: false,
buttons: Ext.MessageBox.OK,
fn: function(buttonId,text,opt){
if (buttonId == 'ok') {
detailStore.reload();
}
}
});
},
failure: function(batch, options) {
console.log('this is failure.');
Ext.MessageBox.show({
titel: MSG['universal_msg_box_header_text'],
icon: Ext.MessageBox.ERROR,
msg: MSG['universal_msg_box_content_insert_fail'],
closable: false,
buttons: Ext.MessageBox.OK
});
}
});
And I don't know why my controll's detailStore.sync cannot work.
Doesn't have any error code.
Just totally no Response....
And I always check my network.
It have no requset.
I'm not quite sure what you're asking. For sync to work you need to change something in the store (modify, delete, add), you need a writer on the proxy and when you then call store.sync() the request(s) is sent to the server. Only after that a response arrives.
Inside proxy make your autoSync:false . When autoSync: true then no need to call detailStore.sync({....}).

Add value to extjs textarea from store

I want to get data from mongo data base and add it to an extjs text area.
Everything is ready, my mongo data base returns a list of user email addresses that I want to add to the extjs text area.
I use this code:
var index = Ext.StoreMgr.lookup("writeup.store.Employee").findExact('name',name);
var rec = Ext.StoreMgr.lookup("writeup.store.Employee").getAt(index);
alert(rec.data.name);
But I get this error:
TypeError: Ext.StoreMgr.lookup(...) is undefined
And this is my store
Ext.define('writeup.store.Employee', {
extend: 'Ext.data.Store',
model: 'writeup.model.Employee',
autoLoad: true,
proxy: {
type: 'ajax',
actionMethods: {
read : 'POST'
},
api: {
read : 'user/view.action'
},
reader: {
type: 'json',
root: 'data',
successProperty: 'success'
},
writer: {
type: 'json',
writeAllFields: true,
encode: false,
root: 'employee'
},
listeners: {
exception: function(proxy, response, operation){
Ext.MessageBox.show({
title: 'REMOTE EXCEPTION',
msg: operation.getError(),
icon: Ext.MessageBox.ERROR,
buttons: Ext.Msg.OK
});
}
}
}
});
try this
Load this code into your controller
var store_name = Ext.create('Ext.data.Store', {
model: 'model name'
});
store_name.load();
store_name.each(function(record){
for(var i=0;i<record.data.yourlocalstorageid.length;i++){
var index = record.data.yourlocalstorageid[i].name;
}
}

Extjs 4, Dynamic Tree and store re-mapping

I need to Extjs Tree panel with dynamic remote data(JSON) for file listing.
and the date field name is not fit to Extjs tree store field. so I need to re-mapping to make fit, like adding leaf field and text field.
the return JSON data is like this:
[{
"id":1,
"yourRefNo":"A91273",
"documentName":"Test Document",
"documentFileName":"login_to_your_account-BLUE.jpg",
"updatedBy":"root root",
"updatedAt":"\/Date(1343012244000)\/"
}]
and this is the tree panel:
Ext.define('App.view.Document.DocumentList', {
extend :'Ext.tree.Panel',
rootVisible : false,
alias: 'widget.Document_list',
store: 'DocumentList_store'
});
and this is the store:
Ext.define('App.store.DocumentList_store', {
extend: "Ext.data.TreeStore",
model: 'App.model.DocumentList_model',
proxy: {
type: 'ajax',
url: '/Document/GetDocumentList/',
actionMethods: {
read: 'POST'
},
reader: {
type: 'json',
root: '' // there is no root
},
pageParam: undefined,
startParam: undefined,
pageParam: undefined
},
root: {
children: []
},
autoLoad: false,
listeners: {
append: function (thisNode, newChildNode, index, eOpts) {
console.log(newChildNode.get('documentName')); // 'Test Document'
newChildNode.set('leaf', true);
newChildNode.set('text', newChildNode.get('documentName'));
// it does not add to tree panel.
}
}
});
after load data from server, and it call the append function well. but after that, nothing show up in tree panel.
What I am doing wrong? please advice me.
Thanks
[EDIT]
This is the model,
Ext.define("App.model.DocumentList_model", {
extend: "Ext.data.Model",
fields: [
'id','yourRefNo','documentName','documentFileName','updatedBy','updatedAt'
]
});
I'm fusing your code with a piece of working code of mine. Try see if this works:
Model:
Ext.define("App.model.DocumentList_model", {
extend: 'Ext.data.Model',
fields: [
{name: 'id'},
{name: 'yourRefNo'},
{name: 'documentName' },
{name: 'documentFileName'},
{name: 'updatedBy'},
{name: 'updatedAt', convert: function(v) { return v;} }, // Notice you can do field conversion here
{name: 'leaf', type: 'boolean', defaultValue: false, persist: false},
],
proxy: {
type: 'ajax',
url: '/Document/GetDocumentList/',
actionMethods: {
read: 'POST'
},
reader: {
type: 'json',
root: 'children'
},
},
});
Store:
Ext.define('App.store.DocumentList_store', {
extend: "Ext.data.TreeStore",
model: 'App.model.DocumentList_model',
root: {
text: 'Root',
id: null,
expanded: true
},
autoLoad: false,
});
JSON Response:
{
"success":true,
"children":[{
"id":1,
"yourRefNo":"A91273",
"documentName":"Test Document",
"documentFileName":"login_to_your_account-BLUE.jpg",
"updatedBy":"root root",
"updatedAt":"\/Date(1343012244000)\/",
"leaf":false
}]
}

Resources