Mustache Renderer Javascript/Ajax

Re: Mustache Renderer Javascript/Ajax

by Mathieu Domingo -
Number of replies: 5
I think i walk one step closer to the solution : regroup end of part 4.1 with 4.2 to get this :
$.post(
    M.cfg.wwwroot + '/blocks/course_overview/change_visibility.php',
    data
    ).done(function(new_data){
        //step 2 : do something to re render the template now the course visibility have change (any way to change only the line? how it's done, look like it will change the full block)
        //should look something like that I think, but I have no idea how I'm supposed to get the "new_data" ...
        templates.render('block_course_overview/main', new_data).done(function(html,js){
        $('[data-region="course-overview"]').replaceWith(html);
        templates.runTemplateJS(js);
     });
});

For test purpose, at the end of step 5,  I added
echo json_encode(array("test"=>"return"));

It's working, the course list go away. Now I need to find how I can fill this array to get the list with new values.

I suppose I need to call the public function export_for_template(renderer_base $output) of file classes/output/main.php but i don't understand how to do it / how it's working..
Is there a way to get an already instantiated object to call this method ? Or should I instantiate a new one ?
What is the renderer_base $output variable ?
In reply to Mathieu Domingo

Re: Mustache Renderer Javascript/Ajax

by Mathieu Domingo -

After becoming crazy many times reading/trying, i finally made another step after looking more carefully the html code...

So, in javascript file, in var data , to get the instanceid of the block, i added this :
block_instanceid :$('[data-block="course_overview"]').data('instanceid')

When i call my php file with ajax, to get this value and then get the instance of this object :
$block_instanceid = required_param('block_instanceid', PARAM_INT);
$blockinstance= block_instance_by_id($block_instanceid);

(it's so easy when it's written but it took me so long to write this 3 lines -_- )


This allow me to call function get_content()
$blockinstance->get_content();

The problem is this function already call a renderer and return me the text already rendered... So i can't give it to  templates.render in my javascript file ..
At this step, if I return it to the javascript, like this :
echo json_encode($blockinstance->get_content());

I can do this in the javascript file :
templates.render('block_course_overview/main',new Object()).done(function(html,js){
    $('[data-region="course-overview"]').replaceWith(new_data.text);
    templates.runTemplateJS(js);
});

It's "working", but it doesn't look like the normal code like in the docs (seems a bit hacky i don't like it)

Looking more closely the code of get_content(), it's seems i need to change just the end of the function to get the return I need.

I tried to make another function and change the end of get_content like this :
$renderer = $this->page->get_renderer('block_course_overview');
$main = new block_course_overview\output\main($config, $tabs, $isediting, $tab, $sortorder, $favourites);
return $main->export_for_template($renderer);

Then I call it like this in my php ajax_script :

$test= $blockinstance->get_content_to_render();

The problem is $test contain all the good values expect all the links , which are all empty..

I tried to look values in process_tab, it's look ok, but if I look in export_for_template it's not.

If anyone have any idea of what happens, thanks to say me because i don't have any :'(

(If it's not clear, I actually updated the github fork )

In reply to Mathieu Domingo

Re: Mustache Renderer Javascript/Ajax

by Mathieu Domingo -

Finally used the "hacky version" .. 

Seems to work nicely smile  (Can change visiblity and favourite course without reloading , and it also work if javascript is deactivated)

In reply to Mathieu Domingo

Re: Mustache Renderer Javascript/Ajax

by Jean-Roch Meurisse -
Picture of Core developers Picture of Plugin developers Picture of Testers

Hi Mathieu,

Which moodle version are you using? Which theme? 

Be careful with course_overview block. I think it will be removed from core in 3.7 (but maybe I am wrong).

It is clearly possible to add that "show/hide" function in myoverview block, even if it's a little more tricky with 3.6 than with 3.5. 

So if you want to implement that in myoverview rather than course_overview, come back to me (in French if you like wink ) and I'll give you hints



In reply to Jean-Roch Meurisse

Re: Mustache Renderer Javascript/Ajax

by Mathieu Domingo -

Hi JR

I'm using Moodle 3.5 with theme adaptable.

If I have understand well, they have already removed it from the core (that's why now it's a plugin), and it probably going to stop working in 3.7
The problem is our users really don't like the "new" (introduced in 3.3 ?) myoverview so we kept the "old" one while we can and we will change when it's will no longer work.

From my point of view, It's "finished" now, everything seems to work nicely (with and without javascript)
To make it really clean, I should have try to change more code and i don't have enough time for it now (and this code is probably going to garbage in something like 1 or 2 year, so it's not worth to spend more time on this just to go around an "hacky" step (a working one actually))

In reply to Jean-Roch Meurisse

Re: Mustache Renderer Javascript/Ajax

by Ângela de Araújo -

Hi Jean-Roch,

I'd like to change the block "myoverview", to add a new sorting list by categories of courses.

Could you give me some hints on how to make this change?