1
votes

I need some help adapting this simple script in PHP that work well to the Silverstripe MVC system.

<script  type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>

<script type="text/javascript">

jQuery(document).ready(function() {
    var refreshId = setInterval(function() {
        jQuery('#load').fadeOut("fast", function () {
            jQuery(this).load("reload.php", function () {
                jQuery(this).fadeIn("fast");
            });
        });
    }, 1000 * 15);
});

</script>

My main problem is how to load a part code to a template and not a full page content. In this example, reload.php is only a part of code. But in Silverstripe, I need render a part of code to a template and retreive it from a controller.

Is there someone know how to do that? Thank you!

1
You'll need an action in your Controller that renders the part you want. You can also reuse a possible existing acition with Director::is_ajax(). See also. ajax-silverstripe.mmkdigital.com/# - wmk
nice! Not exactly what I need, but useful to know how pass code from controller to a template. thanks! - StefGuev
Well, your question is somehow too broad for a better answer. If you tell us more about the silverstripe part of the question and what code you already tried we could help more... - wmk
The link you show me resolve my problem. I've look the code and I have build my code to my need... Do you want to see the codes if you can do a better programming? - StefGuev
If you think i could make it better ;) - wmk

1 Answers

0
votes

I don't know if you have a better idea to do this, but there is mine! It work well for my usage. I'm a junior coder ;)

routes.yml

Director:
  rules:
    'ajax//$Action/$ID': 'AjaxController'

ajaxcontroller.php

class AjaxController extends Controller {

    private static $allowed_actions = array(
        'news'
    );

    public function news(){

        $News = News::get()->sort('Sort',DESC);

        if (Director::is_ajax()){
                // Ajax!
                return $this->renderWith('AjaxInclude', array('News' => $News) );
            } else {
                // Not ajax!
                //return print_r($Pages);
        }
    }
}

includes/AjaxIncludes.ss

<% loop News %>
    <p>$Title</p>
<% end_loop %>

HomePage.php or other pages

class HomePage_Controller extends Page_Controller {

    public function init() {
        parent::init();

        Requirements::javascript($this->ThemeDir()."/javascript/ajax.js");
    }
}

ajax.js

jQuery.noConflict();

(function($) {
    $(document).ready(function() {

        var refreshId = setInterval(function() {    


            $.ajax({
                url: "ajax/news",
                cache: false,
                dataType: "html",
                success: function(data) {
                    if(!data){
                        $("#responsecontainer").removeClass('online');  
                        $("#responsecontainer").addClass('offline');    
                        $("#responsecontainer").fadeOut();  
                        //alert("offline");
                        $("#responsecontainer").html(data);
                    } else {
                        $("#responsecontainer").removeClass('offline'); 
                        $("#responsecontainer").addClass('online'); 
                        //alert("online");
                        $("#responsecontainer").html(data);
                    }
                }
            });
        }, 1000 * 5);
    });
}(jQuery));

Finaly, ID responsecontainer must be in HomePage.ss to be activated or desactivate