I have a JSON file with the below data (just an example of what I have) and I am using this data with handlebars.js to create templates.
The issue I am having is that I need to show 15 stories (in their on divs) on page load however on every page refresh I want the stories to be in different positions on the page.
I guess my simplest question is - how do I randomise each item and still only show 15 stories?
Here is the example of my JSON file (but imagine with over 20 stories)
{
"stories": [{
"realTime": [{
"id": "realTime",
"icon": "link",
"info": {
"title": "Video box",
"type": "Link",
"description": "Lorem ipsum dolor ... elit nonummy quis",
"linkCopy": "Read more",
"link": "http://twitter.com"
}
}, {
"id": "realWorld",
"icon": "file-text",
"info": {
"title": "PDF box",
"type": "PDF",
"description": "Lorem ipsum dolor ... elit nonummy quis",
"linkCopy": "Read more",
"link": "http://www.google.co.uk"
}
}, {
"id": "realResults",
"icon": "comments",
"info": {
"title": "Blog here",
"type": "Blog",
"description": "Lorem ipsum dolor ... elit nonummy quis",
"linkCopy": "Read more",
"link": "http://www.google.co.uk"
}
}, {
"id": "realTime",
"icon": "comments",
"info": {
"title": "Blog here",
"type": "Blog",
"description": "Lorem ipsum dolor ... elit nonummy quis",
"linkCopy": "Read more",
"link": "http://www.google.co.uk"
}
}]
}]
}
Here is the jQuery I am using to retrieve the JSON data along with the handlebars.js compile code.
$.getJSON('./assets/json/stories.json', function(data) {
var source = $("#block-template").html();
var template = Handlebars.compile(source);
$('#block-div').html(template(data));
});