Sencha Touch and PHP Webservice - php

I'm currently learning sencha touch and as a test i wrote a little webservice and i want to display the resulting items in a list
But i can't get it to work...
Ext.define('GS.view.ListTest', {
extend: 'Ext.navigation.View',
requires:[
'Ext.dataview.List',
'Ext.data.proxy.JsonP',
'Ext.data.Store'
],
xtype:'listpanel',
config: {
title: 'Fifa List',
iconCls: 'star',
items: {
xtype: 'list',
itemTpl: '{Player1}',
store: {
autoLoad: true,
fields: ['MatchID','Player1', 'ScorePlayer1' ,'ScorePlayer2','Player2'],
proxy: {
type: 'jsonp',
url: 'http://fifa.verhulstrobin.be/webservices/getMatches.php',
reader: {
type:'json',
rootProperty: 'matches'
}
}
}
}
}
});
I check and my json is valid ...
All it says in the console is Uncaught SyntaxError: Unexpected token :

try this
items: {
xtype: 'list',
itemTpl: '{match.Player1}',
store:new Ext.create('Ext.data.Store', {
autoLoad: true,
fields: ['match'],
proxy: {
type: 'jsonp',
url: 'http://fifa.verhulstrobin.be/webservices/getMatches.php',
reader: {
type:'json',
rootProperty: 'matches'
}
})
}
}

Try something like this instead :
items: {
xtype: 'list',
itemTpl: '{match.Player1}',
store: {
autoLoad: true,
fields: ['match'],
proxy: {
type: 'jsonp',
url: 'http://fifa.verhulstrobin.be/webservices/getMatches.php',
reader: {
type:'json',
rootProperty: 'matches'
}
}
}
}
Hope this helps

Looking at your webservice response I notice that it does not wrap the JSON answer in a function call like JSONP would require. You need to return an item like this:
Ext.data.JsonP.callback1({});
Where the argument of the function must be your actual JSON data instead of an empty object, and the name of the function "Ext.data.JsonP.callback1" is passed to your webservice as parameter named "callback" by the Ext Store.
You can however customize that parameter's name with callbackKey on the proxy:
var store = Ext.create('Ext.data.Store', {
model: 'User',
proxy: {
type: 'jsonp',
url : 'http://domainB.com/users',
callbackKey: 'theCallbackFunction' // <--
}
});
For more detailed information on JSONP and how to implement it for an Ext Js Store you could refer to http://en.wikipedia.org/wiki/JSONP
and http://docs.sencha.com/touch/2-0/#!/api/Ext.data.proxy.JsonP

Related

extjs6: how to display the json object records that are generated via jsonstore/proxy reader

var store = Ext.create('Ext.data.JsonStore', {
storeId:'thisstore',
proxy: {
type: 'ajax',
url: 'Stores/ShowModule/showGrid.php',
simpleSortMode: true,
reader: {
type: 'json',
rootProperty: 'data',
totalProperty: 'total'
}
},
fields: [a,b,c,d],
autoLoad: true
});
I am pretty newbie to extjs 6, and I got this jsonstore that would retrieve the data from the php output using proxy reader. I wonder how to print out the entire json object obtained from this jsonstore into console.log, as to verify if that's exactly the json structure I want.
Many thanks in advance.
In ExtJS 6 , after the store is loaded , you can print records in the console as :
console.log(store.getData());
Use Listeners in Store Config
var store = Ext.create('Ext.data.JsonStore', {
storeId:'thisstore',
fields: [a,b,c,d],
autoLoad: true,
config : {
proxy: {
type: 'ajax',
url: 'Stores/ShowModule/showGrid.php',
simpleSortMode: true,
reader: {
type: 'json',
rootProperty: 'data',
totalProperty: 'total'
}
},
listeners:{
load:function(store){
console.log(store.getData());
}
}
}
});

Extjs 4 store sync issue

I have the following code:
The Store :
Ext.define('strClientesRECO', {
extend: 'Ext.data.Store',
model: 'mdlClientesRECO',
autoLoad: false,
proxy: {
type: 'ajax',
pageParam: undefined,
startParam: undefined,
limitParam: undefined,
api: {
read: 'some url',
create: 'some url',
update: 'some url',
},
reader: {
type: 'json',
root: 'data',
successProperty: 'success',
messageProperty: 'message'
},
writer: {
root: 'records',
encode: true,
writeAllFields: false
}
}
});
The grid has the following properties:
selModel: Ext.create('Ext.selection.CheckboxModel'),
selType: 'cellmodel',
plugins: [{
ptype: 'cellediting',
clicksToEdit: 2
}],
In the controller I have a listener to sync the store every cell update
Ext.ComponentQuery.query('viewGridClientesRECO')[0].getStore().addListener('update', this.onUpdateRecords, this);
onUpdateRecords: function() {
var storeGridClientes = Ext.ComponentQuery.query('viewGridClientesRECO')[0].getStore();
storeGridClientes.sync();
},
But in the PHP file that is executed when I update, if I print $_POST the records sends are correct, but it never sends the id of the edited row.

Extjs variable in proxy api store using crud

