Is there a feature in ExtJS like dojo.load? - extjs

I was wondering if there is a feature in ExtJS where I can just load the individual components that I need. For example, dojo uses dojo.load() and then I specify in the method what component I want to load, be it fx, numeric or other cool parts.

There's no built-in support for this. However, Doug Hendricks has a UX called $JIT (Just In Time) that is supposed to add such functionality (haven't tried it myself). You might check it out (along with the rest of his ext-basex stuff).

ExtJS uses AJAX in numerous ways. There is a class called Ext.data.Store class that you can specify a URL that in turn references a static class called Ext.Ajax. These data stores are typically used to house data for everything from data grids to combo boxes.
Here's some an example of how an individual component's data could be loaded.
var myStore = new Ext.data.Store({
reader: new Ext.data.JsonReader({
root: root,
fields: [ {name: 'fullname'}
,{name: 'first'}
],
})
idIndex: 0 // id for each record will be the first element
autoLoad: true,
proxy: new Ext.data.HttpProxy({
url: getNames.php
})
});
var combo = new Ext.form.ComboBox({
id: 'myID',
name: 'names',
store: myStore,
displayField: 'first',
valueField :'fullname'
});
The Ext.Ajax class can also be used directly.
// Basic request
Ext.Ajax.request({
url: 'foo.php',
success: someFn,
failure: otherFn,
headers: {
'my-header': 'foo'
},
params: { foo: 'bar' }
});
// Simple ajax form submission
Ext.Ajax.request({
form: 'some-form',
params: 'foo=bar'
});

Related

how do we know which event fired load event in extjs 4.1 store

I am using extjs 4.1 store. Which looks like this:
Ext.define('myStore', {
extend: 'Ext.data.Store',
requires: ['myModel'],
autoLoad: false,
proxy: {
type: 'ajax',
url: '/aaa/bbb',
timeout: '90000',
reader: {
type: 'json',
root: 'data'
}
},
listeners: {
'beforeload': function (store, options) {
},
'load': function (store, options) {
}
},
sorters: [{
property: 'SortOrder',
direction: 'ASC'
}]
});
initially I am loading data through proxy. Later based on user interaction, I will extract the data from the store usign store.proxy.reader.rawData and store it in some variable.
Then at later stage, I will load into the store from the variable using loadRawData().
When loadRawData() is called, it also fires the load event.
What I want: I want to diffrentiate between load event fired due to proxy loading the data for the first time vs load event fired due to loadRawData().
Upgrading to ExtJS version 5.1 allows you access to store.loadCount which will let you to check whether this is the first load or a subsequent load. http://docs.sencha.com/extjs/5.1/5.1.0-apidocs/#!/api/Ext.data.Store-property-loadCount
If you don't want to upgrade, you could manually implement this feature (e.g. increment a custom variable on your store in the 'load' listener) and check it to see which behaviour you want to perform.

Store.load() does not execute after setting a new model

I want to reconfigure a GridPanel using myGridPanel.reconfigure(store,columns). I can't use metachange event for this because I need to make a separate call for metadata (back-end constraint).
I make the call for metadata, I receive the metadata that I need and then create a new model with a new set of fields and a new proxy (new url). I set the model to the existing store (which is bind to my gridPanel) and then I call store.loadPage(1) (because I use a bufferedStore) and myGridPanel.reconfigure(store,meta.columns).
My problem is that store.loadPage(1) is not happening. I don't know what I am missing...
My store looks like this:
Ext.define('Foo.store.Results', {
extend: 'Ext.data.BufferedStore',
requires: [
'Foo.model.ResultRecord'
],
config: {
remoteFilter: true
},
constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
pageSize: 50,
storeId: 'Results',
autoLoad: false,
model: 'Foo.model.ResultRecord',
leadingBufferZone: 50,
trailingBufferZone: 10
}, cfg)]);
}
});
I create a new model like this:
createResultModel: function(url,fields) {
console.log('createResultsModel called');
return Ext.define('Foo.model.ResultsModel'+Ext.id(), {
extend: 'Ext.data.Model',
fields: fields,
proxy: {
type: 'ajax',
url: url,
useDefaultXhrHeader: false,
reader: {
type: 'json',
rootProperty: 'data'
}
}
});
}
And I change the model and reconfigure grid like this:
myStore.setModel(me.createResultModel('resources/data/results.json',meta.fields));
myStore.loadPage(1);
resultsGrid.reconfigure(myStore,meta.columns);
Thank you!
It seems that the store.load() was called but with the wrong url.... . I don't know if that is the desired functionality but it seems that the proxy from the old model was set to the store and when I set the new model, the proxy remained the old one.

EXTJS - How to bind save click of a form.Panel to my Model

Ext.define("Imobfusion.data.User",{
extend: "Ext.data.Model",
fields:[
{name: "name",type: "string"},
{name: "email",type: "email"},
{name: "password",type: "password"}
],
proxy: {
type: 'ajax',
api: {
read: '/user/read',
create: '/user/create',
update: '/user/update',
destroy: '/user/destroy'
},
reader: {
type: 'json'
},
writer: {
type: 'json'
}
}
});
Have a simplist way to bind model with form like this?:
Ext.define('Imobfusion.window.UserForm', {
extend: 'Ext.form.Panel',
model: 'Imobfusion.data.User' //This is my need (XD)
});
You can use loadRecord() method witch loads an Ext.data.Model into this form by calling setValues with the record data and on save use updateRecord() witch persists the values in this form into the passed Ext.data.Model object in a beginEdit/endEdit block. If the record is not specified, it will attempt to update (if it exists) the record provided to loadRecord. or just use getRecord() and getValues() to update the record.
You can load record to form. After editing (record.set()) the record data is sent to the server. You need an example?

