Formfield value from Webservice in Sencha touch - sencha-touch

In my Sencha touch application, I am populating my container by creating form items dynamically from a json object.
For a textfield I'm using the code as below:
Ext.getCmp('mytestpanel').add({
xtype: response.screens[index].screenItems[i].xtype,
id:response.screens[index].screenItems[i].name,
label: response.screens[index].screenItems[i].title,
name: response.screens[index].screenItems[i].name
}
});
The json used:
{
"screens": [
{
"screenname": "Screen 1",
"screenItems": [
{"xtype": "textfield", "title": "Temperature"," name": "textfield1", "value":"service211"},
{"xtype": "button", "title": "Update", "name": "button1"}
]
},
{
"screenname": "Screen 2",
"screenItems": [
{"xtype": "emailfield", "title": "screen2"," name": "textfield2"},
{"xtype": "checkboxfield", "title": "label3", "name": "textfield13"},
{"xtype": "selectfield", "title": "Submit", "name": "button11"}
]
}
]
}
I need to update the value of this text field by using a webservice as seen in the json
{"xtype": "textfield", "title": "Temperature"," name": "textfield1", "value":"someservice211"}
As the values will be in real time, how am I supposed to do this?

What about using a store for reading the json? you could setInterval to store.load() and then add/edit the dynamic items on the load callback:
proxy: {
type: 'ajax',
url: 'http://URL',
reader: {
type: 'json',
rootProperty: 'screens'
}
},
autoLoad: true,
listeners: {
initialize: function() {
var interval = setInterval(function() {
Ext.StoreMgr.lookup('myStore').load();
}, 2000);
},
load: function(st) {
var testPanel = Ext.getCmp('mytestpanel');
Ext.each(st, function(record) {
var obj = Ext.getCmp(record.get('id'));
if (obj) {
if (record.get('value')) obj.setValue(record.get('value'));
} else {
testPanel.add({
xtype: record.get('xtype'),
id: record.get('id'),
label: record.get('label'),
name: record.get('name'),
value: record.get('value')
});
}
}
}
}
hope it helps-

Related

How to display a List of object in Kendo Grid in jquery?

I am working on a project in which I need to display List of objects in Kendo Grid.
below is the code for data binding.
$("#GridProperty").kendoGrid({
dataSource: {
transport: {
read: {
dataType: "json",
url: $_BindPropertyData,
data: { "LegalEntityId": LegalEntityId, "DatasetId": DatasetId },
async: true,
}
},
schema: {
model: {
fields: {
GeoId: { type: "int" },
GeoDescription: { type: "string" },
GeoAbbreviation: { type: "string" },
//ComponentDesc: { type: "string", from: "CompModelList.ComponentDesc" },
Id: { type: "int" },
Amount1: { type: "decimal", from: "CompModelList.SaDataModel.Amount1" },
Amount2: { type: "decimal"}
//lstcompModel: {type: ""}
}
}
},
},
noRecords: {
template: "<p style='margin-top:5px;'>There are currently no items available<p>"
},
height: 525,
reorderable: true,
groupable: true,
sortable: true,
filterable: true,
resizable: true,
columnMenu: true,
selectable: "row",
dataBound: function () {
},
columns: Columns
});
Description :
I am returning list of objects as json from my Action . but i am not able to bind the list of objects in kendo grid.
I am breaking my head for last four days. Please help.
Thanks

Sencha Touch Build production app not working as expected

I am using Ext.plugin.PullRefresh plugin which is working fine when I directly run my app on
the browser but when I build my production through sencha cmd 4.0 its duplicating my content whenever I pull to refesh.
I have already used idProperty on my model.
My Jsonp:
callback({"Message":"Success","Post":[{"id":"35","UserId":"faisalkhalid690","Content":"lol","Time":"2013-12-03 05:28:15"},{"id":"50","UserId":"faisalkhalid","Content":"asdfasdfasdf","Time":"2013-12-03 05:52:27"},{"id":"51","UserId":"faisalkhalid","Content":"sadfasdfasdf","Time":"2013-12-03 05:52:38"},{"id":"52","UserId":"faisalkhalid","Content":"holloa","Time":"2013-12-03 05:52:50"},{"id":"70","UserId":"faisalkhalid690","Content":"hello","Time":"2013-12-04 23:22:52"}]});
Model:
Ext.define('talkbag.model.Comments', {
extend: 'Ext.data.Model',
config: {
idProperty: 'id',
fields: [
{ name: 'id', type: 'auto' },
{ name: 'UserId', type: 'auto' },
{ name: 'Content', type: 'auto' },
{ name: 'Time', type: 'auto' }
]
}
});
Store:
Ext.define('talkbag.store.Comments', {
extend:'Ext.data.Store',
storeId:'Comments',
config:{
autoLoad: true,
model:'talkbag.model.Comments',
proxy: {
type: 'jsonp',
url : 'http://www.litemake.com/ViewComments.php?Pid='+talkbag.User.PostId,
reader: {
type: 'json',
rootProperty: 'Post'
}
}
}
});
View:
Ext.define('talkbag.view.ViewPost.ViewCommentDetail', {
extend:'Ext.Panel',
xtype:'ViewCommentDetail',
requires:['Ext.plugin.PullRefresh'],
extend:'Ext.Panel',
config:{
layout:'vbox',
items:
[{
xtype:'appbar',
},{
flex:1,
xtype:'list',
store:'Comments',
plugins: [
{
xclass: 'Ext.plugin.PullRefresh',
pullRefreshText: 'Pull down for new Comments',
}
],
disableSelection :true,
itemTpl:'<table><tr><td width="80px"><table align="center"><tr><td align="center"><img src="http://www.litemake.com/getPic.php?userId={UserId}" heigth="30px" width="30px"/></td></tr><tr><td style="font-size:0.6em">{UserId}</td></tr></table></td><td style="padding-left:20px"><table><tr><td style="font-size:0.7em; padding:0px 0px 5px 0px">{Content}</td></tr><tr><td style="font-size:0.5em">{Time}</td></tr></table></td></tr></table>',
}]
}
});
Have you added a requires for it? that is
requires:['Ext.plugin.PullRefresh']
or something like that?
I have also faced this type of duplicating record issue, In my case i have put an autogenerated id field in my json, like this way
{
"items": [
{
"id": 0,
"ContactName": "Aanitha Ps",
},
{
"id": 1,
"ContactName": "Aby",
}
]
}
and reference that id field in my model, i think sencha some times not taking model default id field.

Kendo UI: Sum in footertemplate

I'm trying to get my footerTemplate working with a Kendo UI grid. I want to get the sum of 'hours_worked' in the footer of the tabel. I've tryed several options from the Kendo UI example but it doesn't work for me. What im doing wrong?
$(document).ready(function() {
$("#grid").kendoGrid({
dataSource: {
transport:
read: {
url: "mods/hours/data/get_hours.php?id=<?php echo $volunteer_id; ?>",
dataType: "json"
}
},
schema: {
model: {
fields: {
hours_id: { type: "number" },
volunteer_first_name: { type: "string" },
volunteer_last_name: { type: "string" },
hours_date: { type: "date" },
location_name: { type: "string" },
work_type_name: { type: "string" },
volunteer_id: { type: "number" },
hours_worked: { type: "number" }
}
}
},
aggregate:[{ field:"hours_worked", aggregate:"sum" }],
pageSize: 10
},
height: 350,
filterable: true,
sortable: true,
pageable: true,
selectable:true,
columns: [
{
title:"Naam",
template:"#=volunteer_last_name#, #=volunteer_first_name#",
},{
title:"Locatie",
field:"location_name",
},{
title:"Werkzaamheden",
field:"work_type_name",
},{
title:"Uren",
field:"hours_worked",
footerTemplate:"Sum: #=sum#",
},{
title:"Datum",
field:"hours_date",
},{
width:"200px",
title:"Opties",
filterable: false,
template:"<a href='?p=edit_reported_hours&id=#=volunteer_id#&hours_id=#=hours_id#' class='k-button'>Bewerken</a> <a href='?p=manage_reported_hours&o=delete&id=#=volunteer_id#&hours_id=#=hours_id#' class='k-button'>Delete</a>"
},
]
});
});
</script>
Add a curly bracket after transport and it will work.
transport: {
read: {
url: "mods/hours/data/get_hours.php?id=<?php echo $volunteer_id; ?>",
dataType: "json"
}
},
See it here: http://jsfiddle.net/OnaBai/bWS7C/