I would put a variable in my proxy api store (using crud) but it not work.
The api config is an object , could you use a variable in object in javascript ?
I use Sencha Architect and he format api...
Do you have a suggest ?
My base store :
Ext.define('ModuleGestion.store.Pays', {
extend: 'Ext.data.Store',
requires: [
'ModuleGestion.model.Pays'
],
constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
autoLoad: true,
model: 'ModuleGestion.model.Pays',
storeId: 'StorePays',
proxy: {
type: 'ajax',
api: {
create: 'http://visual04/ModuleGestion/php/Pays.php?action=create',
read: 'http://visual04/ModuleGestion/php/Pays.php?action=read',
update: 'http://visual04/ModuleGestion/php/Pays.php?action=update',
destroy: 'http://visual04/ModuleGestion/php/Pays.php?action=destroy'
},
reader: {
type: 'json',
root: 'data'
},
writer: {
type: 'json',
root: 'data'
}
}
}, cfg)]);
}
});
My model with variable in api proxy
var Url = 'http://visual04/ModuleGestion/php/';
var UrlPays = Url+'Pays.php';
Ext.define('ModuleGestion.store.Pays', {
extend: 'Ext.data.Store',
requires: [
'ModuleGestion.model.Pays'
],
constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
autoLoad: true,
model: 'ModuleGestion.model.Pays',
storeId: 'StorePays',
proxy: {
type: 'ajax',
api: '{\r\n create: UrlPays+'action=create',\r\n read: UrlPays+'action=read',\r\n update: UrlPays+'action=update',\r\n destroy: UrlPays+'action=destroy'\r\n}',
reader: {
type: 'json',
root: 'data'
},
writer: {
type: 'json',
root: 'data'
}
}
}, cfg)]);
}
});
You can use the UrlPays anywhere in your file, since you declared it in the global scope (i.e. outside of any function).
That should work if you fix this line:
api: '{\r\n create: UrlPays+'action=create',\r\n read: UrlPays+'action=read',\r\n update: UrlPays+'action=update',\r\n destroy: UrlPays+'action=destroy'\r\n}',
like this:
api: {
create: UrlPays + 'action=create',
read: UrlPays + 'action=read',
update: UrlPays + 'action=update',
destroy: UrlPays + 'action=destroy'
},
See the difference in the syntax highlighter?

kendo grid request null when update and sent type is create

I am using the Kendo UI Grid. Basically, what I want to do is bind the grid on remote data which are sent on json format and be able to edit it.
The Grid receive the data and display it as I want. However, when I want to edit a cell, the request received in the php file is null, and the type sent is "create" while I would like it to be an "update".
Here is the code used:
-the javascript contains the grid declaration
$(document).ready(function() {
var dataSource = new kendo.data.DataSource({
batch: true,
transport: {
read: {
dataType: "json",
type: "POST",
url: "testgrid.php?type=read"
},
update: {
dataType: "json",
type: "POST",
url: "testgrid.php?type=update"
},
create: {
dataType: "json",
type: "POST",
url: "testgrid.php?type=create"
},
destroy: {
dataType: "json",
type: "POST",
url: "testgrid.php?type=destroy"
}
},
schema: {
data: function(result) {
return result.Result || result.data;
},
model: {
id: "bbmindex",
fields: {
totalvente: {type: "number"}
}
},
total: function(result) {
return result.totalData || result.PageSize || result.length || 0;
}
}
});
var param = {
columns: [
{
field: "naturetravaux", title: "nature travaux"
},
{
field: "totalvente", title: "total vente"
}
],
selectable: 'multiplerows',
editable: true,
toolbar: ["create", "save", "cancel", "destroy"],
dataSource: dataSource
};
$("#grid").kendoGrid(param);
});
-the php script send the data to the dataSource
<?php
header('Content-Type: application/json');
$request = json_decode(file_get_contents('php://input'));
$type = $_GET['type'];
$result = null;
if ($type == 'create') {
some code
} else if ($type == 'read') {
some code which send the data
} else if ($type == 'update') {
some code
}
echo json_encode($result);
?>
any idea about what is wrong here?
Any help would be much appreciated,
thank you in Advance
Martin
If your server receives a create is because the idof the edited record is 0, null or undefined. Please check that you get from the server a field called bbmindex and is not 0
You expect the request to be posted as JSON however the Kendo DataSource doesn't do that by default. You need to use the parameterMap option and make it return JSON:
parameterMap: function(data, type) {
return kendo.stringify(data);
}

Access uploaded file in JSON encoded data

I've encoded my form data into JSON. This has been achieved by the following ExtJS store configuration:
Ext.define('XXX.store.Registration', {
extend: 'Ext.data.Store',
model: 'XXX.model.Registration',
autoLoad: true,
pageSize: 15,
autoLoad: {
start: 0,
limit: 15
},
proxy: {
type: 'ajax',
api: {
create: './server/registration/create.php',
read: './server/registration/get.php',
update: './server/registration/update.php',
destroy: './server/registration/destroy.php'
},
reader: {
type: 'json',
root: 'registrations',
successProperty: 'success'
},
writer: {
type: 'json',
writeAllFields: true,
encode: true,
root: 'registrations'
}
}
});
My server side code has been implemented in PHP. I can access the encoded form fields by using the field name as a key, as exemplified below:
$reg = $_REQUEST['registrations'];
$data = json_decode(stripslashes($reg));
$registerNum = $data->registerNum;
$folioNum = $data->folioNum;
One of the fields in my form is a fileuploadfield. How can I access the uploaded file from the uploaded JSON. Any assistance will be highly appreciated.

Categories