Building search application form and displaying result in grid upon submit in extjs-mvc

I am trying to build a search application using ExtJS. I have created dummy form to search for personal details. I have a php script connected to mysql DB. I am able to pass form parameters to php and able to get the return result in msg box. but I am not understanding how to pass it to store and display the same in grid in MVC. I have tried to pass the return data of php to store and then called Grid (List.js) in controller. still did not work. I have shown all the codes that i have used to do this.Another doubt which i have, is that essential to use proxy part of code (i.e url:app/scripts/Info.php) in both store and onSearchButtonClick function in controller? as I can directly pass the return values to store from onSearchButtonClick function, I hope it is not essential to connect php script in both places. However, it would be really nice experts clarify this.
Following is my store:
Ext.define('App.store.Info', {
extend: 'Ext.data.Store',
model: 'App.model.Info',
alias: 'widget.infostore',
pageSize : 50,
autoLoad : false,
remoteFilter: true,
proxy :{
type : 'ajax',
url : 'app/scripts/Info.php',
reader : {
type : 'json',
root : 'result',
successProperty : 'success'
}
},
listeners: {
load : function(store) {
store.each(function(record) {
record.commit();
});
}
}
});
My model looks perfect, simply to reduce somuch code I havent put here
Here is my grid:
Ext.define('App.view.info.List' ,{
extend: 'Ext.grid.Panel',
alias : 'widget.infolist',
store : 'Info',
initComponent: function(){
this.columns = [
{header:'PID',dataIndex:'pid'},
{header:'Name',dataIndex:'name'},
{header:'Address', dataIndex:'address'},
{header:'Contact', dataIndex:'contact'}
];
this.callParent(arguments);
}
});
This is what my php script returns:
{'success':true, 'result':{'pid':'100','name':'Suman','address':'Bangalore','contact':'suman#xyz.com'}}
Here is controller:
Ext.define('App.controller.Info', {
extend: 'App.controller.Base',
models: ['Info'],
stores: ['Info'],
views: [
'info.Index',
'info.List'
],
refs: [{ref: 'info',selector: 'info'}],
init: function(){
console.log('Main controller init');
this.control({
'button[action=search]':{
click: this.onSearchButtonClick
}
});
},
onSearchButtonClick:function(){
var form = Ext.getCmp('ppanel');
if(form.getForm().isValid()){
Ext.Ajax.request({
waitMsg: 'Searching...',
method: 'POST',
url: 'app/scripts/Info.php',
params: {
searchData: Ext.encode(form.getValues())
},
scope:this,
success: this.onSearchSuccess,
failure: this.onSearchFailure
//Ext.MessageBox.alert("XXXXX","dat");
});
}
},
onSearchSuccess: function(response){
var gData = Ext.JSON.decode(response.responseText);
//var grid = Ext.widget('infolist'); //not working -need help
this.getInfoStore().load(gData);
//Ext.getCmp().setActiveItem('infolist'); //not working-need help
//this.getViewport().getLayout().setActiveItem('infolist'); //not working need help
Ext.MessageBox.alert("XXXXX",response.responseText); //works
},
onSearchFailure: function(err){
Ext.MessageBox.alert('Status', 'Error occured during searching...');
}
});
I hope I have provided required information to understand the problem. Looking forward some sort of help.
The problem is that you have two instances of the store, one in grid and one in controller.
If you want a single instance store (like it seems you want) you have two options:
Add it to your application
Assign a storeId to your store definition.
(if you already added that store to your application, ignore the above text)
Or, better yet, do not work directly with the store but with your grid, like this:
First add a ref to your view->grid in your controller:
refs: [{ref: 'info',selector: 'info'},{selector:'infolist', ref:'infoGrid'}]
And then, in your onSearchSuccess handler, instead of calling: this.getInfoStore().load(gData); you should call: this.getInfoGrid().getStore().loadData(gData);
BTW: this.getInfoStore().load(gData); will never load an array of data or a record, for that you should use: this.getInfoStore().loadData(gData);
Hope this gets you in the right track.

ExtJS 4.1 - Changing value of JsonStore

I use ExtJS 4.1. Here's my model and store:
Ext.define('MyModel', {
extend: 'Ext.data.Model',
fields: ['status', 'data', 'data1', 'data2']
});
var store1 = Ext.create('Ext.data.JsonStore', {
model: 'MyModel',
proxy: {
type: 'ajax',
url : 'actionJsonServlet'
},
autoLoad: true
});
After loading the store by Ajax, I want to change value of first "status" (just for first row) of the JsonStore.
I tried lines below but it doesn't work (record is undefined at line 2):
var record = store1.getAt(0);
record.set("status", "Waiting");
I have this error:
Cannot call method 'set' of undefined
Most likely your issue is due to asynchronous nature of store load. Depending on how your code is written you maybe attempting to do store operations too early, before the store is loaded even though you have autoLoad turned on.
The best approach is to set up a load event listener on the store and perform your operation then.
Her is an example:
Ext.define('MyApp.store.Drafters', {
extend:'Ext.data.Store',
model:'MyApp.model.User',
autoLoad:true,
proxy:{
type:'ajax',
url:'user/drafters.json',
reader:{
type:'json',
root:'data'
}
},
listeners:{
load:function (store, recs) {
store.insert(0, {uid:'', name:''}); //adding empty record to enable deselection of assignment
}
}
});

Resources