I am learning ExtJS framework, for experiments I use on front-end side ExtJS and on back-end side JavaEE Spring framework (it is configured like as REST service). So, I start my front-end part on localhost:1841 and back-end part on localhost:8080. Question is:
How I can say to ExtJS.Store that requests need to send to
localhost:8080/** instead of localhost:1841/**?
Sorry for my English!
In ExtJS have singleton you can use this class.
Singleton pattern is a design pattern which restricts instantiation of a class to only one object. This is useful when exactly one object is needed across the system. The Singleton pattern provides a single point of access to a particular instance. Implementation of a singleton pattern must satisfy the single instance and global access principles.
For this "How I can say to ExtJS.Store that requests need to send to localhost:8080/** instead of localhost:1841/**?"
You can use in your app like below code :
Firstly create singletone class
/*
* Create singletone class in your application
* This class you can access in your application anywhere you want within the app.
* Usage:
* commonUtility.getServerUrl();// whatever property you have defined inside of config you can access like this
*/
Ext.define('APPNAME.utils.SingleToneClassName', {
alternateClassName: 'commonUtility',
singleton: true,
config: {
/*
* you can put local or live also or whatever you want.
* for local it will be ip address like this {'http://192.168.30.83:8080/'}
* for live is will be live tomcate host url {http://example.com/}
*/
serverURL: 'http://192.168.30.83:8080/'
},
constructor: function(config) {
var me = this;
me.initConfig(config);
},
});
Create/Define your store
//Your store
Ext.define('APPNAME.store.StoreName', {
extend: 'Ext.data.Store',
fields: ['your fields here'],
storeId: 'storeIdHere',
alias: "store.storeAliasHere",
proxy: {
type: 'ajax',
url: commonUtility.getServerUrl() + 'your Server Method name here', //Based on your server URL acceptance
withCredentials: true,
reader: {
type: 'json',
rootProperty: 'data',
keepRawData: true
}
},
autoLoad: true, //If you need auto load then put true otherwise false
listeners: {
beforeload: function(store, operation, options) {
//If you have token based authenthication then you need to put like below
store.getProxy().setHeaders({
"x-auth-token": 'your token here'
});
//If you have need to pass some parameter in API method then you can pass like below
store.getProxy().extraParams.your_parameter_name = 'value';
}
},
});
//If you want to load your store on some event or any other functions
//then
Ext.getStore('your_storeId_herer').load({
url: commonUtility.getServerUrl() + 'your Server Method name here', //Based on your server URL acceptance
params: {
//If you have need to pass some params in server side then
//you put here like
name: 'value'
}
});
I hope this will help you. for more details you can refer ExtJS6.x Docs
Related
Unanswered: store ajax serving multi read requests (best practice question)
Hi, im trying to understand the proper design concept of Store proxies using the Ajax api config and have a question on how this should be done.
suppose i have a store which has a server proxy using the Ext.data.proxy.Ajax class and i have an api with the following:
proxy: {
type: 'ajax',
api: {
read: 'some/something/list.json',
create: 'some/something/insert.json',
update: 'some/something/update.json',
destroy: 'some/something/destroy.json'
}
}
now suppose my read is triggered by a search button and when i have a blank text box and click search it makes a request through the read api to retrieve the list.json. but i want to have another read as part of the same store / api to read individual records say something like this:
read: 'some/something/<field_value>.json'
my proxy read is already assigned to the list.json but i want to allow the same store proxy to be able to read from individual record searches also. granted that i cant have two read statements in my proxy. how would I go about writing this?
help me understand? maybe my server controller has to be able to determine by keyword #PathVariable if path is list i.e .json then call the list db query otherwise if .json then run the individual search query through the db.?? and if so what would be the read: url?
whats the best way to design and build this?
Thanks in advance
If you want to read single record instead of defining proxy in store you should do it in Model and then call load on model itself something like below.
Ext.define('app.model.User', {
{
fields: [
{ name: 'LoginUserId', type: 'string' },
{ name: 'FirstName', type: 'string' },
{ name: 'LastName', type: 'string' }],
proxy:
{
type: 'rest',
url: '/User',
reader:
{
type: 'json',
root: ''
}
}
});
var user = Ext.ModelMgr.getModel('app.model.User');
user.load(123, {
success: function(userObj) {
}
});
Using Ext-JS 4.1 with Spring 3.1 Controllers.
I am trying to retrieve an object from my Spring Controller using the load() method. I read a string from a text field and send that into the load method. The string field will contain a fully qualified server name such as "company.server.com". What's happening is that the value within the Spring controllers is "company.server", in other words it drops the ".com". It I put in an additional period at the end such as "company.server.com." then it comes in properly as "company.server.com". There seems to be some sort of tokenizing going on. I used commas (,) just to see what would happen. Using commas the string came in as expected. For some reason periods (.) is causing the issue.
Here is the Model:
Ext.define('AB.model.Server', {
extend: 'Ext.data.Model',
fields: [
{name:'serverName', type:'String'},
{name:'memory', type:'String'},
{name:'cpus', type:'int'}
],
proxy {
type: 'rest',
url: '/web/user/'
}
});
Here is a snippet from the form which makes the load() call:
Ext.define('AB.view.Form', {
extend: 'Ext.form.Panel',
....
,{
xtype: 'button',
text: 'Retrieve Information',
handler: function() {
Ext.ModelManager.getModel('AB.model.User').load(Ext.getCmp('serverName').getValue(), {
success: function(user) {
alert("Success");
}
....
}
Using Firebug I see this as the URL being called:
http://myServer/web/user/company.server.com?_dc=13461612333647?id=company.server.com
So the URL has the correct server name but on the Spring Controller side the value of my parameter is "company.server".
When I directly put the following URL directly in my web browser:
http://myServer/web/user/company.server.com/
it works properly with the parameter in the Spring Controller being "company.server.com".
Is this an EXT JS problem? Is a problem with EXT JS to Spring? I don't think it is a Spring issue alone since the URL directly in the browser works properly.
UPDATE:
When I put the following URL directly in my web browser:
http://myServer/web/user/company.server.com
it behaves the same as the EXT JS Rest call. Notice there is no ending slash (/). So maybe this is a Spring issue? Or maybe a web.xml issue?
I have 2 APIs which I want to use simultaneously, API1 and API2.
API2 deliver news feeds to API1, while API1 handles all the content in a list form. This means that if any list in API1 is clicked on, it will it will retrieve the the news feeds from API2 using an ID that has been defined.
Who can help me out here? I am stuck. The screenshot of my code is here: http://i1159.photobucket.com/albums/p637/Apulo_Cosmas/2API.jpg
Thanks so much.
Per the Sencha documentation here (under Digging In): http://docs.sencha.com/touch/2-0/#!/guide/first_app
You need to add a listener and a detail panel to your configuration, using this code (reference to contentId is not needed, you just need to pull the description property -- which contains the content -- from the original feed):
detailCard: {
xtype: 'panel',
scrollable: true,
styleHtmlContent: true
},
listeners: {
itemtap: function(nestedList, list, index, element, post) {
this.getDetailCard().setHtml(post.get('description'));
}
}
It might be easier if you manually listen to your list (which is fetched from API 1) itemtap event.
In your controller, there's should be something like:
refs: {
bloglist: 'blog list'
},
control: {
bloglist: {
itemtap: 'fetchAPI2'
}
},
fetchAPI2: function (list,index,target,record){
id = record.get('contentId'); //this is your id for using API2.
Ext.data.JsonP.request({
scope: this,
url: API2_URL,
callbackKey: 'callback',
params: {your params for API2 here},
callback: function(success,result) {
// whatever you want to do here
}
});
}
I am using this to get the result from server
controller.allVisitStore = new Ext.data.Store({
model: 'allVisit',
autoLoad : true,
proxy: {
type: 'ajax',
id: 'allvisit_app_localstore',
url: '/RadMobApp/api',
extraParams:{
action:'query',
queryName:'GET_ALL_VISIT',
authToken: localStorage.getItem("auth_token"),
patTicketId: localStorage.getItem("patientId"),
retFormat:'XML',
keyValuePair:'yes'
},
// the return will be XML, so lets set up a reader
reader: new Ext.data.XmlReader({
// records will have an "T4" tag
record: 'data'
})
}
});
but i am not getting any thing.But i formed this url in browser and checked this i got the correct result. now here i want to check is there any problem in the url formation.How to check the url formation with extra parameter which is pass through ajax. I have checked in Inspect element-> network -> api there is no any api request found there.Is anything wrong in my code. Thanks in advance...
Use Firebug for Firefox or Chrome's developer tools to see what's going on when that store attempts to load itself. My hunch is that your url is incorrect and should be url: '/api' because RadMobApp is probably your app root.
I'm having a problem with loading a Sencha store. My store declaration is like this:
Ext.regModel('Car',{
idProperty: 'id',
fields: [
//'id', 'company', 'driver', 'carType','xCoordinate','yCoordinate'
{name: 'id', type: 'int'},
{name:'company', type:'string'} ,
{name:'driver', type:'string'},
{name:'carType', type:'string'},
{name:'xCoordinate', type:'int'},
{name:'yCoordinate', type:'int'}
]
});
var strr= new Ext.regStore({
id:'carStore',
model:'Car', //configuration option is the cars
proxy: {
type: 'ajax',
url: 'http://localhost:8080/A/carStore.html&callback=?',
reader: {
type: 'json',
root: 'carss'
},
},
autoLoad: true
});
And I'm keeping the store in a list:
CarManagementSystem.views.carList = new Ext.List({
id: 'carList',
store: 'carStore',
onItemDisclosure: function (record) {
var selectedCar = record;
CarManagementSystem.views.addNewCar.load(selectedCar);
CarManagementSystem.views.viewport.setActiveItem('addNewCar', { type: 'slide', direction: 'left' });
},
itemTpl: '<div class="list-item-id">{id}</div>' +'<div class="list-item-driver">{driver}</div>'
});
However, when I try to load the list with my JSON file, I get this error:
XMLHttpRequest cannot load http://localhost:8080/A/carStore.html&callback=?&_dc=1311751412006&limit=25. Origin null is not allowed by Access-Control-Allow-Origin.
sencha-touch-debug.js:7212
Uncaught TypeError: Cannot read property 'length' of undefined
I'm keeping my JSON file in an html format and it is kept in the server. I'd appreciate any help.
Thanks!
You are encountering the classic CORS issue.
It's a browser security, called web-security. If you are in dev mode, you can run Chrome and disabling this flag. But if you run your app in production on a browser, you will need to bypass this ajax specification restriction.
To bypass it, you can use proxys (such as creating a back-end script on the same domain will load for you the resource) or you can use JSON-P. For this you will need to change your store to a script tag BUT ALSO you will need the server to be able to detect a callback param and send it to you as a JS function automatically executed when inserted in the DOM.
I will add that you won't have this issue when running in Phonegap because PhoneGap is not running a web server but serves files with the file:// protocol.
If you want to learn more, and I recommand as it is a common pb when developing mobile web apps, you should learn what JSON-P is, what CORS is, and how it works.
You can't hit a port number (8080) when making an AJAX request. If you end up needing a cross-domain request change your proxy type to 'scripttag'.
You should change the proxy type from 'ajax' to 'type: 'scripttag'