How to bind json data to screen with tpl config? - json

I'm working on Sencha's Admin Dashboard sample and trying to customize 'Weather' panel.
I've created a JSON format url with OpenWeatherMap's Current weather data API. The thing I can't bind JSON data to Weather panel with tpl config. I've created a ViewModel and called it within Component but it did not worked as well.
Here is the component class;
Ext.define('OWeb.view.dashboard.Weather', {
extend: 'Ext.Component',
xtype: 'weather',
baseCls: 'weather-panel',
border: false,
height: 80,
store: {
proxy: {
type: 'ajax',
url: 'http://api.openweathermap.org/data/2.5/weather?q=Antalya,TR&appid=9b59049894d42af608baf69f869b9ace&units=metric',
reader: {
type: 'json'
}
},
autoLoad: true
},
tpl: '<div class="weather-image-container"><img src="resources/img/{icon}" alt="{weather.description}"/></div>'+
'<div class="weather-details-container">' +
'<div>{main.temp}°</div>' +
'<div>{weather.main}</div>' +
'</div>'
});
And this is link for JSON data which returns via OpenWeatherMap and snippet;
{
"coord": {
"lon": 30.72,
"lat": 36.77
},
"weather": [{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}],
"base": "stations",
"main": {
"temp": 25,
"pressure": 1015,
"humidity": 23,
"temp_min": 25,
"temp_max": 25
},
"visibility": 10000,
"wind": {
"speed": 5.7,
"deg": 320
},
"clouds": {
"all": 0
},
"dt": 1507184400,
"sys": {
"type": 1,
"id": 6028,
"message": 0.0025,
"country": "TR",
"sunrise": 1507175759,
"sunset": 1507217657
},
"id": 323776,
"name": "Antalya",
"cod": 200
}
Thanks, any advice is welcome.
UPDATE
I've found this post and I tried same thing; extend from Ext.DataView, setting proxy type jsonp and using itemTpl config. Now I can bind to JSON data but only can displaying {main.temp}. Any idea please?
Ext.define('OWeb.view.dashboard.Weather', {
//extend: 'Ext.Component',
extend: 'Ext.DataView',
xtype: 'weather',
baseCls: 'weather-panel',
border: false,
height: 80,
store: {
proxy: {
type: 'jsonp',
url: 'http://api.openweathermap.org/data/2.5/weather?q=Antalya,TR&appid=9b59049894d42af608baf69f869b9ace&units=metric',
reader: {
type: 'json'
}
},
autoLoad: true
},
itemTpl: '<div class="weather-image-container"><img src="{weather.icon}" alt="{weather.description}"/></div>'+
'<div class="weather-details-container">' +
'<div>{main.temp}°</div>' +
'<div>{weather.description}</div>' +
'</div>'
});

