Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

SS3 Ajax simple example (wanted)


Go to End


3 Posts   4749 Views

Avatar
Spihex

Community Member, 5 Posts

3 September 2012 at 4:15pm

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)

Avatar
Willr

Forum Moderator, 5523 Posts

7 September 2012 at 10:41pm

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

Avatar
Spihex

Community Member, 5 Posts

8 September 2012 at 1:17am

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?