I want to load a record based on my tree node selection and show it in my form. But my store is not loaded in my controller handler. Where and how should I load the store with my parameter?
var me = this;
var idTag= me.getMyNode();
me.getTagStore().on('beforeload', function(store, operation, eOpts) {
operation.params = {
idTag: idTag
};
}, me);
// it will not be loaded here and I get rec=undefined
var rec = me.getTagStore().load().getAt(0);
me.getMyForm().loadRecord(rec);
And here is my store:
Ext.define('TTT.store.Tag', {
extend: 'Ext.data.Store',
requires: [
'TTT.model.Tag'
],
model: 'TTT.model.Tag',
proxy: {
type: 'ajax',
url: 'tag/find.json',
reader: {
type: 'json',
root: 'data'
}
}
,baseParams: {
idTag: 30
},
});
You can call the load method manually whenever needed and pass any parameters needed, like this:
me.getTagStore().load({
params: {
idTag: idTag
}
});
When you call this you should see your request with the parameter in your console window in the browser.
In your code above, this line:
// it will not be loaded here and I get rec=undefined
var rec = me.getTagStore().load().getAt(0);
The load operation takes a little time, not much but it does take a little time. You need to listen to the load event on your store to be able to do things like this, here is an example:
me.getTagStore().load({
params: {
idTag: idTag
},
scope: this,
callback: function(records, operation, success) {
// the operation object
// contains all of the details of the load operation
console.log(records);
}
});
Checkout the documentation for store.
Related
I have list and pull down to refresh plugin in it. When the refresh function fires i would like to show the load mask. But its not showing there. when i commented store.removeAll(); line i can see the loadmask working. I dont konw whats the problem with store.removeAll(). Please help me to solve this issue. Thanks in advance
{
xclass: 'Ext.ux.PullRefreshFn',
pullRefreshText: 'Pull down for refresh Contacts!',
refreshFn: function() {
console.log('pull refresh working');
Ext.Viewport.setMasked({
xtype: 'loadmask',
message: 'Please Wait...'
});
var store = Ext.getStore('Contactsstore');
store.removeAll();
var url = apiurl+'Contact.ashx?switch=GetContactList&api_id=4&getDataAgain=true';
store.getProxy().setUrl(url);
store.loadPage(1,{
callback: function (records, operation, success, response) {
if (success==1) {
Ext.Viewport.setMasked(false);
} else {
Ext.Viewport.setMasked(false);
}
}
});
Ext.getCmp('searchcontact').reset();
}
}
Here is my Store configuration
Ext.define('WinReo.store.Contactsstore', {
extend: 'Ext.data.TreeStore',
requires: [
'WinReo.model.Contactsmodel'
],
config: {
storeId: 'Contactsstore',
defaultRootProperty: 'items',
model: 'WinReo.model.Contactsmodel',
autoLoad: false,
pageSize: 20,
proxy: {
type: 'ajax',
method:'post',
id: 'Contactsstoreproxy',
url:apiurl+'Contact.ashx?switch=GetContactList&api_id=4&getDataAgain=false',
reader: {
type: 'json',
rootProperty: 'items'
}
},
listeners: {
load: function(store, records, successful, operation, eOpts) {
callback:{
succes:
if(store.getCount()!=0){
var RecordCount=store.getAt(0).get('RecordCount');
//console.log('RecordCount',RecordCount);
store.setTotalCount(RecordCount);
var storectscount = store.getTotalCount();
//Ext.Msg.alert('Store Total count',storectscount, Ext.emptyFn);
}
}
}
}
}
}
});
Loading masks won't get rendered until the browser has a chance to render them, and that won't happen until your Javascript code completes. I suspect that for some reason, the removeAll call isn't completing quickly (or at all), or an event listener on clear isn't completing like it needs to. Check your store's configuration for syncRemovedRecords: true and autoSync: true. You can also try removeAll(true) to keep the clear event from firing.
Update
Looking at your store definition, I can see at least one problem: Your load listener doesn't look like it's defined properly. You're defining a callback field inside of a function (which isn't going to compile), and 'succes' is misspelled. Is this what you had in mind?
load: function(store, records, successful, operation, eOpts) {
if(successful === true && store.getCount()!=0){
var RecordCount=store.getAt(0).get('RecordCount');
store.setTotalCount(RecordCount);
}
}
}
I have a Panel whose layout is accordin. I have a datastore which gets the data from server in json format and maps to a model. The data I get is a list of that model. Now in my view, I need to access the store and get all the records, and for each record I need to create a Panel and add it to the parent Panel.
My Store
Ext.define('NOS.store.ResultGroupsStore', {
extend: 'Ext.data.Store',
requires : ['NOS.model.ResultGroup'],
model: 'NOS.model.ResultGroup',
fields:['groupName'],
alias: 'widget.resultStore',
// autoLoad: true,
proxy: {
type: 'ajax',
url: 'showResult.nos',
reader: {
type: 'json'
}
}
});
My View
Ext.require('Ext.panel.Panel');
Ext.define('NOS.view.ResultView' ,{
extend: 'Ext.panel.Panel',
alias:'widget.results',
title: 'My title',
layout:'accordion',
items:null,
initComponent : function() {
console.log("reached in view initialise");
results = Ext.create('NOS.store.ResultGroupsStore');
results.load();
results.each(function(aResultGroup, index) {
console.log('Record ' + index);
});
console.log("End in view initialise");
this.callParent(arguments);
},
});
But it never enters the loop above. I am sure the data is loaded properly to the store because when I use a grid.Panel and map the columns it renders the data.
Please help!!
Store loading is asynchronous, when you you're iterating over the store, it hasn't loaded yet so the store is empty. You need to do it in the store callback.
Ext.require('Ext.panel.Panel');
Ext.define('NOS.view.ResultView' ,{
extend: 'Ext.panel.Panel',
alias:'widget.results',
title: 'My title',
layout:'accordion',
items:null,
initComponent : function() {
var results = this.results = Ext.create('NOS.store.ResultGroupsStore');
results.on('load', this.onStoreLoad, this, {
single: true
});
results.load();
this.callParent(arguments);
},
onStoreLoad: function() {
this.store.each(function(rec){
});
}
});
I want to filter an Ext store using a custom algorithm. The JSON/AJAX proxy returns >100 records and I need to reduce the number to the top-5 based on a number of criteria.
How to go about this (not the algorithm but where to trigger it)?
My current approach is to use a custom reader like so
Ext.define('MyReader', {
extend : 'Ext.data.reader.Json',
alias : 'reader.myReader',
getResponseData : function(response) {
var data = this.callParent([response]);
// algorithm
return filteredData;
}
});
Ext.define('SunApp.store.Stations', {
extend: 'Ext.data.Store',
requires: ['MyReader'],
config: {
model: 'SunApp.model.Station',
autoLoad: true,
proxy: {
type: 'ajax',
url: 'data.json',
reader: {
type: 'myReader'
}
}
}
});
but I'd much rather base the algorithm on the store model than on the raw JSON data. Hence, I'd like to filter while/after the data is loaded into the store. Note that the store is created implicitly by Ext when the list view is displayed:
Ext.define('SunApp.view.Stations', {
extend: 'Ext.List',
xtype: 'stations',
config: {
title: 'Stations',
store: 'Stations',
...
Therefore, I can't just add a function to the store that I invoke manually before it's being passed to the list.
In this case where the filter may need to traverse the store data (i.e. records) several times to reduce the full set to the desired set I believe (I'm still a Sencha rookie...) the following may be fine:
write an on-load event listener
do the magic filtering
call setData on the store passing the filtered data
Hence, instead of filtering in the reader it'd be like this:
Ext.define('SunApp.store.Stations', {
extend: 'Ext.data.Store',
config: {
model: ...,
sorters: [...],
listeners: {
load: function(store, records, success, eOpts) {
var filteredRecords = filter(records);
store.setData(filteredRecords);
}
},
proxy: ...
}
});
I am using extjs(4.1.0) MVC .
I have a custom grid set up with combo box filter, datepicker filters etc. I am attaching snippets from the controller, view, and store.
I response coming in is different from the model, hence, the response is intercepted by overriding a read() of the story proxy.
Also data needs to be sent as Json data in payload, so the buildRequest() of store proxy is also overridden to add json data to request. (all sort, paging params are also added here)
Adding to this - the view has infinite scrolling.
The view loads fine the first time. But on combobox change/date change, store is reloaded with passing these as json params in request.
I see the response in XHR in firebug/chrome, but the grid continously shows the loading mask, waiting for loading.. Kindly help on what could be going wrong.
Ext.define('Message.store.ListingStore', {
extend: 'Ext.data.Store',
model: 'Message.model.ListingModel',
autoLoad: true,
remoteSort: true,
purgePageCount:0,
buffered: true,....
.......
proxy: {
type: 'ajax',
actionMethods: {
read: 'POST'
},
url: 'http://localhost:8080
buildRequest: function(operation) {
Ext.apply(request, {
params: null,
jsonData: Ext.apply(request.params, {
page: operation.page,
start: operation.start,
limit: operation.limit,
catId:catValue,
sort:operation.sorters[0].property,
dir:operation.sorters[0].direction
})
});
........return request;
}
root: 'data',
totalProperty:'total',
successProperty :'success'
},
.....
});
//controller
Ext.define('Message.controller.Listing', {
extend:'Ext.app.Controller',
stores: ['ListingStore','SubscriptionStore','SubCategoryMasterStore','PriorityMasterStore'],
models: ['ListingModel'],
views: [
'MessageListing'
],
init:function () {
var controller = this;
'messagelisting combo' : {
select : this.handleComboSelect
},
'messagelisting datefield' : {
select : this.handleDateSelect
},
.....
handleComboSelect : function(gridview, el, rowIndex, colIndex, e, rec, rowEl) {
this.getStore('ListingStore').load();
},
handleDateSelect: function(gridview, el, rowIndex, colIndex, e, rec, rowEl) {
this.getStore('ListingStore').load({callback: function(response){
}});
This is a very complex case, so i am unable to post the entire code.
Thank You.
I think you have the issue described by me here:
http://www.sencha.com/forum/showthread.php?242445-Grid-filters-race-conditions-FiltersFeature
Regards,
Alek
I would like to declare a global variable that takes value from DataStore and display its value in the alert Window.
var params = 1;
var storeTeacher = new Ext.data.JsonStore({
id: 'IDstoreTeacher',
url: 'school.php',
method: 'POST',
baseParams:{task: "LABEL",
parametar: params},
root: 'rows',
fields:
[{name: 'NameT', type: 'string', mapping: 'Teacher_name'}],
autoLoad: true
});
var TeacherName;
storeTeacher.load({
scope: this,
callback: function (records, operation, success) {
TeacherName = storeTeacher.getAt(0).get('NameT');
}
});
alert(TeacherName);
But, only thing I get is the alert Window that says: undefined
Your alert(TeacherName); call is being executed before the callback on your storeTeacher.load callback is being executed.
If you put the alert(TeacherName) in the callback function it will populate your TeacherName properly and show correctly in the alert:
storeTeacher.load({
scope: this,
callback: function (records, operation, success) {
TeacherName = storeTeacher.getAt(0).get('NameT');
alert(TeacherName);
} });
This behavior is expected.