Custom Module AJAX URL

SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Goal : Custom Module POST via AJAX to correct URL
--
Searching thru JR code, we find many examples of .ajax - all seem to build .ajax URL in a similar manner:
    var url = core_system_url + '/' + jrProfile_url + '/get_pulse_counts/__ajax=1';
    $.ajax({
        type: 'GET',
        url: url,
        dataType: 'json',
        cache: false,
        success: function(n)
        {
            if (cb !== null && typeof cb == "function") {
                cb(n);
            }
        }
    });

Question:
1) Where do these values "core_system_url", "jrProfile_url" get set? Are similar vars available by default to custom modules?
var url = core_system_url + '/' + jrProfile_url + '/get_pulse_counts/__ajax=1';
2) In code above, how does JR Custom Module framework implement the last param "__ajax=1"
3) How do Custom Modules build the correct .ajax URL in JS?
--
This is not a general code question, we are fluent with php, ajax, js code, etc...
--
This question is about how to implement .ajax correctly inside the JR Custom Module framework. Specifically how to correctly set the .ajax URL in the JS of a custom module.

Please Advise...
updated by @softdesigns: 07/23/17 11:41:24AM
SteveX
SteveX
@ultrajam
7 years ago
2,584 posts
1. They are created when a module has a javascript file added in its init function
2. Just like your example, but replace jrProfile_url with sdModule_url (assuming your module is called sdModule)
3. You don't need to do anything except add the parameter so the router can do its job.
In your module javascript file you create your url before your ajax function:
var url = core_system_url + '/' + sdModule_url + '/my_custom_view/__ajax=1';

Create your view (named view_sdModule_my_custom_view) in your module index.php (look at view_jrProfile_get_pulse_counts in jrProfile index.php as an example). The view needs to encode and return its data using jrCore_json_response($_your_array) - just pass your array in and Jamroom does the rest.


--
¯\_(ツ)_/¯ Education, learning resources, TEL, AR/VR/MR, CC licensed content, panoramas, interactive narrative, sectional modules (like jrDocs), lunch at Uni of Bristol. Get in touch if you share my current interests or can suggest better :)
michael
@michael
7 years ago
7,714 posts
Javascript | Smarty
core_system_url = {$jamroom_url}
jrProfile_url = {jrCore_module_url module="jrProfile" assign="murl"}

Because any modules url can be changed in the ACP by the admin user, its a mistake to hardcode /audio for the audio module, instead use jrAudio_url

That will be available in any module that has a registered javascript file included. eg in the _int()
    jrCore_register_module_feature('jrCore', 'javascript', 'jrAudio', 'jrAudio.js');
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Cool - We want to make sure we are coding correctly for JR framework.
--
BTW - Smarty may not be the latest code trend, but Smarty seems powerful, clean, and fast - should work great for us...
--
Thanks to all replies :)
Need time to study further...
updated by @softdesigns: 04/24/17 08:27:34AM

Tags