Using a proxy in a store is not working? - json

hi i create a store connect to contacts.json is not working ,here is my store
Ext.define('senchatest.store.List', {
extend: 'Ext.data.Store',
requires: ['Ext.data.proxy.Ajax'],
alias: 'store.List',
config: {
model: 'senchatest.model.Contact',
proxy: {
type: 'ajax',
url: 'contacts.json',
reader: {
type: 'json'
}
}
}
});
and this is my modal
Ext.define('senchatest.model.Contact', {
extend: 'Ext.data.Model',
config: {
fields: ['firstName', 'lastName']
}
});
and it is my json file
[
{ "firstName": "Tommy", "lastName": "Maintz" },
{ "firstName": "Ed", "lastName": "Spencer" },
{ "firstName": "Jamie", "lastName": "Avins" },
{ "firstName": "Aaron", "lastName": "Conran" },
{ "firstName": "Dave", "lastName": "Kaneda" },
{ "firstName": "Michael", "lastName": "Mullany" },
{ "firstName": "Abraham", "lastName": "Elias" },
{ "firstName": "Jay", "lastName": "Robinson" },
{ "firstName": "Zed", "lastName": "Zacharias "}
]
what is wrong its not display a data
instead of proxy directly use data it work
eg:
Ext.define('senchatest.store.List', {
extend: 'Ext.data.Store',
requires: ['Ext.data.proxy.Ajax'],
alias: 'store.List',
config: {
model: 'senchatest.model.Contact',
data :[
{ "firstName": "Tommy", "lastName": "Maintz" },
{ "firstName": "Ed", "lastName": "Spencer" },
{ "firstName": "Jamie", "lastName": "Avins" },
{ "firstName": "Aaron", "lastName": "Conran" },
{ "firstName": "Dave", "lastName": "Kaneda" },
{ "firstName": "Michael", "lastName": "Mullany" },
{ "firstName": "Abraham", "lastName": "Elias" },
{ "firstName": "Jay", "lastName": "Robinson" },
{ "firstName": "Zed", "lastName": "Zacharias "}
]
}
});
This code is working but proxy code is not working what is a issue

Take a look at http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.reader.Reader, it has a perfect example how you can do what you desire
Store
Ext.define('senchatest.store.List', {
extend: 'Ext.data.Store',
alias: 'store.List',
// use the model #Oğuz Çelikdemir suggested
model: 'senchatest.model.Contact',
proxy: {
type: 'ajax',
url : 'contacts.json',
reader: {
type: 'json',
root: 'contacts'
}
},
});
contacts.json
{
"success": true,
"contacts": [
{ "firstName": "Tommy", "lastName": "Maintz" },
{ "firstName": "Ed", "lastName": "Spencer" },
{ "firstName": "Jamie", "lastName": "Avins" },
{ "firstName": "Aaron", "lastName": "Conran" },
{ "firstName": "Dave", "lastName": "Kaneda" },
{ "firstName": "Michael", "lastName": "Mullany" },
{ "firstName": "Abraham", "lastName": "Elias" },
{ "firstName": "Jay", "lastName": "Robinson" },
{ "firstName": "Zed", "lastName": "Zacharias "}
]
}

