Just want to say thank you for sharing this. Although I have been using extjs for some time now, I am far from being an expert. But anywayz, I think this Ext.Direct implementation is great!
Printable View
Just want to say thank you for sharing this. Although I have been using extjs for some time now, I am far from being an expert. But anywayz, I think this Ext.Direct implementation is great!
Do you have an example of how to use the authorize function? And how to send username and password encrypted to it?
In the form I have the api config option set to 2 functions: load (which successfully load a string into the field) and submit (where I get an error from my custom class, complaining about an empty object in the parameter).
Demo: http://extme.eu/DirectTest/
PHP Code:// the body part of the index.php
<body>
<script type="text/javascript" src="Numbers.php?javascript"></script>
<script type="text/javascript" src="form.js"></script>
</body>
PHP Code:<?php
// the class
require 'ExtDirect.php';
class Numbers {
public function validate($data) {
// $_POST is empty!
// error: 'Cannot use object of type stdClass as array'
$num = $data['num'];
$response = array('success' => true);
if (!is_numeric($num)) {
$response['success'] = false;
$response['errors'] = array(
'num'=>'Not a valid number.'
);
}
return $response;
}
public function load($id) {
return 'loaded: '.$id;
}
}
ExtDirect::provide('Numbers');
ExtDirect::$form_handlers = array( 'Numbers::validate');
?>
Obviously the class receives an empty array; where does that come from? Even the $_POST array is empty.PHP Code:// form.js
Ext.ns('Ext.php');
Ext.onReady(function () {
Ext.QuickTips.init();
Ext.php.callback = function (response, e) {
if (true === e.status) {
Ext.php.formExample.items.items[0].setValue(response);
}
};
Ext.php.formExample = new Ext.form.FormPanel({
title: 'Validation Form'
,monitorValid: true
,renderTo: Ext.getBody()
,padding: 10
,height:120
,width:300
,buttons:[{
text: 'Validate'
,handler: function(){
Ext.php.formExample.getForm().submit();
}
}]
,items: [{
fieldLabel: 'Enter a number'
,xtype: 'textfield'
,name: 'num' // also tried with id: 'num'
,msgTarget: 'side'
,allowBlank:false
}]
,api: {
load: Ext.php.Numbers.load('loadtest', Ext.php.callback)
,submit: Ext.php.Numbers.validate
}
});
});
In get_request_actions() (line 274) the foreach tries to access $rpc->$variable. Is this correct?
I tried with ExtDirect::$default_api_output = "javascript", ExtDirect::$instantiate_static = true, ExtDirect::$include_inherited_methods = true and almost every other config setting.PHP Code:$$variable = ( isset( $rpc->$variable ) ? $rpc->$variable : '' );
I would be grateful for a kick in the right direction.
// $_POST is empty!
// error: 'Cannot use object of type stdClass as array'
Doesn't that mean that you should be using:
?Code:$num = $data->num;
Regards,
Andy
Thanks Andy. If you run the demo with Firebug, you'll notice that the data is an empty object. The PHP class method should get a $_POST as an array, but also this is empty. Its puzzling. It's a simple form, it should submit simple data, only one field. In other test environment this form runs ok.
OK, you got me stumped so I had a play around.
Change:
toCode:ExtDirect::provide('Numbers');
ExtDirect::$form_handlers = array( 'Numbers::validate');
I think the provide has to go last!Code:ExtDirect::$form_handlers = array( 'Numbers::validate');
ExtDirect::provide('Numbers');
Also, check the comments here about "receiving parameters":
http://www.sencha.com/forum/showthre...078#post484078
This means your PHP function definition would be:
Hope that helps!Code:public function validate($num) {
Andy
Andy, that was it!
Your suggestion to switch the call to the ExtDirect methods did the trick.
The call to ExtDirect::provide() apparently has to be the last call in the sequence, which seems perfectly logical. I can't believe I missed that.
Thank you so much for your help!
Roland
No problem! Glad to help
Andy
Hello, are you planning any updates for ext 4.0? :-?
I tryed to solve the problems by myself on the "PR" and now on "BETA 1", but i could“t do it...
Any update will be most wellcome...
Hi J.Bruni,
i love your Ext.Direct implementation.
After playing around with SAKI's Ext Direct HTTP State Provider and realizing the Implementation of
examples/state/SessionProvider.js
I figured out, that this kind of implementation perfectly fits into your Direct Implementation.
That means, your implementation will also be able to save and restore component ui state information.
To make this working i added i modified the javascript api
i also added some code at the end ofCode:/**
* @return string JSON encoded array containing the full API declaration
*/
static public function get_api_javascript()
{
session_start();
//unset($_SESSION['extjsstate']);
if(!isset($_SESSION['extjsstate'])){
$_SESSION['extjsstate'] = array(
'sessionId'=>session_id()
);
}
$template = <<<JAVASCRIPT
Ext.namespace( '[%namespace%]' );
[%descriptor%] = [%actions%];
Ext.Direct.addProvider( [%descriptor%] );
Ext.appState = [%appstate%];
JAVASCRIPT;
$elements = array(
'[%actions%]' => self::get_api_json(),
'[%namespace%]' => ExtDirect::$namespace,
'[%descriptor%]' => ExtDirect::$descriptor,
'[%appstate%]' => json_encode($_SESSION['extjsstate'])
);
return strtr( $template, $elements );
}
protected function get_request_actions()
and that's it.Code:// handle state
session_start();
if(!isset($_SESSION['extjsstate'])){
$_SESSION['extjsstate'] = array(
'sessionId'=>session_id()
);
}
foreach($_COOKIE as $name=>$value){
// look for state cookies
if(strpos($name, 'ys-') === 0){
// store in session
$_SESSION['extjsstate'][substr($name, 3)] = urlencode($value);
// remove cookie
setCookie($name, '', time()-10000, '/');
}
}
The only thing to do is to initialize StateManager onReady
Now the API include contains a new variable holding the state infoCode:Ext.onReady(function() {
...
Ext.state.Manager.setProvider(
new Ext.state.SessionProvider({state: Ext.appState})
);
... init code
Ext.appState = {"sessionId":"9e7f3v1vbcrf5r45j3hb28g8p2","MyDirectoryGrid":"o%3Acolumns%3Da%25...."};
Now if something is changed inside ui, the next roundtrip through Ext.Driret Router the UI State will be catched from COOKIE and stores inside PHP Session (Serverside instead of Client Side implementation).
My kind of implementation is only for demo/testing. Maybe you can implement such feature via new toogle Property and callBack function (like auth) to make implementation of individually store containers for session data possible (maybe mySQL or SQLite).
I think, that would be a great addon to your fantastic implementation.
Cheers,
Holger