Please look at the points below to understand this.
Ext.Component class does not work with store because of which your code was not working. You could have created the store elsewhere and then retrieved the data from store and set the data property of the component to it. (Note that for Ext.Component, the tpl property works with data property:
var data = store.getData();
component.setData(data);
jsonP proxy is needed only when you need to fetch data from a different domain.
If you are getting data in a store then better option is to extend your class from Ext.DataView because it works with store.
The values were not properly showing in the template because weather property was an array of objects and we needed something like weather[0].icon. So for this, Model was required with fields properly mapped.(Look at the mapping property.)
I have created a Fiddle for your code. Image is not showing because url is not returning any image. Rest is working. Hope this is helpful to you.

Related

Loading pushpin in the forge viewer does not respect the viewerState

We are using the "Autodesk.BIM360.Extension.PushPin" extension inside the forge viewer to enable push pins.
When a push pin has been added to the model, we serialize the pushpin data and store it in our database. An example of such a pushpin is here:
{
"id": "12",
"label": "12",
"status": "quality_issues-not_approved",
"position": {
"x": 15.324803588519861,
"y": -10.150864635427533,
"z": -5.532972775562976
},
"type": "issues",
"objectId": 24518,
"externalId": "d9a1e318-14d0-4d08-b7ab-6d1c331454c2-002793d1",
"viewerState": {
"seedURN": "dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6MDQyY2QwMmUtNzU0Yi00ZDY2LTgyYTMtNjBmYjFlOWVjMjcxL2U5ODAxZTA4LTUwZjQtNDc0ZS05ZWU4LTAxYWQ0ZGM0ODFiYl9WMV9Lb25nZXN0aWVuKzMwKy0rVGlsYnlnbmluZystK0clMjVDMyUyNUE2bGRlbmRlK2QuKzA1LjA2LnJ2dA",
"objectSet": [{
"id": [],
"isolated": [],
"hidden": [],
"explodeScale": 0,
"idType": "lmv"
}],
"viewport": {
"name": "",
"eye": ["-15.17842530349136", "-0.9048862425583284", "0.6506974303790392"],
"target": ["-22.06049144652811", "0.915848677106827", "-0.4205110420886964"],
"up": [-0.14385076361076257, 0.038057482024001874, 0.9888673247056924],
"worldUpVector": [0, 0, 1],
"pivotPoint": ["-22.510046835506888", "1.6223793651751013", "3.668585646439837"],
"distanceToOrbit": 7.198985875545766,
"aspectRatio": 1.491792224702381,
"projection": "orthographic",
"isOrthographic": true,
"orthographicHeight": 7.198985875545767
},
"renderOptions": {
"environment": "Boardwalk",
"ambientOcclusion": {
"enabled": true,
"radius": 13.123359580052492,
"intensity": 1
},
"toneMap": {
"method": 1,
"exposure": -7,
"lightMultiplier": -1e-20
},
"appearance": {
"ghostHidden": true,
"ambientShadow": true,
"antiAliasing": true,
"progressiveDisplay": true,
"swapBlackAndWhite": false,
"displayLines": true,
"displayPoints": true
}
},
"cutplanes": [],
"globalOffset": {
"x": -20.808594999999997,
"y": 6.686511499999999,
"z": 8.456207
}
},
"objectData": {
"guid": "6de5f80c-73da-30ae-b2d1-8a78f177c2a4",
"urn": "dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6MDQyY2QwMmUtNzU0Yi00ZDY2LTgyYTMtNjBmYjFlOWVjMjcxL2U5ODAxZTA4LTUwZjQtNDc0ZS05ZWU4LTAxYWQ0ZGM0ODFiYl9WMV9Lb25nZXN0aWVuKzMwKy0rVGlsYnlnbmluZystK0clMjVDMyUyNUE2bGRlbmRlK2QuKzA1LjA2LnJ2dA",
"viewableId": "aaff5911-e8b1-4ae2-b41c-4284d0703eb4-00150218",
"viewName": "{3D}"
}
}
We then load the pushpin into the model again at a later point (when the user reopens the model), like this:
pushPinExtension.loadItems([pushPinItem]);
The result is that the pushpin is added in the model at the correct place, but the viewer state is incorrect. It seems like the viewer state for the pushpin is set to the viewer state of the model at the time when we load the pushpin - and not to the viewer state stored inside the pushpin.
Is this expected behaviour? - and if so, how do I use the viewer state from the pushpin instead?
why not explicitly load the viewer state stored in the pushpin separately after loading the pushpin:
pushPinExtension.loadItems([pushPinItem]);
viewer.restoreState(pushPinItem.viewerState)
EDIT:
Try restore the viewer state when an item is clicked - subscribe to the click event with:
viewer.restoreState(...)
//...
})

Highcharts - Passing in Color with JSON