Modify the model as below ( config property isn't necessary ) :
Ext.define('senchatest.model.Contact', {
extend: 'Ext.data.Model',
fields: [
{name: 'firstName', type: 'string'},
{name: 'lastName', type: 'string'}
]
});
EDIT :
I create a Fiddle on Sencha, check it out.
JSON Sample

you may use
autoLoad: true,
proxy: {
type: 'ajax',
url : 'contacts.json'
}

Related

How to use GJSON to convert array of objects to new array of objects with different structure/keys

I am trying to use GJSON package to read a JSON payload(from some APIs) and convert it to a structure that I desire.
Input:
{
"programmers": [
{
"firstName": "Janet",
"lastName": "McLaughlin"
},
{
"firstName": "Elliotte",
"lastName": "Hunter"
},
{
"firstName": "Jason",
"lastName": "Harold"
}
]
}
A generic config so that I can query as many endpoint with different keys without changing code.
[
{
"url": "some-magical-api",
"gjson_syntax": "{new_key:!\"mage\",nested_1:{programmers.#.firstName},nested_2:{programmers.#.lastName}}"
}
// other magical APIs
]
Current output:
{
"new_key": "mage",
"nested_1": {
"firstName": [
"Janet",
"Elliotte",
"Jason"
]
},
"nested_2": {
"lastName": [
"McLaughlin",
"Hunter",
"Harold"
]
}
}
Desired output:
[
{
"new_key": "mage",
"nested_1": {
"firstName": "Janet"
},
"nested_2": {
"lastName": "McLaughlin"
}
},
{
"new_key": "mage",
"nested_1": {
"firstName": "Elliotte"
},
"nested_2": {
"lastName": "Hunter"
}
},
{
"new_key": "mage",
"nested_1": {
"firstName": "Jason"
},
"nested_2": {
"lastName": "Harold"
}
}
]
What would be the GJSON syntax to achieve the desired output?
Should I use some other approach/library?

How to use JasperReports json metadata exporter to produce an array of objects that contain objects

I have data as simple as below is a jasper report:
firstName
lastName
john
carter
mary
ann
jack
thomas
and would like to export it to json. I'm trying to use Json metadata as described here.
I'm able to get the output below:
[
{
"firstName": "john",
"lastName": "carter"
},
{
"firstName": "mary",
"lastName": "ann"
},
{
"firstName": "jack",
"lastName": "thomas"
}
]
by using the schema below:
{
_type: 'array',
_children: {
_type: 'object',
firstName: 'value',
lastName: 'value'
}
}
and these properties in the text fields:
<property name="net.sf.jasperreports.export.json.path" value="firstName"/>
<property name="net.sf.jasperreports.export.json.path" value="lastName"/>
The problem arises when I try to generate a more complicated (but not that much) json. As below:
[
{
"person": {
"firstName": "john",
"lastName": "carter"
}
},
{
"person": {
"firstName": "mary",
"lastName": "ann"
}
},
{
"person": {
"firstName": "jack",
"lastName": "thomas"
}
}
]
The schema changes of course to this:
{
_type: 'array',
_children: {
_type: 'object',
person: {
_type: 'object',
firstName: 'value',
lastName: 'value'
}
}
}
I changed the path in the text fields to this:
<property name="net.sf.jasperreports.export.json.path" value="person.firstName"/>
<property name="net.sf.jasperreports.export.json.path" value="person.lastName"/>
What I get is this malformed json:
[
{
"person": {
"firstName": "john",
"lastName": "carter"
},
{
"firstName": "mary",
"lastName": "ann"
},
{
"firstName": "jack",
"lastName": "thomas"
}
}
]
Do you guys have any idea what I'm doing wrong? There is so little resources on this Json metadata exporter, even from JasperReports. Sometimes I think it's a half baked obscure feature nobody uses or cares about.

Can i covert this in a CSV format where all the element gonna be under one row, without repeating the doc attribute?

[
{
"doc": "ghgagsa",
"element": [
{
"firstName": "John",
"lastName": "Doe"
},
{
"firstName": "Anna",
"lastName": "Smith"
},
{
"firstName": "Peter",
"lastName": "Jones"
}
]
},
{
"doc": "ghgagsaaa",
"element": [
{
"firstName": "Joaahn",
"lastName": "Doae"
},
{
"firstName": "Anana",
"lastName": "Smiath"
},
{
"firstName": "Petaaer",
"lastName": "Jonaes"
}
]
}
]
Using jq, it's .[].element[]|[.firstName,.lastName]|#csv
Here an example https://jqplay.org/s/XmlRS6Yh9v

JSON source to datatables

I have this code:
testdata = [{
"hasresults": true,
"resultscount": 5,
"dob": null,
"chart": {
"rows": [
{
"chart": "BAR000",
"firstname": "RUSSELL",
"lastname": "BARON"
},
{
"chart": "BAR001",
"firstname": "BRUSELL",
"lastname": "BARON"
},
{
"chart": "BAR002",
"firstname": "GARY",
"lastname": "BARON"
}
]
}
}];
$('#test').dataTable({
"aaData": testdata,
"aoColumns": [{
"mDataProp": "chart"
}, {
"mDataProp": "firstname"
}, {
"mDataProp": "lastname"
}]
});
Can someone help me why is this not working? It seems that if I removed the following, it will work:
"hasresults": true,
"resultscount": 5,
"dob": null,
"chart": {
Not working fiddle
Working fiddle
You just need to address testdata the correct way. testdata is an array holding an object which have another object, chart, holding an array, rows.
$('#test').dataTable({
"aaData": testdata[0].chart.rows, //<------
"aoColumns": [{
"mDataProp": "chart"
}, {
"mDataProp": "firstname"
}, {
"mDataProp": "lastname"
}]
});
Your code working here -> http://jsfiddle.net/j1fvL96e/

How can I convert json into an extjs model?

I make the following json client side:
{
"employees": [
{
"firstName": "John",
"lastName": "Doe",
"computers": [
{
"name": "comp1"
},
{
"name": "comp2"
}
]
},
{
"firstName": "Anna",
"lastName": "Smith",
"computers": [
{
"name": "comp1"
},
{
"name": "comp2"
}
]
},
{
"firstName": "Peter",
"lastName": "Jones",
"computers": [
{
"name": "comp1"
},
{
"name": "comp2"
}
]
}
]
}
And I would like to stick it into this model:
Ext.define('Employees',{
extend: 'Ext.data.Model',
hasMany: [{
model: 'Computers',
name: 'computers',
}],
fields: [{name: 'firstname'}, {name: 'lastname'}]
});
Ext.define('Computers'{
extend: 'Ext.data.Model',
belongsTo: 'Employees',
fields: [{name: 'name'}]
});
How can I convert the json into my Employees extjs model?
You should have to define a data store like this:
Ext.create('Ext.data.Store', {
model: 'Employees',
storeId: 'myDataStore',
proxy: {
type: 'ajax',
url : 'path/to/your/json/file/Employees.json',
reader:{
type:'json',
root: 'employees'
}
}
});
and load it somewhere.