Loading associated data in Sencha Touch without nesting

I'm working on a basic Sencha Touch application that displays a list of text messages and the name of an associated user that sent the message. I have followed the tutorials online on how to setup model associations but each tutorial assumes that the server produces data with a nested structure.
The data I am working with has a flat structure with primary/foreign key relationships, and I cannot figure out how to get Sencha to load both stores from a single response.
model/User.js
Ext.define('App.model.User', {
extend: 'Ext.data.Model',
config: {
fields: [
{ name: 'uid', type: 'number' },
{ name: 'name', type: 'string' },
]
}
});
store/Users.js
Ext.define('App.store.Users', {
extend: 'Ext.data.Store',
config: {
model: 'App.model.User',
autoLoad: true,
}
});
model/Message.js
Ext.define('App.model.Message', {
extend: 'Ext.data.Model',
config: {
fields: [
{ name: 'id', type: 'number' },
{ name: 'uid', type: 'number' },
{ name: 'message', type: 'string' }
],
associations: [{
type: 'belongsTo',
model: 'App.model.User',
primaryKey: 'uid',
foreignKey: 'uid'
}],
proxy: {
type: 'jsonp',
url: 'messages.json',
reader: {
type: 'json',
rootProperty: 'req_messages'
}
}
}
});
store/Messages.js
Ext.define('App.store.Messages', {
extend: 'Ext.data.Store',
config: {
model: 'App.model.Message',
autoLoad: true,
}
});
The messages are correctly loaded and displayed by my application (sample JSON response below), but I cannot figure out how to get the associated users to be loaded into the store. Can this be solved with a configuration, or will I need a custom reader? Any help appreciated!
Sample JSON
{
"users": [{
"uid": "1",
"name": "John"
}, {
"uid": "3033",
"name": "Noah"
}],
"req_messages": [{
"id": "539",
"uid": "1",
"message": "my message"
}, {
"id": "538",
"uid": "1",
"message": "whoops"
}, {
"id": "534",
"uid": "3033",
"message": "I love pandas."
}]
}
I've never really worked with associations and I went through the document to try to find something that would load two stores with on request, but I couldn't find anything. So here's how I would do it :
Model
Ext.define('App.model.User', {
extend: 'Ext.data.Model',
config: {
fields: [
'uid',
'name'
]
}
});
Ext.define('App.model.Message', {
extend: 'Ext.data.Model',
config: {
fields: [
'id',
'message',
'uid'
],
associations: { type: 'hasOne', model: 'User', primaryKey: 'uid', foreignKey: 'uid'}
}
});
Stores
Ext.define('App.store.Users', {
extend: 'Ext.data.Store',
config: {
model: 'App.model.User',
autoLoad: true,
proxy: {
type: 'ajax',
url: 'http://www.titouanvanbelle.fr/test.json',
reader: {
type: 'json',
rootProperty: 'users'
}
},
listeners:{
load:function(s,r,success,op){
var msgs = JSON.parse(op.getResponse().responseText).req_messages;
Ext.each(msgs, function(msg) {
Ext.getStore('Messages').add(Ext.create('App.model.Message',msg));
});
}
}
}
});
Ext.define('App.store.Messages', {
extend: 'Ext.data.Store',
config: {
model: 'App.model.Message'
}
});
List
Ext.define("App.view.Main", {
extend: 'Ext.Panel',
config: {
fullscreen: true,
layout:'fit',
items: [{
xtype:'list',
store:'Messages',
itemTpl: new Ext.XTemplate(
'<tpl for=".">',
'{[this.getUserName(values)]} : {message}',
'</tpl>',
{
getUserName(v){
var s = Ext.getStore('Users'),
u = s.getAt(s.find('uid',v.uid));
return u.get('name');
}
}
)
}]
}
});
And you'd get something like this :
Hope this helps. If you need explanation, let me know.

