From 2847eeed782a42023e0272897cb86dbaf04f62c9 Mon Sep 17 00:00:00 2001 From: patrick Date: Sun, 6 Dec 2020 17:13:56 +1100 Subject: [PATCH] first ajax call done --- js/token.js | 6 ------ js/workspace.js | 41 +++++++++++++++++++++++++++++++++++++++++ member.php | 25 ++++++++++++++++++++++++- 3 files changed, 65 insertions(+), 7 deletions(-) delete mode 100644 js/token.js create mode 100644 js/workspace.js diff --git a/js/token.js b/js/token.js deleted file mode 100644 index 77a61c7..0000000 --- a/js/token.js +++ /dev/null @@ -1,6 +0,0 @@ -(function ($) { - $(function () { - $('#test').html("def"); - console.log("called"); - }); -})(jQuery); \ No newline at end of file diff --git a/js/workspace.js b/js/workspace.js new file mode 100644 index 0000000..f755674 --- /dev/null +++ b/js/workspace.js @@ -0,0 +1,41 @@ +(function ($) { + // http://davidwalsh.name/javascript-debounce-function + function debounce(func, wait, immediate) { + var timeout; + return function () { + var context = this, args = arguments; + var later = function () { + timeout = null; + if (!immediate) + func.apply(context, args); + }; + var callNow = immediate && !timeout; + clearTimeout(timeout); + timeout = setTimeout(later, wait); + if (callNow) + func.apply(context, args); + }; + }; + + /*______________________________________________________*/ + + $(function () { + $('#test').html(mm.display_name); + console.log(mm); + }); + + + $(document).on("click", "#step1", function(){ + $.post(mm.ajax_url, { // POST request + _ajax_nonce: mm.nonce, // nonce + action: "list_users", // action + client : 333, + }, function(response, status, xhr){ + alert(response); + }).fail(function(){ + alert('network error '); + }); + + }); + +})(jQuery); \ No newline at end of file diff --git a/member.php b/member.php index 9c64589..ab80289 100644 --- a/member.php +++ b/member.php @@ -29,6 +29,17 @@ class Member{ add_filter('rewrite_rules_array', array($this,'my_add_rewrite_rules')); // hook add_query_vars function into query_vars add_filter('query_vars', array($this,'add_query_vars')); + + // + $this->ajax_hook('list_users'); + } + + private function ajax_hook($code, $admin_only = false) + { + add_action("wp_ajax_$code", array($this,"ajax_$code" )); + if (!$admin_only) { + add_action("wp_ajax_nopriv_$code", array($this,"ajax_$code")); + } } public function shortcode_workspace($attrs) { @@ -73,7 +84,7 @@ class Member{ private function register_medal_js() { //wp_enqueue_style( 'mm', plugins_url('css/workspace.css', __FILE__)); - wp_enqueue_script('mm', plugins_url('js/token.js', __FILE__), array('jquery', 'jquery-ui-core')); + wp_enqueue_script('mm', plugins_url('js/workspace.js', __FILE__), array('jquery', 'jquery-ui-core')); wp_localize_script( 'mm', 'mm', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => $this->nonce, // It is common practice to comma after @@ -82,6 +93,18 @@ class Member{ ) ); } + + function ajax_list_users() + { + $response = array( + 'status' => 'success', + 'users' => [ + 1,2,3,4,5,6,7 + ], + ); + wp_send_json($response); + } + } $mm = new Member(); \ No newline at end of file