Jump to:

23487 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » SS3 Ajax simple example (wanted)

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1926 Views
  • Spihex
    Avatar
    Community Member
    5 Posts

    SS3 Ajax simple example (wanted) Link to this post

    Hello!

    First, I would like to say thank you for your work with Silverstripe. Finally I found something functional, working and still clear enough that a novice was able to understand. Second, even though I am a novice, I would note that to realize almost any answer.
    I just have not found the right answers. Well, or just do not understand something :-D

    So. The main question concerns the order of execution Ajax in Silverstripe. I found this example [url=http://www.balbuss.com/ajax-with-jquery]http://www.balbuss.com/ajax-with-jquery, and even almost understand how works Form and Ajax. But the question remains:

    Some Blog function:

    function LatestBlog ($ num = 5) {
             $ news = DataObject :: get_one ("BlogHolder");
             return ($ news)? DataObject :: get ("BlogEntry", "ParentID = $ news-> ID", "ID DESC", "", $ num): false;
         }


    apparently, this is an old example

    It is clear that here we have the last five blog posts. But like I could get the data using Ajax? Say, by pressing the «Show more» (something like the search results are displayed on this site).

    (translated by online-translator)

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: SS3 Ajax simple example (wanted) Link to this post

    So. The main question concerns the order of execution Ajax in Silverstripe.

    An ajax request is the just another HTTP request, the execution order doesn't alter between Ajax, non ajax.

    Taking your blog example - to make it 'ajax' friendly you can do something like:

    function LatestBlog ($ num = 5) {
    $news = DataObject :: get_one ("BlogHolder");

    if($news) {
    if($this->isAjax()) {
    $start = (int) $_GET['start'];
    $articles = DataObject :: get ("BlogEntry", "ParentID = $ news-> ID", "ID DESC", "", "$start,$num"):
    return json_encode($articles->toArray());
    }
    return DataObject :: get ("BlogEntry", "ParentID = $ news-> ID", "ID DESC", "", $ num):
    }
    }

    Now accessing yoursite.com/Home/LatestBlog?start=5 via jquery $.getJSON will return you a json feed of all the articles. You may not want JSON however, and rather return a SSViewer template.

    I've added a ticket to document a simple ajax example - http://open.silverstripe.org/ticket/7848

  • Spihex
    Avatar
    Community Member
    5 Posts

    Re: SS3 Ajax simple example (wanted) Link to this post

    Oh, thanks for your help, Willr!

    "You may not want JSON however, and rather return a SSViewer template"
    By using RenderWith('some_template') in Init or Index function?

    1926 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.