I asked this question before the weekend and am still stuck despite following the advice. This time I will post all of the relevant code in the hope someone can help me get to the bottom of this.
Important note: the data store and model work perfectly when I statically enter the data into the data store using the data: [{ json data }] parameter of the store. But doing it using ajax proxy fails (even though I can see in chrome that test.php gets called and echoes out the json data (see test.php below).
This is my store:
new Ext.data.Store({
model: "SearchResult",
proxy: {
type: "ajax",
url : "test.php",
extraParams : 'test',
reader: {
type: "json",
}
},
});
I load it when a button is clicked on via a handler.
Here is what is echoed out in test.php:
<?php
echo "[{stock: 'Tommy', storePhone: '353535', year: '1984', make: 'Ferrari', trim: 'trim', miles: '12345', storename: 'branch name' }]";
?>
Been stuck on this for a while so any help much appreciated!
It;s not enough to echo a string that looks like your json ... you should use php methods to encode it ... for your example it will be
<?php
$data = array(array('stock'=> 'Tommy', 'storePhone'=> 353535, 'year'=> '1984', 'make'=> 'Ferrari', 'trim'=> 'trim', 'miles'=> '12345', 'storename'=> 'branch name' ));
echo json_encode($data);
?>
You need to provide a "success: true" property and put your data into a root property in your JSON response.
You should then add the root property to your reader's config.
{
"success": true,
"rows": [
{
"stock": "Tommy",
"storePhone": "353535",
"year": "1984",
"make": "Ferrari",
"trim": "trim",
"miles": "12345",
"storename": "branchname"
}
]
}
Your store:
new Ext.data.Store({
model: "SearchResult",
proxy: {
type: "ajax",
url : "test.php",
extraParams : 'test',
reader: {
type: "json",
root: 'rows'
}
},
});
This solves the issue
store.proxy.url = 'loader.php?user=' + var_here;
store.load();
Related
Possible Duplicate: How to set url and root dynamically in extjs
Hi there, I have a simple memoryStore. If I tried not to declare its proxy during the Ext.Define, I am unable to retrieve the proper data root later on, even if I do set the proxy. Am I doing something wrong?
Here's a test case:
var store = Ext.create('Ext.data.Store', {
storeId: 'JailNames',
autoLoad: true,
fields: [
{
name: 'name',
type: 'string'
},
],
data: {
data_regionI: [
{name: "Jail 1"},
{name: "Jail 2"},
{name: "Jail 3"},
],
data_regionII: [
{name: "Jail 4"},
{name: "Jail 5"},
{name: "Jail 6"},
],
},
proxy: {
type: 'memory',
reader: {
type: 'json',
root: 'data_regionI'
}
}
})
store.setProxy({
type: 'memory',
reader: {
type: 'json',
root: 'data_regionII'
}
} )
store.load();
store.getAt(0).raw //still returns Jail 1
Looking through store.getProxy().reader.root I get the data_regionII as a root. Why?
Thanks in advance
If I copy your code into a sencha fiddle of Version 4.1.1, it throws an Uncaught TypeError: Cannot read property 'raw' of undefined, which is what I expected, because the store shouldn't contain any records at all after the call to load().
There are many problems in your understanding what a store does and what a proxy does:
A normal store's load function will tell the proxy to fetch the data, tell the reader to make records from it, and load it into the data property of your store, overwriting(!) the data you have defined at initialization.
But a memory store's load function isn't intended to do anything at all, and isn't intended to be used at all.
A memory store isn't intended to hold more than one store content at the same time. (you can, however, store the unused contents in an unused(!) property of the store's JavaScript object).
A store, no matter which proxy, does not require autoLoad:true to load the content of data into the store - the content of data is automatically used as the default data of the store after initialization.
That said, it's still possible to achieve what you want with just a few lines of code. You don't even have to create all the functions I only made for readability:
var store = Ext.create('Ext.data.Store', {
storeId: 'JailNames',
fields: [
{
name: 'name',
type: 'string'
},
],
myData: { // custom property!
data_regionI: [
{name: "Jail 1"},
{name: "Jail 2"},
{name: "Jail 3"},
],
data_regionII: [
{name: "Jail 4"},
{name: "Jail 5"},
{name: "Jail 6"},
],
},
loadRegion1:function() {
this.loadRegion("data_regionI");
},
loadRegion2:function() {
this.loadRegion("data_regionII");
},
loadRegion:function(rootProperty) { // custom function for better readability
this.loadRawData(this.myData[rootProperty]); // load data without proxy, but with reader!
},
proxy: {
type: 'memory',
reader: {
type: 'json'
}
}
});
store.loadRegion1();
console.log(store.getAt(0).get("name")); //returns Jail 1
store.loadRegion2();
console.log(store.getAt(0).get("name")); //returns Jail 4
I keep on getting an internal server 500 error every time I attempt to load my store. I am currently trying to connect to our API endpoint that contains the data that I need. This is the error I'm getting every time (FYI, checking the 'Accept' Header, it seems it's empty. I am not sure how I can have an application/json there to correctly connect to it):
My Store is setup like this:
Ext.define('EcommBackoffice.store.TierCapacity', {
extend: 'Ext.data.Store',
model: 'EcommBackoffice.model.TierCapacityModel',
storeId: 'tier-capacity-id',
autoLoad: true,
sorters: [{
property: 'name',
direction: 'ASC'
}],
proxy: {
type: 'rest',
url: EcommBackoffice.Global.getAPIEndPoints().tier_capacity + '?siteCode=bgp88',
reader: {
type: 'json',
root: ''
},
listeners: {
exception: function(proxy, response, op) {
if (response.status === 403 || response.status === 401) return; /*skip this exception handler and check App exception handler*/
Ext.Msg.alert('ERROR', response.responseText + ' ' + response.statusText);
}
}
}
});
And my model like this:
Ext.define('EcommBackoffice.model.TierCapacityModel', {
extend: 'Ext.data.Model',
fields: [{
name: 'id'
}, {
name: 'paymentOption',
type: Ext.data.SortTypes.asUCString
}, {
name: 'tier',
type: Ext.data.SortTypes.asUCString
}, {
name: 'media',
type: Ext.data.SortTypes.asUCString
}, {
name: 'channels',
type: Ext.data.SortTypes.asUCString
}]
});
The API contains something like this:
[{
"name": "DEBIT",
"tiers": [{
"name": "Default",
"media": [{
"name": "OFFICE",
"channels": [{
"name": "CHANNEL-001",
"currentVolume": 0,
"maxVolume": 0,
"yesterdayVolume": 0
}]
}]
}]
}]
Moreover, I am a bit unfamiliar with setting up the models and stores. I am assuming that is where I'm missing something. Am I structuring the model correctly based on the API response? I tried reading the docs, but I still can't wrap my head around it.
Error Code 500 tells :
The server encountered an unexpected condition which prevented it from fulfilling the request.
As I can see that while processing code at your Java end , you are getting NullPointerException .
So when an unexpected exception occurs , Http 500 error code is thrown which is shown on your browser. This error code has nothing to be done from client side.
The error 500 is not an ExtJS issue. It's a backend issue. You should check what backend technologies you use and add these to the tags to get any help.
checking the 'Accept' Header, it seems it's empty. I am not sure how I can have an application/json there to correctly connect to it)
proxy: {
headers: {
Accept: 'application/json'
},
I am having problems loading an array of data from an external file into my store.
This is the file containing the data:
/data/contacts
[
["Lisa", "lisa#hotmail.com", "555-222-3333"],
["Bart", "bart#hotmail.com", "555-222-3333"],
["Homer", "homer#hotmail.com", "555-222-3333"],
["Marge", "marge#hotmail.com", "555-222-3333"]
]
This is my store:
Ext.define('MyApp.store.Contacts', {
extend: 'Ext.data.Store',
autoLoad: true,
alias: 'store.contacts',
model: 'MyApp.model.Contact',
proxy: {
type: 'ajax',
reader: {
type: 'array'
},
url: '../data/contacts'
}
});
This is my model:
Ext.define('MyApp.model.Contact', {
extend: 'Ext.data.Model',
alias: 'model.contact',
fields: [
{name: 'name', mapping: 0},
{name: 'email', mapping: 1},
{name: 'phone', mapping: 2},
]
});
And I am getting this error:
[E] Ext.JSON.decode(): You're trying to decode an invalid JSON String: [
["Lisa", "lisa#hotmail.com", "555-222-3333"],
["Bart", "bart#hotmail.com", "555-222-3333"],
["Homer", "homer#hotmail.com", "555-222-3333"],
["Marge", "marge#hotmail.com", "555-222-3333"]
]
Does anyone have any suggestions on what I am doing wrong, or what I should do? Preferably I do not want to change the format of the array in the data file.
Okay, I realized what I did wrong. It was a very stupid user error made by me.
In my data file: /data/contacts, what I really had was this:
[
["Lisa", "lisa#hotmail.com", "555-222-3333"],
["Bart", "bart#hotmail.com", "555-222-3333"],
["Homer", "homer#hotmail.com", "555-222-3333"],
["Marge", "marge#hotmail.com", "555-222-3333"]
]
// [
// {name: 'Lisa', email: 'lisa#simpsons.com', phone: '555-222-1212'},
// {name: 'Bart', email: 'bart#simpsons.com', phone: '555-333-2212'},
// {name: 'Homer', email: 'homer#simpsons.com', phone: '555-122-1212'},
// {name: 'Marge', email: 'marge#simpsons.com', phone: '555-123-1212'}
// ]
I was previously testing other options for my JSON format, and I was silly enough to think that I could place comment //'s in a normal file.
Removing the comments from the file will fix the error. The array defined in this file can now be loaded into the store.
The error message says that the received JSON is invalid. You can check the validity online at http://jsonlint.com. If it is invalid it is the first thing to fix.
Then, I see the combination of ajax proxy and array reader for the first time in many years with Ext and I'm not sure it is supported.
I am using Sencha Touch 2.3 and Extjs 4.2
Issue: Handle multiple root nodes JSON response(from single response) in multiple stores.
{
total:
[
{
exp_amount_tot: "71962.00",
income_amount_tot: "462129.00"
}
],
data:
[
{
id: "1",
userid: "2",
name: "Any",
notes: "",
},
]
}
I need to save the above response into two different stores.
1. dataStore rootproperty:"data"
2. summaryStore rootProperty: "total"
Please help me to fix this issue.
Store:
proxy: {
type: "ajax",
api: {
create: "http://localhost/api/getAccounts.php/create",
read: "http://localhost/api/getAccounts.php/getall",
update: "http://localhost/api/getAccounts.php/update",
},
reader: {
type: "json",
successProperty: 'success',
rootProperty: 'data',
messageProperty: 'message'
},
},
you can create 2 different stores with "data" & "total" root properties respectively and use the store.add() method to add the data to individual store.
I'm attempting to populate a grid with a data store that uses a proxy and a defined model and reader. Similar stores aren't having the same issue, but one is.
Model
Ext.define('DrillDescriptionGridModel', {
extend: 'Ext.data.Model',
fields: [
{name: 'sentTime', type: 'string'},
{name: 'sDescription', type: 'string'},
{name: 'receivedTime', type: 'string'},
{name: 'seconds', type: 'number'},
{name: 'formatted', type: 'string'},
{name: 'alertPhone', type: 'string'},
{name: 'alertEmail', type: 'string'}
]
});
Reader
var DrillDescriptionReader = Ext.create('Ext.data.JsonReader', {
type: 'json',
model: 'DrillDescriptionGridModel',
root: 'data'
});
Store
DrillDescriptionStore = Ext.create('Ext.data.Store', {
model: 'DrillDescriptionGridModel',
autoLoad: false,
proxy: {
type: 'ajax',
url: '/inc/ajax/Monitors.php',
actionMethods: 'POST',
reader: DrillDescriptionReader
},
listeners: {
load: function() {
console.log(this.getAt(0));
DrillDescriptionPanel.show();
}
}
});
The proxy returns a json string
{"data":[{"sDescription":"Status Normal","sentTime":"12:00:00 am","receivedTime":"12:00:01 am","seconds":"2","formatted":"2 seconds","alertPhone":"","alertEmail":""}, [...]
The console.log in the load listener displays
Ext.Class.c.m
data: Object
alertEmail: ""
alertPhone: ""
formatted: "2 seconds"
receivedTime: "12:00:01 am"
seconds: 2
sentTime: "12:00:00 am"
__proto__: Object
[...]
raw: Object
alertEmail: ""
alertPhone: ""
formatted: "2 seconds"
receivedTime: "12:00:01 am"
sDescription: "Status Normal"
seconds: 2
sentTime: "12:00:00 am"
__proto__: Object
[...]
Anyone know why the sDescription field would be mapped in the raw object, but not the data object, or spot an error in the code? Any help would be greatly appreciated. Thanks.
if you are using extjs 4 as you tagged it .. then the json reader is not defined properly
The class should be Ext.data.reader.Json
So I've figured this one out, and unfortunately it had NOTHING to do with the code that I posted. Thanks for all the help Rocky.
I had another store a little further down in the code that was trying to utilize the same reader... which seems like it shouldn't have been a problem at all, but apparently was. Modified the reader in the store below and code worked right away.
Thanks again for the help Rocky, and thanks for taking a look nscrob.