Hi chaps
I'm new to Adapt.
I just wondered if there is any Javascript code examples for communicating with Moodle 2.9+ / Totara via AJAX from Adapt?
Moodle 2.9+ recommends using a webservice for AJAX : https://docs.moodle.org/dev/AJAX
So I have created a web service and created a Javascript AMD module, also recommended for Moodle 2.9+ :
https://docs.moodle.org/dev/Javascript_Modules
There is an example here : https://docs.moodle.org/dev/User:Damyon_Wiese/Ajax_Service
So for example, in /local/myplugin/classes/external.php, I have a method called get_hello_world that returns 'woohoo'.
So in /local/myplugin/amd/src/hello_world.js I have something like:
// Hello world module. define(['jquery', 'core/ajax'], function($, ajax) { return { get_hello_world: function() { var deferred = $.Deferred(); var ajaxrequests = [{ methodname: 'local_myplugin_get_hello_world', args: {} }]; var deferreds = ajax.call(ajaxrequests); $.when.apply(null, deferreds).done( function() { // External web service returns an array of objects
// So grab the first row.
var result = arguments[0];
// result.hello_world now contains 'woohoo' deferred.resolve(result); } ).fail( function(ex) { deferred.reject(ex); } ); return deferred.promise(); } }; });
Then to test this works, in /local/myplugin/amd/src/hello_world_test.js I have something like
define(['jquery', 'local_myplugin/hello_world'], function($, gethelloworld) { var component = { init: function() { gethelloworld.get_hello_world().done(function (result) { $('#testhelloworld').text(result.hello_world); // #testhelloworld now contains 'woohoo'. // How can I transfer this value to adapt? }); } }; return component; });
But I don't know how Adapt can pick up the value?
Cheers, Russ