What could I be doing wrong; I only get -1 when I do a search of a store?
My model
Ext.define('app.model.Sections', {
extend: 'Ext.data.Model',
config: {
fields: [{
name: 'id',
type: 'int'
}, {
name: 'title',
type: 'string'
}, {
name: 'content',
type: 'string'
}]
}
});
My Store
Ext.define('app.store.Sections', {
extend: 'Ext.data.TreeStore',
requires: [
'app.model.Sections'
],
config: {
autoLoad: true,
model: 'app.model.Sections',
proxy: {
type: 'ajax',
url: 'resources/data/data.json',
reader: {
type: 'json',
rootProperty: 'items'
}
}
}
});
The json file is like this
{
"items": [{
"id": "27",
"title": "Title 1",
"content": "",
"items": [{
"id": "85",
"title": "Title 2",
"content": "content 2",
"leaf": true
}, {
"id": "78",
"title": "Title 3",
"content": "content 3",
"leaf": true
}]
}, {
"id": "29",
"title": "Title 4",
"content": "",
"items": [{
"id": "97",
"title": "Title 5",
"content": "content 5",
"leaf": true
}, {
"id": "93",
"title": "Title 6",
"content": "content 6",
"leaf": true
}, {
"id": "105",
"title": "Title 7",
"content": "content 7",
"leaf": true
}]
}]
}
I am able to get the store and view the data in the store but the index always comes out -1
var store = Ext.getStore('Sections');
var index = store.findExact('id', 85);
console.log(store);
console.log(index);
I used carcel's suggestion to do the store.on('load', function ());.
I also changed the data type of id to string so there would be no confusion.
initialize: function() {
var sectionID = '85';
var store = Ext.getStore('Sections');
// search once the store is loaded
store.on('load', function() {
// get the node record
var record = store.getNodeById(sectionID);
if (record == null)
console.log('no record found');
else {
var parentPanel = Ext.getCmp('myabouttest');
parentPanel.setHtml(record.data.content);
var titlebar = Ext.getCmp('mytitilebar');
titlebar.setTitle(record.data.title);
}
}
});
}
The initialize function completes before the store can be loaded so to display the data I have to specify IDs for the panel and the title and then get their components to set the items.
Related
I have a remote data and I am trying to build Kendo UI Chart's DataSource. The remote data represents a generic data model for most charts. The model goes like this,
model: {
fields: {
Title: { field: "Title", type: "string" },
XLabel: { field: "XLabel", type: "string" },
YLabel: { field: "YLabel", type: "string" },
Legend: [
{?????????{ type: "string" }}
]
},
hasChildren: true,
children: "ChartDataSets"
}
}
Sample of remote data:
{
"Chart": [
{
"Title": "1",
"XLabel": "",
"YLabel": "",
"Legend": [ "P1","P2","P3"],
"ChartDataSets": [
{
"GroupName": "Week 0",
"Series": [
{
"Key": "2015",
"Value": 42
},
{
"Key": "2016",
"Value": 42
}
]
},
{
"GroupName": "Week 1",
"Series": [
{
"Key": "2015",
"Value": 52
},
{
"Key": "2016",
"Value": 32
}
]
}
]
}
]
}...
So Legend is an array of strings and ChartDataSets is an array of json objects. How do I represent Legend as an array or that it has children?
Also, would you recommend using Hierarchical DataSource? How is the performance affected if I use Hierarchical DataSource?
__________Never Mind - Found it ____________
Solution:
model: {
fields: {
Title: { field: "Title", type: "string" },
XLabel: { field: "XLabel", type: "string" },
YLabel: { field: "YLabel", type: "string" },
Legend: [{field: "Legend"}]
},
hasChildren: true, ...
I have data that look like this:
{[{id: "1",
stories: [{
id: "11",
items: [{ id:"111", title:"bla bla" },{ id:"222", title:"bla bla" },{ id:"333", title:"bla bla" }]
}]
its array of objects that have 3 levels of items.
how do i manage it in the best practice in redux?
Check out https://github.com/gaearon/normalizr. It allows you to describe nested data as collections of schemas. For your example, I think you could use:
import { normalize, Schema, arrayOf } from 'normalizr';
const collection = new Schema('collections');
const story = new Schema('stories');
const item = new Schema('items');
collection.define({
stories: arrayOf(story)
});
story.define({
items: arrayOf(item)
})
// i'm not sure what your outer result type is, so i've
// just named it 'collection'
const collections = [{id: "1",
stories: [{
id: "11",
items: [{ id:"111", title:"bla bla" },{ id:"222", title:"bla bla" },{ id:"333", title:"bla bla" }]
}]
}]
const normalized = normalize(collections, arrayOf(collection));
/* normalized === {
"entities": {
"collections": {
"1": {
"id": "1",
"stories": [
"11"
]
}
},
"stories": {
"11": {
"id": "11",
"items": [
"111",
"222",
"333"
]
}
},
"items": {
"111": {
"id": "111",
"title": "bla bla"
},
"222": {
"id": "222",
"title": "bla bla"
},
"333": {
"id": "333",
"title": "bla bla"
}
}
},
"result": [
"1"
]
} */
The result key tells you that you have received one collection with the id of 1. From there you can index into the entities key, which has been flattened by id. For more on how to then use this in your dispatcher, check https://github.com/gaearon/normalizr#explanation-by-example.
Disclaimer: I have not used normalizr, but since it was written by Dan Abramov (author of Redux) I think you'll be in good hands.
I am getting json data from server for make the navigation menu (has the sublinks ) - for that, i am making my model as follow. is this correct..? any one help me please?
Here is the json i am getting from server:
[
{
"label": "General",
"link": "#/general",
"subLinks": [
{
"label": "Dashboard",
"link": "#/dashboard"
},
{
"label": "My Task",
"link": "#/mytask"
},
{
"label": "My Documents",
"link": "#/mydocuments"
},
{
"label": "My Templates",
"link": "#/mytemplates"
},
{
"label": "Search",
"link": "#/search"
}
]
},
{
"label": "Repositories",
"link": "#/reposotories",
"subLinks": []
},
{
"label": "SavedSearches",
"link": "#/savedSearches",
"subLinks": []
},
{
"label": "Favourites",
"link": "#/favourites",
"subLinks": []
},
{
"label": "Reports",
"link": "#/reports",
"subLinks": []
},
{
"label": "Preferences",
"link": "#/preferences",
"subLinks": []
}
]
after i receive my json i use the parse method to manipulate the models:
define(["backbone","models/model","collection/baseCollection"], function (Backbone,model,baseCollection) {
var mainLinkModel = model.extend({
defaults:{
label:"mainLink",
link:"#",
subLinks:[
label:"mainLink",
link:"#"
]
}
})
var headerCollection = baseCollection.extend({
url:function(){
return this.path + "edms/navigationLinksTest"
},
model:model,
initialize:function(){
},
parse:function(response){
var mainNavi = [];
_.each(response.navList, function(m){
// i am making new models.. but how to handle the sublinks?
mainNavi.push(new mainLinkModel(m));
})
}
});
return new headerCollection;
})
how to handle this kind of models.. any one help me in this please..?
I am trying to load some data from the json file hosted on the local server but I am getting an error "XMLHttpRequest cannot load http://localhost:8080/data.json?_dc=1355779280677&page=1&start=0&limit=25. Origin null is not allowed by Access-Control-Allow-Origin. " as soon as the application loads. My list is not populated with the records.
My code is as below.
Ext.define('POC.view.HomePage', {
extend: 'Ext.TabPanel',
requires:['Ext.TitleBar','Ext.dataview.List',
'Ext.Ajax','Ext.data.proxy.Ajax'],
xtype:'homePage',
alias: 'widget.wuHomePageView',
config: {
fullscreen: true,
items: [
{
title: 'Home',
iconCls: 'home',
items: [
{
xtype: 'list',
title: 'Sample',
height: '100%',
onItemDisclosure: true,
store: {
autoLoad: true,
fields: ['name'],
proxy: {
type: 'json',
url: 'http://localhost:8080/data.json',
reader: {
type: 'ajax',
rootProperty: 'stores'
}
}
},
itemTpl: '{name}',
}],
},
{
title: 'Contact',
iconCls: 'user',
html: 'Contact Screen'
}
]
},
});
My Json File is as below.
{
"stores": [
{
"name": "Science Gallery",
"logo": "sciencegallery.jpg",
"desc": "Get some food",
"telephone": "016261234",
"city": "Dublin",
"add1": "Pearse Street",
"post": "2",
"country": "Ireland",
"latitude": "53.34422",
"longitude": "-6.25006",
"menu": [
{
"item": "SC Sandwich"
},
{
"item": "SC Toasted Sandwich"
},
{
"item": "SC Panini"
},
{
"item": "SC Ciabatta"
},
{
"item": "SC Burrito"
}
]
},
{
"name": "Spar",
"logo": "spar.jpg",
"desc": "Get some food",
"telephone": "016261234",
"city": "Dublin",
"add1": "Mayor Street",
"post": "2",
"country": "Ireland",
"latitude": "53.34422",
"longitude": "-6.25006",
"menu": [
{
"item": "Spar Sandwich"
},
{
"item": "Spar Toasted Sandwich"
},
{
"item": "Spar Panini"
},
{
"item": "Spar Ciabatta"
},
{
"item": "Spar Burrito"
}
]
}
]
}
Please help.
Thanks
There is no such a thing like a json proxy! You have to use an ajax proxy:
proxy: {
type: 'ajax',
.....
}
I have a json file and I assume that I do not know anyting about the content. I do not know the model. However it is given in the json file the model, the data, and other information about the grid. How I'll create the columns etc in this way?
Stackoverflow is littered with questions very similar to this one. I worked through them all and did not find a definitive solution. However, most of the provided answers pointed me in the right direction. I'll give me best shot at putting all those suggestions together and making this clear for others:
Model: (Only shows 2 fields that will be in all JSON responses. Will still be overwritten)
Ext.define('RTS.model.TestsModel', {
extend: 'Ext.data.Model',
alias: 'model.TestsModel',
fields: [
{
name: 'poll_date'
},
{
name: 'poller'
}
]
});
Store:
Ext.define('RTS.store.TestsStore', {
extend: 'Ext.data.Store',
alias: 'store.TestsStore',
model: 'RTS.model.TestsModel',
constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
autoLoad: false,
proxy : {
type : 'ajax',
url : 'tests.php',
reader : {
type : 'json',
root : 'tests',
successProperty : 'success'
}
},
storeId: 'tests-store'
}, cfg)]);
}
});
View: (The columns will be defined in each JSON response)
Ext.define('RTS.view.TestsView', {
extend: 'Ext.grid.Panel',
alias: 'widget.TestsView',
id: 'tests-view',
title: 'Tests',
emptyText: '',
store: 'TestsStore',
initComponent: function() {
var me = this;
Ext.applyIf(me, {
viewConfig: {
},
columns: [
]
});
me.callParent(arguments);
}
});
Controller: (The controller does all the work in forcing the view and model to change based on the JSON response).
Ext.define('RTS.controller.TestsController', {
extend: 'Ext.app.Controller',
alias: 'controller.TestsController',
stores: [
'TestsStore'
],
models: [
'TestsModel'
],
views: [
'TestsView'
],
init: function(application) {
// When store changes, trigger an event on grid
// to be handled in 'this.control'.
// NOTE : Ext JS does not allow control of
// non-component events.
// Ext JS 4.2 beta will allow the controller
// to detect non-component changes and handle them
var testsStore = this.getStore('TestsStore');
testsStore.on("metachange", metaChanged, this);
function metaChanged(store, meta) {
var grid = Ext.ComponentQuery.query('TestsView')[0];
grid.fireEvent('metaChanged', store, meta);
};
this.control({
"TestsView": {
metaChanged: this.handleStoreMetaChange
}
});
},
/**
* Will update the model with the metaData and
* will reconfigure the grid to use the
* new model and columns.
*/
handleStoreMetaChange: function(store, meta) {
var testsGrids = Ext.ComponentQuery.query('TestsView')[0];
testsGrids.reconfigure(store, meta.columns);
}
});
JSON Response:
Your json response must have the "metaData" property included. It should define the fields just as you would on a static model and the view that would normally be defined to show the fields.
{
"success": true,
"msg": "",
"metaData": {
"fields": [
{
"name": "poller"
},
{
"name": "poll_date"
},
{
"name": "PING",
"type": "int"
},
{
"name": "SNMP",
"type": "int"
},
{
"name": "TELNET",
"type": "int"
},
{
"name": "SSH",
"type": "int"
},
{
"name": "all_passed"
}
],
"columns": [
{
"dataIndex": "poller",
"flex": 1,
"sortable": false,
"hideable": false,
"text": "Poller"
},
{
"dataIndex": "poll_date",
"flex": 1,
"sortable": false,
"hideable": false,
"text": "Poll Date"
},
{
"dataIndex": "PING",
"flex": 1,
"sortable": false,
"hideable": false,
"text": "PING",
"renderer": "RenderFailedTests"
},
{
"dataIndex": "SNMP",
"flex": 1,
"sortable": false,
"hideable": false,
"text": "SNMP",
"renderer": "RenderFailedTests"
},
{
"dataIndex": "TELNET",
"flex": 1,
"sortable": false,
"hideable": false,
"text": "TELNET",
"renderer": "RenderFailedTests"
},
{
"dataIndex": "SSH",
"flex": 1,
"sortable": false,
"hideable": false,
"text": "SSH",
"renderer": "RenderFailedTests"
},
{
"dataIndex": "all_passed",
"flex": 1,
"sortable": false,
"hideable": false,
"text": "All Passed",
"renderer": "RenderFailedTests"
}
]
},
"tests": [
{
"poller": "CHI",
"poll_date": "2013-03-06",
"PING": "1",
"SNMP": "0",
"TELNET": "1",
"SSH": "0",
"all_passed": "0"
},
{
"poller": "DAL",
"poll_date": "2013-03-06",
"PING": "1",
"SNMP": "0",
"TELNET": "1",
"SSH": "0",
"all_passed": "0"
},
{
"poller": "CHI",
"poll_date": "2013-03-04",
"PING": "1",
"SNMP": "0",
"TELNET": "1",
"SSH": "0",
"all_passed": "0"
},
{
"poller": "DAL",
"poll_date": "2013-03-04",
"PING": "1",
"SNMP": "0",
"TELNET": "1",
"SSH": "0",
"all_passed": "0"
},
{
"poller": "CHI",
"poll_date": "2013-03-01",
"PING": "1",
"SNMP": "0",
"TELNET": "1",
"SSH": "0",
"all_passed": "0"
}
]
}
http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.reader.Json -> Response MetaData section
in the grid don't forget to add this one columns: [], then under the store listeners: { 'metachange': function(store, meta) { myGrid.reconfigure(store, meta.columns); } } and the response json file should have metaData with fields and columns. Read Response MetaData section in the documentation for more info.
You can create grid definition in runtime. Look at the reconfigure method: http://docs.sencha.com/ext-js/4-0/#!/api/Ext.grid.Panel-method-reconfigure