Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP: Rendering view after ajax

I am building an app in CakePHP and I have a jquery dialog window and every time the user opens it I want to perform a jquery request that will populate the content with the result of the request. I have an js file that is in the webroot/js folder, with the following script:

$.ajax({                    
    url:'/projects/getAssets',
    type:"POST",                                        
    data:assetData,
    //dataType:'text',
    update: '#assetManagerContent'
});

In my controller file (ProjectsController) I have the following function:

function getAssets($id = null) {
    // Fill select form field after Ajax request.
    if(!empty($this->data)){
        $this->set('assetsFilter',
        $this->Project->Asset->find('list',
            array(
                'conditions' => array(
                    'Asset.project_id' => '23'
                )
            )
        )
    );
    $this->render('elements/assets', 'ajax');
    }
}

And finally I have the view (elements/assets):

<?php $assetsFilter = $this->requestAction('projects/getAssets'); ?>
  <?php foreach($assetsFilter as $assetFilter): ?>
    <div class="assetManager-asset">
        <div class="thumb"></div>
        <div class="label-name"><?php echo $assetFilter['AssetType']['type'] ?></div>
        <div class="label-date"><?php echo $assetFilter['Asset']['layer'] ?></div>
        <?php //echo $assetFilter['Asset']['id'] ?>
    </div>
   <?php endforeach; ?>

When the user opens the dialog the ajax request is triggered but nothing seems to happen in the #assetManagerContent div.

I hope someone can tell me what I am doing wrong

like image 787
Ronny vdb Avatar asked Jan 18 '13 19:01

Ronny vdb


2 Answers

As far as I know, there is no update option in the jQuery ajax api. Instead, you should add the success callback and populate the data there:

$.ajax({                    
    url:'/projects/getAssets',
    type:"POST",                                        
    data:assetData,
    //dataType:'text',
    success: function(data) {
      $('#assetManagerContent').html(data);
    }
});
like image 154
jeremyharris Avatar answered Oct 30 '22 00:10

jeremyharris


There is no update-option, just as jeremyharris already pointed out.

If you only want to fill an element with HTML loaded via AJAX, you can also use $.load():

$('#assetManagerContent').load('/projects/getAssets', assetData);

It's basically a shorthand for the corresponding $.ajax() call, and will automatically issue a POST request if the data parameter is present.

See: http://api.jquery.com/load/

like image 42
lethal-guitar Avatar answered Oct 30 '22 02:10

lethal-guitar