Nested List infinite loop with Sencha Touch 1

I am new to Sencha Touch and web-programming. I am trying to display a nested list and am only getting the first level items in a loop over and over again.
I have looked at the documentation and kitchen sink examples, but I just can't get it right. I would be very thankful for tips!
This is my Code:
Ext.regModel('Site', {
fields: [{
name: "sitename",
type: "string"
}, {
name: "last_connection",
type: 'auto'
}]
});
Ext.setup({
icon: 'icon.png',
tabletStartupScreen: 'tablet_startup.png',
phoneStartupScreen: 'phone_startup.png',
glossOnIcon: false,
onReady: function() {
var store = new Ext.data.TreeStore({
model: 'Site',
proxy: {
type: 'ajax',
url: 'network.json',
reader: {
type: 'json',
root: 'items'
}
}
});
var nestedList = new Ext.NestedList({
fullscreen: true,
title: 'Netzwerk',
displayField: 'sitename',
plugins: [new Ext.LeafSelectedPlugin()],
// add a / for folder nodes in title/back button
getTitleTextTpl: function() {
return '{' + this.displayField + '}<tpl if="leaf !== true">/</tpl>';
},
// add a / for folder nodes in the list
getItemTextTpl: function() {
return '{' + this.displayField + '}<tpl if="leaf !== true">/</tpl>';
},
store: store
});
}
});
And the structure of the json-file (only one node as example):
{
"items": [{
"id": "11",
"sitename": "Site A",
"items": [{
"id": "11",
"sitename": "Endpoint 1",
"last_connection": "2012-03-02T10:03:22+0100",
"ping_time": 63,
"leaf": true
}, {
"id": "12",
"sitename": "Endpoint 2",
"last_connection": "2012-03-02T10:03:22+0100",
"ping_time": 57,
"leaf": true
}],
"last_connection": "2012-03-02T10:03:22+0100",
"ping_time": 57
}]
}
Thanks!
I know this post is a little bit old, but if it would already be answered it would have saved me 2 "long" hours of my life, so here it goes:
I figured out I was missing the defaultRootProperty inside TreeStore. According to your JSON you need to set this property to items.
Here is how your TreeStore should look like:
var store = new Ext.data.TreeStore({
model: 'Site',
defaultRootProperty: 'items',
proxy: {
type: 'ajax',
url: 'network.json',
reader: {
type: 'json',
root: 'items'
}
}
});