I am posting this question because I have no idea what else to do. I have seen similar questions in Zend but the answers that have been accepted are simply not working for me.
Environment: Zend Framework 1.12 Jquery: 1.10.3 for jquery-ui.css, jquery-1.9.1.js and 1.10.3 jquery-ui.js
When I use the default functionality example of the jquery api documentation, everything is smooth, it works wonders. The problem comes when I place as source the url that connects to the action "automplete" of my controller. My controller does good, ir responds in json format, but the thing is, it responds as so:
{"json":[{"label":"John-1","value":"John"},{"label":"Jay","value":"Jay-1"},{"label":"Lusi","value":"Lusi-1"}]}
So you see, it places everything inside an array of 1 with key 'json', which is maddening, because Jquery doesn´t understand that, jquery only wants/needs/looks for the array with data. So I understand why it doesn´t work, but I don´t know how to go around it (I am new to php, zend and javascript).
For the controller to stop rendering viewscripts and layout and send the information passed to the view as json, I use the switchContext helper in the init() method:
public function init() {
$contextSwitch = $this->_helper->getHelper('contextSwitch');
$contextSwitch->addActionContext('autocomplete', 'json')
->initContext();
}
Here is the autocompleteAction:
public function autocompleteAction()
{
$autocomplete_data=array(
array('label'=>'John-1', 'value'=>'John'),
array('label'=>'Jay', 'value'=>'Jay-1'),
array('label'=>'Lusi', 'value'=>'Lusi-1')
);
//echo Zend_Json::encode($autocomplete_data);
$this->view->json= $autocomplete_data;
}
As you can see, the reason it puts "json" as key for the data of all the autocomplete information I was sent is the name of the variable passed to the view. (I tested it by changing the variable name form json to "lala" and when I accessed via the url the array's key was now "lala" as in {"lala":[{"label":"John-1","value":"John"}....
You can see I also tried using the json_encode() (which is what Zend_Json::encode() uses), and echoing that, and this is what gets sent:
[][{"label":"John-1","value":"John"},{"label":"Jay","value":"Jay-1"},{"label":"Lusi","value":"Lusi-1"}]
I don´t even know how to access the data with that.
Does anyone know how to simply send the array [{"label":"something", "value":"something"}...] just like that?
Or does anyone know how I could manipulate the response from "source:" so that it reads whatever my controllers responds as--> response['json'] ¿?
EDIT
Alright, I believe I am onto something. The api documentation for jquery has this example, and I believe it is doing what I want: manipulating the resposne from the source. So I tried to mimick it with the following:
$( "#profesor" ).autocomplete({
source:function(response ) {
$.ajax({
url: url,
dataType: "json",
success: function( data ) {
console.log(data);
response(data['json']);
}
});
},
minLength:1
});
When it gets to console.log(data), 'data' is the response my controller is sending (the one with the 'json' key in the array). Wonderful, so all I should do is set the response for the autocomplete to be data['json'], but once it gets to that line...the following exception comes up: "Uncaught type Error: object is not a function". What does that mean? That it can´t access the "response" function of the autcomplete?
FINISH EDIT thanks in advance!