Ext.define('Olvldefnition', {
extend: 'Ext.data.Model',
fields: [
{
name : 'defKey'
},
{
name : 'defStructKey',
type : 'int'
},
{
name : 'defLevelNo',
type : 'int'
}, {
name : 'defLevelDiscP',
type : 'string'
}, {
name : 'defLevelDiscS',
type : 'string'
}, {
name : 'defSeqNo',
type : 'int'
}
]
});
var storeDef = Ext.create('Ext.data.Store', {
id: 'storeDef',
model: 'Olvldefnition',
proxy: {
type: 'jsonp',
api : {
read : 'definition/view.action'
},
reader: {
type: 'json',
root: 'data',
idProperty: 'defKey',
successProperty : 'success',
messageProperty : 'message'
}
}
});
This is my code it gets a JSON data like as follows.
{
"total":3,
"data":[
{"modifiers":{"status":"A","effstartdate":"2012-04-02 00:00:00.0","effenddate":"2012-04-26 00:00:00.0","updateUser":"ARCSADMIN","updateDate":"2012-04-16 16:04:29.162","remarks":null,"entryDate":"2012-04-07 19:34:28.923","entryUser":"ARCSADMIN"},
"defKey":105,
"defSeqNo":2,
"defStructKey":73,
"defLevelNo":1,
"defLevelDiscP":"Branch",
"defLevelDiscS":"Branch",
"olvstructure":{"modifiers":{"status":"A","effstartdate":"2012-04-06 00:00:00.0","effenddate":"2012-04-27 00:00:00.0","updateUser":"ARCSADMIN","updateDate":"2012-04-06 20:03:43.817","remarks":"Remark","entryDate":"2012-04-06 20:03:37.252","entryUser":"ARCSADMIN"},"olsKey":73,"olsSeqNo":1,"olsLevel":3,"olsSeperator":"-"}},{"modifiers":{"status":"A","effstartdate":"2012-04-02 00:00:00.0","effenddate":"2012-05-23 00:00:00.0","updateUser":"ARCSADMIN","updateDate":"2012-04-30 12:22:12.899","remarks":null,"entryDate":"2012-04-07 19:33:58.405","entryUser":"ARCSADMIN"},"defKey":104,"defSeqNo":1,"defStructKey":73,"defLevelNo":2,"defLevelDiscP":"Dept1","defLevelDiscS":"Dept1","olvstructure":{"modifiers":{"status":"A","effstartdate":"2012-04-06 00:00:00.0","effenddate":"2012-04-27 00:00:00.0","updateUser":"ARCSADMIN","updateDate":"2012-04-06 20:03:43.817","remarks":"Remark","entryDate":"2012-04-06 20:03:37.252","entryUser":"ARCSADMIN"},"olsKey":73,"olsSeqNo":1,"olsLevel":3,"olsSeperator":"-"}},{"modifiers":{"status":"I","effstartdate":null,"effenddate":null,"updateUser":null,"updateDate":null,"remarks":null,"entryDate":"2012-04-19 13:52:04.676","entryUser":"ARCSADMIN"},"defKey":118,"defSeqNo":3,"defStructKey":73,"defLevelNo":3,"defLevelDiscP":"sdasda","defLevelDiscS":"dsd","olvstructure":{"modifiers":{"status":"A","effstartdate":"2012-04-06 00:00:00.0","effenddate":"2012-04-27 00:00:00.0","updateUser":"ARCSADMIN","updateDate":"2012-04-06 20:03:43.817","remarks":"Remark","entryDate":"2012-04-06 20:03:37.252","entryUser":"ARCSADMIN"},"olsKey":73,"olsSeqNo":1,"olsLevel":3,"olsSeperator":"-"}}],"message":"view","success":true}
but it shows an error like this
SCRIPT1004: Expected ';'
view.action?_dc=1337079455042&page=1&start=0&limit=25&callback=Ext.data.JsonP.callback1, line 1 character 9
maybe problem is proxy type: 'jsonp'?
because in your example I only see json not jsonp and your api url is on same server so why not use json instead of jsonp?
Example:
var storeDef = Ext.create('Ext.data.Store', {
id: 'storeDef',
model: 'Olvldefnition',
proxy: {
type: 'ajax',
api : {
read : 'definition/view.action'
},
reader: {
type: 'json',
root: 'data',
idProperty: 'defKey',
successProperty : 'success',
messageProperty : 'message'
}
}
});
I think you've got simple syntax error somewhere. It has nothing to do with parsing JSON data.
Related
I have a nested JSON and I cannot seem to extract the nested array. I am not sure if I set up my store and models correctly. It seems right. As you can see at the bottom of this post, when I try to get the nested part out, it doesn't work.
JSON data:
{
"data":{
"name":"John",
"age":"23",
"gender":"Male",
"purchase":[
{
"item":"shirt",
"qty":1,
"price":10
},
{
"item":"hat",
"qty":2,
"price":25
},
{
"item":"pants",
"qty":1,
"price":15
}
]
},
"success":true,
"errorMsg":""
}
Store:
Ext.define('Pvm.store.MyStore', {
extend: 'Pvm.store.PvmBaseStore',
requires: [
'Pvm.model.Customer'
],
model: 'Pvm.model.Customer',
autoLoad: false
});
Customer Model:
Ext.define('Pvm.model.Customer', {
extend: 'Ext.data.Model',
requires: [
'Ext.data.Field',
'Ext.data.proxy.Ajax',
'Ext.data.reader.Json'
],
fields: [{
name: 'name',
type: 'auto'
}, {
name: 'age',
type: 'auto'
}, {
name: 'gender',
type: 'auto'
}],
associations: {
type: 'hasMany',
model: 'Pvm.model.Purchase',
name: 'purchase',
associationKey: 'purchase'
},
proxy: {
type: 'ajax',
actionMethods: {
create: 'POST',
read: 'POST', // changed read's method to POST (from GET) so we get the parameters as form data (not in URL, for security reasons)
update: 'POST',
destroy: 'POST'
},
url: '/pvmsvr/uiapi?cmd=readXml',
reader: {
type: 'json',
rootProperty: 'data'
}
}
});
Purchase Model:
Ext.define('Pvm.model.Purchase', {
extend: 'Ext.data.Model',
requires: [
'Ext.data.Field',
'Ext.data.proxy.Ajax',
'Ext.data.reader.Json'
],
fields: [{
name: 'item',
type: 'auto'
}, {
name: 'qty',
type: 'auto'
}, {
name: 'price',
type: 'auto'
}],
associations: {
type: 'belongsTo',
model: 'Pvm.model.Customer'
}
});
Controller Code:
onStoreLoaded: function(store, records, successful, eOpts) {
console.log(store); // works
console.log(records); // works
console.log(records[0].get('name')); // works
console.log(records[0].purchase(); // doesn't work; returns 'undefined'
}
I am idiot. I needed the child model's class name in the parent's requires...
I have a simple model, let's say:
Ext.define('UserModel', {
extend: 'Ext.data.Model',
fields: [
{name: 'firstname', type: 'string'},
{name: 'lastname', type: 'string'}
]
});
And a json file that looks like this:
{
"DatabaseInJSON": {
"Users": [
{
"KeyFirstName": "John",
"KeyLastName": "Doe"
},{
"KeyFirstName": "James",
"KeyLastName": "Howlett"
}
],
"OtherStuffWeDontCareAbout": [
...
]
}
}
My question is:
If I create a store like this, how can i map the attribute "firstname" from my model to "KeyFirstName" from my json ?
Ext.define('my.custom.Store', {
extend: 'Ext.data.Store',
model: 'UserModel',
proxy: {
type: 'ajax',
url: 'path/to/my/file.json',
reader: {
type: 'json',
rootProperty: 'DatabaseInJSON'
}
}
});
You need to either employ mapping or a convert function
Have a look at the demo here which demonstrates both in action.
For the sake of the demo I turned your store into a memory proxy store and you are I presume also accessing your rootProperty wrong as it should be rootProperty: 'DatabaseInJSON.Users'
Code:
Ext.application({
name: 'Fiddle',
launch: function() {
myData = {
"DatabaseInJSON": {
"Users": [{
"KeyFirstName": "John",
"KeyLastName": "Doe"
}, {
"KeyFirstName": "James",
"KeyLastName": "Howlett"
}],
"OtherStuffWeDontCareAbout": {}
}
};
Ext.define('UserModel', {
extend: 'Ext.data.Model',
fields: [{
name: 'firstname',
mapping: 'KeyFirstName',
type: 'string'
}, {
name: 'lastname',
convert: function(v, record) {
return record.data.KeyLastName;
},
type: 'string'
}]
});
Ext.define('my.custom.Store', {
extend: 'Ext.data.Store',
model: 'UserModel',
proxy: {
type: 'memory',
reader: {
type: 'json',
rootProperty: 'DatabaseInJSON.Users'
}
}
});
myStore = Ext.create('my.custom.Store', {
data: myData
});
console.log(myStore.getRange());
}
});
Generally your Json properties should match the same names of your fields so that the reader can read them properly, to map 'KeyFirstName' to 'firstname' I think your best option would be to create a mapping in the field definition on the model.
This would apply this globally for all requests and I believe it would reverse the mapping when it come to saving the data.
To use the mapping in your case, you would need something like:
Ext.define('UserModel', {
extend: 'Ext.data.Model',
fields: [
{name: 'firstname', type: 'string', mapping: function(data) { return data.KeyFirstName; } },
{name: 'lastname', type: 'string', mapping: function(data) { return data.KeyLastName; } }
]
});
Other than changing the format of the JSON data, the only other way I can think of would be to override the read or getResponseData method of the JsonReader
var RouteSeqStore = Ext.create('Ext.data.JsonStore', {
model: 'RouteSeqModel',
storeId: 'RouteSeqStore',
autoLoad: false,
sorters: [{
property: 'Route_Seq',
direction: 'ASC'
}],
proxy: {
type: 'ajax',
url: 'route/get-routeseq.php',
api: {
destroy: 'route/delete-routeseq.php',
create: 'route/insert-routeseq.php',
//read: 'http://visual04/ModuleGestion/php/Pays.php?action=read',
update: 'route/update-routeseq.php',
},
actionMethods: 'POST',
baseParams: {
_id : 0,
},
reader: {
type: 'json',
idProperty: '_id'
},
writer: {
type: 'json',
id: '_id'
}
}
});
this is my EXTJS post json code, when posting 1 object, extjs wont have square bracket
{"_id":11,"Route_Seq":1,"Location_Name":"B.STATION","Route_LocationID":"1","Route_ID":"2","id":null}
when multi array json result will having the [ ] bracket, how to let JSON post with square bracket even 1 object only
what i expect the result is :
[{"_id":11,"Route_Seq":1,"Location_Name":"B.STATION","Route_LocationID":"1","Route_ID":"2","id":null}]
any solution?
Simple: just set the allowSingle config on your writer to be "false". This will force the writer to send model instances as an array ALWAYS, regardless of the number being persisted in any given request. Be sure to check out the docs for this.
Here is my model:
Ext.define('DynTabBar.model.Menu',{
extend: 'Ext.data.Model',
config: {
fields: [{
name: 'Title', type: 'string'
}, {
name: 'Level', type: 'int'
}, {
name: 'Parent', type: 'string'
}]
}
});
Store:
Ext.define('DynTabBar.store.MenuItems', {
extend: 'Ext.data.Store',
xtype: 'menustore',
config: {
storeId: 'menuStore',
model: 'DynTabBar.model.Menu',
autoLoad: false,
proxy: {
type: 'ajax',
id: 'menuproxy',
url: 'http://localhost:50567/api/Menu',
reader: {
type: 'json'
}
}
}
});
In controller I have smth like that:
var mainStore = Ext.getStore('menuStore');
mainStore.load();
My json respone from server is:
[{"Title":"home","Level":1,"Parent":""},{"Title":"home1","Level":2,"Parent":"home"},{"Title":"home2","Level":2,"Parent":"home"},{"Title":"info","Level":1,"Parent":""},{"Title":"info1","Level":2,"Parent":"info"},{"Title":"info2","Level":2,"Parent":"info"},{"Title":"info3","Level":2,"Parent":"info"}]
But store data is empty. What I'm missing or doing wrong?
I fixed the problem, the problem is that I'm not waiting while store data is loaded and do something while store isn't loaded. I fix that doing what I need in store loaded callback.
mainStore.load(callback);
I try to load fields config to json store via metadata. The json is:
{
"rows":[
{
"datev":"02.01.2011",
"w1":"100",
"w2":"200"
},
{
"datev":"02.01.2011",
"w1":"300",
"w2":"50"
},
{
"datev":"03.01.2011",
"w1":"10",
"w2":"450"
}
],
"metaData":{
"fields":[
{
"name":"datev"
}
],
"root":"rows"
}
}
and my store is:
var test = new Ext.data.JsonStore({
url: 'test.php'
});
test.load();
The metadata doesn't load. What is wrong with the code?
I think you need to add a JSON reader
var reader = new Ext.data.JsonReader({
fields: []
});
var store = new Ext.data.Store({
nocache : true,
reader : reader,
autoLoad : true,
remoteSort : true,
proxy : new Ext.data.HttpProxy({
url : '/getjson?queryable=featureType&featureType=Electronic%20Device',
method : 'GET'
})
or maybe
var store = new Ext.data.JsonStore({
url: 'somewhere',
fields: []
});
You are not specifying the Id Property, and neither the Root property. this is my code i hope this help
var EStore = new Ext.data.JsonStore
({
api: { read: 'getAssignedJobs',
create: 'createAssignedJobs',
update: 'updateAssignedJobs'
},
root: 'jobData',
idProperty: 'Id',
autoSave: true,
batch: false,
successProperty: 'success',
writer: new Ext.data.JsonWriter({ encode: true, writeAllFields: true }),
fields: [
{ name: 'Id', type: 'int' },
{ name: 'ResourceId', mapping: 'fitter_id', type: 'int' },
{ name: 'StartDate', type: 'date', format: 'd/m/Y G:i' },
{ name: 'EndDate', type: 'date', format: 'd/m/Y G:i' },
{ name: 'status', type: 'int' },
{ name: 'job_id', type: 'int' }
]
});