Created
December 10, 2010 12:28
-
-
Save erikreagan/736150 to your computer and use it in GitHub Desktop.
Sample accessory with ajax method for EE 2.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
// sample code for thread http://expressionengine.com/forums/viewthread/174318/ | |
class Accessory_test_acc | |
{ | |
public $name = 'Test Accessory'; | |
public $id = 'accessory_test_acc'; | |
public $version = '1.0.0'; | |
public $description = 'Test bed for accessories'; | |
function Accessory_test_acc() | |
{ | |
$this->EE =& get_instance(); | |
} | |
public function set_sections() | |
{ | |
// Add some stuff to the head (you could alternatively load a js file from your add-on package) | |
// @see http://expressionengine.com/user_guide/development/usage/cp.html#add_to_head | |
// @see http://expressionengine.com/user_guide/development/usage/cp.html#load_package_js | |
$this->EE->cp->add_to_head(" | |
<script type='text/javascript'> | |
$(function() { | |
// .bind() documentation: http://api.jquery.com/bind/ | |
$('#my_ajax_link').bind('click',function(e){ | |
e.preventDefault(); | |
// $.load() documentation: http://api.jquery.com/load/ | |
$('#my_ajax_box').load($(this).attr('href'),function(){ | |
// $.dialog() documentation: http://jqueryui.com/demos/dialog/ | |
$('#my_ajax_box').dialog({ | |
modal: true, | |
title: 'Howdy', | |
buttons: { | |
Ok: function() { | |
$(this).dialog('close'); | |
} | |
} | |
}); | |
}); | |
}); | |
}); | |
</script> | |
"); | |
$this->sections['Test One'] = "<p><a id='my_ajax_link' href='".BASE.AMP."C=addons_accessories&M=process_request&accessory=accessory_test&method=process_ajax&id=232'>Click this to test the ajax return</a></p><div id='my_ajax_box'></div>"; | |
} | |
public function process_ajax() | |
{ | |
// Check to make sure it's an ajax request - if not we return a user error for this example | |
if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) | |
{ | |
// normall I'd build this in a view file using $this->EE->load->view('file',$data,TRUE) | |
// but I can't seem to load a view inside these accessory methods. See this for details: | |
// http://expressionengine.com/forums/viewthread/175547/ | |
// http://expressionengine.com/bug_tracker/bug/14741/ | |
$out = "<div style='margin-top:10px'>"; | |
$out .= "<p>This content is coming from within my <pre>process_ajax()</pre> method of the accessory.</p><br/>"; | |
$out .= "<ul>"; | |
// Simulate some data coming from the database or something - we'll just use a basic array | |
$array = array("one","two","three","four","five"); | |
foreach ($array as $result) { | |
$out .= "<li>{$result}</li>"; | |
} | |
$out .= "</ul>"; | |
$out .= "</div>"; | |
exit($out); | |
// exit(file_get_contents(dirname(__file__).'/views/entry_info.php')); | |
} else { | |
return $this->EE->output->show_user_error('general','Javascript is not enabled (apparently)'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment