使用javascript / jQuery在zend模型中调用php方法的最佳方法是什么
I'm developing a website where i have to communicate with web services. I’m using Ajax calls like this, for instance if I want to call a method called getCustomer(...) I have in my Zend project a module called "customer".
Inside of my controller, I have an action called "jsonAction"
public function jsonAction() {
$this->_helper->viewRenderer->setNoRender();
$server = new Zend_Json_Server();
$server->setClass('Customer_Model_Customer');
$server->handle();
exit;
}
Them in my model I have the function:
/**
* Get Customer
* @param string $customerNr
* @return object
*/
public function getCustomer($customerNr){
/*....*/
}
So I want to call this function using Ajax calls. What i did was something like this:
getCustomer : function(customerNr){
if(customerNr == null)
return;
var request = {};
request.method = "getCustomer";
request.params = {};
request.params.customerNr = customerNr;
request.id = Math.floor(Math.random()*101);
postObject(webServicesURL, JSON.stringify(request), successGetCustomer);
},
Where postObject it’s the Ajax function:
function postObject(url, request, successCallback){
try{
$.ajax({
type: "POST",
dataType: 'json',
url: url,
contentType: "application/x-www-form-urlencoded",
data: request,
xhrFields: {
withCredentials: true
},
success: function(data){
successCallback(data);
},
error: function (data) {
/*Notify error*/
}
});
} catch(ex){
/*erro*/
}
}
My question is, there is another way to do this? a best and elegant way? I'm new in web developing and that’s why i'm asking for your help.
Note: I'm using php Zend framework 1.12, Ember 1.0 and JQuery 1.8
A better approach could have been not to call a single action rather calling different action and let contextswitch
to expose various data type you can find a code snippet here
Here is an approach
//put this in your bootstrap
protected function _initContextSwitch()
{
// Initialize contextSwitch helper
Zend_Controller_Action_HelperBroker::addHelper(new Custom_Action_Helper_ContextSwitch());
}
class Custom_Action_Helper_ContextSwitch extends
Zend_Controller_Action_Helper_ContextSwitch
{
public function preDispatch()
{
$actionName = $this->getActionController()->getRequest()->getActionName();
$this
->addActionContext($actionName, 'json')
->initContext();
}
}
Now extend Zend_Controller_Action_Helper_ContextSwitch to make all action to expose json data
Now call every method with query string(format=json) like http://example.com/module/controller/action?format=json
this and will expose json data for every action
N.B Remember this is a custom action helper which namespace is Custom
If you're using Ember.js, then you should probably be using ember-data, Ember's data persistence component.
I suggest that you read the emberjs.com guides. Using Ember.js isn't something to be taken lightly and requires the adherence to conventions and standards. You need to significantly rethink your current approach.
ember-data implements a client-side RESTFUL API interface. Just like on the server, you should be specifying Models on the client. Ember.js is a framework, not a library. Use it!
You can also check if a request is ajax like this (in your controller):
$this->_request->isXmlHttpRequest()
With this you can call any action, do what is needed, and then, depending on the value you get from the request check, you either just let the view render or, in case of an AJAX call, print out a json string
public function dosomethingAction(){
// do something ...
$this->view->data = $data = array(
'param1' => 'v1'
'param2' => 'v2'
);
if($this->_request->isXmlHttpRequest()){
$this->getHelper('json')->sendJson($data);
}
}