Sample accessory with ajax method for EE 2.x


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. // sample code for thread http://expressionengine.com/forums/viewthread/174318/
  3. class Accessory_test_acc
  4. {
  5.  
  6. public $name = 'Test Accessory';
  7. public $id = 'accessory_test_acc';
  8. public $version = '1.0.0';
  9. public $description = 'Test bed for accessories';
  10.  
  11. function Accessory_test_acc()
  12. {
  13. $this->EE =& get_instance();
  14. }
  15.  
  16. public function set_sections()
  17. {
  18. // Add some stuff to the head (you could alternatively load a js file from your add-on package)
  19. // @see http://expressionengine.com/user_guide/development/usage/cp.html#add_to_head
  20. // @see http://expressionengine.com/user_guide/development/usage/cp.html#load_package_js
  21. $this->EE->cp->add_to_head("
  22. <script type='text/javascript'>
  23. $(function() {
  24. // .bind() documentation: http://api.jquery.com/bind/
  25. $('#my_ajax_link').bind('click',function(e){
  26. e.preventDefault();
  27. // $.load() documentation: http://api.jquery.com/load/
  28. $('#my_ajax_box').load($(this).attr('href'),function(){
  29. // $.dialog() documentation: http://jqueryui.com/demos/dialog/
  30. $('#my_ajax_box').dialog({
  31. modal: true,
  32. title: 'Howdy',
  33. buttons: {
  34. Ok: function() {
  35. $(this).dialog('close');
  36. }
  37. }
  38. });
  39. });
  40. });
  41. });
  42. </script>
  43. ");
  44.  
  45. $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>";
  46.  
  47. }
  48.  
  49. public function process_ajax()
  50. {
  51. // Check to make sure it's an ajax request - if not we return a user error for this example
  52. if (isset($_SERVER['HTTP_X_REQUESTED_WITH']))
  53. {
  54. // normall I'd build this in a view file using $this->EE->load->view('file',$data,TRUE)
  55. // but I can't seem to load a view inside these accessory methods. See this for details:
  56. // http://expressionengine.com/forums/viewthread/175547/
  57. // http://expressionengine.com/bug_tracker/bug/14741/
  58.  
  59. $out = "<div style='margin-top:10px'>";
  60. $out .= "<p>This content is coming from within my <pre>process_ajax()</pre> method of the accessory.</p><br/>";
  61. $out .= "<ul>";
  62. // Simulate some data coming from the database or something - we'll just use a basic array
  63. $array = array("one","two","three","four","five");
  64. foreach ($array as $result) {
  65. $out .= "<li>{$result}</li>";
  66. }
  67. $out .= "</ul>";
  68. $out .= "</div>";
  69. exit($out);
  70. // exit(file_get_contents(dirname(__file__).'/views/entry_info.php'));
  71. } else {
  72. return $this->EE->output->show_user_error('general','Javascript is not enabled (apparently)');
  73. }
  74. }
  75.  
  76. }

URL: https://gist.github.com/736150

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.