Currently, I'm using a back end process to create a JSON file for a 3d bar chart with HighCharts.
[{
"name": "Town",
"data": ["NorthCentral","NorthEast","SouthCentral","SouthEast","West"]
}, {
"name": "Population",
"data": [99.47,82,89,82,82]
}]
What I would like know is - can you pass in colors with each data point? for example:
[{
"name": "Town",
"data": ["NorthCentral","NorthEast","SouthCentral","SouthEast","West"]
}, {
"name": "Population",
"data": [{color: 'red',99.47},82,89,82,82]
}]
I have tried already with no luck. This might not be supported but I thought I would ask.
Try something like
data: [{
name: 'Point 1',
color: '#00FF00',
y: 0
}, {
name: 'Point 2',
color: '#FF00FF',
y: 5
}]
A sample with line chart

Extracting json object and filling in user label

[{"id":1,"name":"ABC"},{"id":2,"name":"XYZ"}]
This is how the data is returned by the controller in json format
I want to store it in the following format:
var json = {
"users": [
{ "id": "1", "name": "ABC" },
{ "id": "2", "name": "XYZ" },
]
}
Following is the code I have used but doesnt work.
var json =
{
"users": $.getJSON("#Url.Action("SearchCMSAdmins")")
}
$("#DistributorCMSAdmin").tokenInput("#Url.Action("SearchWithName")", {
theme: "facebook",
preventDuplicates: true,
prePopulate:json.users
});
Any help is appreciated. Thanks in advance.
$.getJson is asynchronous, so you need to use the success callback for getting the response and doing the operation. Try this INstead.
$.getJSON("#Url.Action("SearchCMSAdmins")",function(data){
var json= {"users":data};
$("#DistributorCMSAdmin").tokenInput("#Url.Action("SearchWithName")", {
theme: "facebook",
preventDuplicates: true,
prePopulate:json.users
});
});

json not loading in iphone device while using sencha touch and phonegap

I'm trying to load a json into my view. Im using phonegap with sencha touch and when I load the app to my phone the json does not load at all.. It works fine in the browser and in the simulator.
I would really appreciate some help from the experts
Here is the main code that im trying:
the store:
App.stores.freebees = new Ext.data.Store({
model: 'Freebee',
autoLoad: true,
proxy: {
type: 'ajax',
url: 'fixtures/freebees',
reader: {
type: 'json'
}
}
});
the list view:
App.views.FreebeesList = Ext.extend(Ext.List, {
id: 'indexlist',
layout: 'fit',
store: App.stores.freebees,
itemTpl: '{companyName}, {title}, {address}',
listeners: {
'itemtap': function(list, index, item, obj) {
Ext.dispatch({
controller: 'Freebee',
action: 'showDetails',
id: list.getRecord(item).data.id,
lat: list.getRecord(item).data.lat,
longitude: list.getRecord(item).data.longitude,
companyName: list.getRecord(item).data.companyName,
address: list.getRecord(item).data.address,
});
}
},
initComponent: function() {
App.views.FreebeesList.superclass.initComponent.apply(this, arguments);
}
});
Ext.reg('App.views.FreebeesList', App.views.FreebeesList);
the json:
[
{
"id": 1,
"title": "Freebee 1",
"companyName": "Företaget AB 1",
"address": "Ekuddsvägen 1 Nacka 131 38 Sweden",
"lat": 59.3058,
"longitude": 18.1463
},
{
"id": 2,
"title": "Freebee 2",
"companyName": "Företaget AB 2",
"address": "Ekuddsvägen 2 Nacka 131 38 Sweden",
"lat": 59.305,
"longitude": 18.1478
}
]
From my limited experiece with ST so far, you cant load a file which is on the device like that. You may have to load the json file using a script tag and pass it in as data.
data = [ { "id": 1, "title": "Freebee 1", "companyName": "Företaget AB 1", "address": "Ekuddsvägen 1 Nacka 131 38 Sweden", "lat": 59.3058, "longitude": 18.1463 }, { "id": 2, "title": "Freebee 2", "companyName": "Företaget AB 2", "address": "Ekuddsvägen 2 Nacka 131 38 Sweden", "lat": 59.305, "longitude": 18.1478 } ];
then load it into your store like so:
App.stores.freebees = new Ext.data.Store({ model: 'Freebee', root:data, autoLoad: true, proxy: { type: 'ajax', url: 'fixtures/freebees', reader: { type: 'json' } } });
(added root:data)
I had a similar error.. Please see my question and answer here:
Sencha parses JSON in iphone simulator, but not on iPhone - phonegap

