0

I want to rewrite this jQuery Ajax Request which is not working well with cakePHP with a code block in my cakePHP view using JsHelper. So far, I've setup everything( included the helper in my controller, written the buffer in my Layout, included my library(jQuery) ), but I dont know how to rewrite this:

         $.ajax
            ({
                url: 'clients/loadJsonMarkers',
                accepts: 'json',
                type: 'POST',
                data: postData,
                dataType: 'json',
                error: function(xhr,status,err){
                    alert("DEBUG: status"+status+" \nError:"+err);
                },  
                success: function(transport)
                {   

                    var markers = new Array();

                    for(var i in transport.clients)
                    {
                        var latlng = transport.clients[i].Client.geoloc.replace("(", "");
                        latlng = latlng.replace(")", "");
                        latlng = latlng.split(',');

                        //console.debug(latlng);
                        markers.push(new google.maps.LatLng(parseFloat(latlng[0]),parseFloat(latlng[1])));
                    }

                    loadMap(markers);

                }
            });

What I've achieved to write so far was this:

    $this->Js->get('document');
    $this->Js->event('load',
            $this->Js->request(
                array('action' => 'loadJsonMarkers'),
                array('assync'=>TRUE, 'type'=>'json', 'method'=>'POST', 'data'=>$_POST)
                ),
            array('success'=>$this->Html->scriptBlock("
                    function(transport)
                    {   
                        var markers = new Array();

                        for(var i in transport.clients)
                        {
                            var latlng = transport.clients[i].Client.geoloc.replace('(', '');
                            latlng = latlng.replace('(', '');
                            latlng = latlng.split(',');

                            //console.debug(latlng);
                            markers.push(new google.maps.LatLng(parseFloat(latlng[0]),parseFloat(latlng[1])));
                        }

                        loadMap(markers);

                    }"
            ))
    );

But I feel like something is missing and I don't know if I should use this selector(document).

Pedro Tanaka
  • 48
  • 1
  • 7
  • Why not write it as regular JS since you already have it? It is [likely it will be removed](https://groups.google.com/d/topic/cakephp-core/UaXqk_rTdxE/discussion) in CakePHP 3.0 anyway. – jeremyharris Jan 25 '13 at 01:07
  • Man, I've tried to. I swear, this was a blind shot, a desperate attempt to get this thing working. Here is my first question, where my JS code is not working: http://stackoverflow.com/questions/14486895/cakephp-wont-work-with-ajax-jquery – Pedro Tanaka Jan 25 '13 at 15:21

1 Answers1

0

Since Cake buffers everything and places it in a $(document).ready() function, all you should need to do is add it to the buffer.

$request = $this->Js->request(
  array('action' => 'loadJsonMarkers'),
  array(
    'async' => true, 
    'type' => 'json', 
    'method' => 'POST', 
    'data' => $_POST,
    'wrapCallbacks' => false,
    'success' => "function(transport) {
      var markers = new Array();

      for(var i in transport.clients) {
        var latlng = transport.clients[i].Client.geoloc.replace('(', '');
        latlng = latlng.replace('(', '');
        latlng = latlng.split(',');

        markers.push(new google.maps.LatLng(parseFloat(latlng[0]),parseFloat(latlng[1])));
      }
      loadMap(markers);
    }"
  )  
);
$this->Js->buffer($request);

Make sure you're writing the buffer in your layout.

However, since you've already got the pure JS writting you should probably just buffer that, since the JsHelper will likely be removed in CakePHP 3.0.

jeremyharris
  • 7,884
  • 22
  • 31
  • Man, thanks for your help, but It still doesn't work. The code is executed, I can see in the debug, but nothing happens in the application, not even my map is plotted. – Pedro Tanaka Jan 25 '13 at 15:32
  • Man you wouldn't believe, but after 5 days trying to discover what the hell was going on with this code, I got it, it was a problem with my view, I should delete all the content that was inside of , I don't know why, but it is working now. – Pedro Tanaka Jan 25 '13 at 16:52
  • Just to let you know, I've found out that I was having a problem with my css and solved my problem with the tips on this page: http://stackoverflow.com/questions/9548330/zoom-bar-disfigured-in-google-maps-for-rails-gmaps4rails – Pedro Tanaka Jun 13 '13 at 17:42