extJS: reading a nested JSON

I have a pretty nested JSON coming from a ldap_search() call. I would like to use this information to populate an ExtJS ComboBox, but I am facing some troubles with the reader. Apparently, I am not able to read the information that I need in the ComboBox, that is the mail address of the people, the uid and the cn
I think the whole problem lies in the store. I was trying the following code:
var store= new Ext.data.JsonStore({
url:'search.php',
root: '',
totalProperty: 'count',
fields: [
{name:'cn', type: 'string', mapping:'cn.0'},
{name:'mail', type: 'string', mapping:'mail.0'},
{name:'uid', type: 'string', mapping:'uid.0'}
]
});
but FireBug told me missing ; before statement return obj.cn.0 in ext-all.js (line 7). I tried with another, easier JSON array and it works, that is why I really think the problem lies in this part of code, especially in the mapping.
an example of JSON returned by search.php is:
{
"count": 2,
"0": {
"mail": {
"count": 1,
"0": "Mail address not registered."
},
"0": "mail",
"uid": {
"count": 1,
"0": "name0.surname0#domain.com"
},
"1": "uid",
"cn": {
"count": 1,
"0": "Surname0 Name0"
},
"2": "cn",
"count": 3,
"dn": "cn=Surname0 Name0,ou=personal,dc=domain,dc=com"
},
"1": {
"mail": {
"count": 1,
"0": "name1.surname1#domain.com"
},
"0": "mail",
"uid": {
"count": 1,
"0": "name1.surname1"
},
"1": "uid",
"cn": {
"count": 1,
"0": "Surname 1 Name 1"
},
"2": "cn",
"count": 3,
"dn": "cn=Surname1 Name1,ou=personal,dc=domain,dc=com"
}
}
Thanks for your time.
Yep, that JSON structure is not going to work straight away with standard ExtJS JSONReader. Take a look at this example taken from the ExtJS API documentation on how the JSON should look like.
{
results: 2000, // Reader's configured totalProperty
rows: [ // Reader's configured root
// record data objects:
{ id: 1, firstname: 'Bill', occupation: 'Gardener' },
{ id: 2, firstname: 'Ben' , occupation: 'Horticulturalist' },
...
]
}
Also, the root config option is required, you cannot leave it empty. In the above example your root would be "rows".
You are probably going to need to parse that JSON of yours into a simpler format at first, before feeding it to the JSONReader.
I was looking to do the same thing, but have one of the nested items be a field in my chart. This post kept coming up, so I thought it might be helpful to see what I did to solve the chart issue. The key to solving it is knowing that the label config exists: http://docs.sencha.com/ext-js/4-0/#!/api/Ext.chart.Label. Using that you can override the default render of what you pass in. In this example the field is "key" (Not shown here, but my model is using the default type for 'key' (ie., not string)). The key object gets passed to renderer. Using function(t), I can now access that object like javascript and pass back the name under the object.
json
key : {
wholePath : "c:/.../fileName.txt",
fileName : "fileName.txt",
}
code:
axes: [
{
title: 'Values',
type: 'Numeric',
position: 'left',
fields: ['value'],
minimum: 0,
maximum: 100,
minorTickSteps: 1
},
{
title: 'File Name',
type: 'Category',
position: 'bottom',
fields: ['key'],
label: {
renderer: function(t) {
var fileName = t.name;
return fileName;
}
}
}