afdxs ('-' * 5)
A Pen by Siôn J. Lewis on CodePen.
afdxs ('-' * 5)
A Pen by Siôn J. Lewis on CodePen.
<div id="header"> | |
<h2>RSS Cross-Origin Request Test</h2> | |
</div> | |
<div id="content"> | |
<ul id="rss-feed"> | |
</ul> | |
<span id="error"></span> | |
</div> | |
<div id="footer" class="hide"> | |
<br/> | |
<a href="http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api" target="_blank">Enabling Cross-Origin Requests in ASP.NET Web API</a> | |
<br/> | |
<br/> | |
<div> |
var SJL = SJL || {}; | |
SJL.RSSFeed = SJL.RSSFeed || {}; | |
SJL.RSSFeed.Model = SJL.RSSFeed.Model || {}; | |
SJL.RSSFeed.Controller = SJL.RSSFeed.Controller || {}; | |
SJL.RSSFeed.Util = SJL.RSSFeed.Util || {}; | |
SJL.RSSFeed.Model = function () { | |
var self = {}; | |
self.item = function (title, link, description, shortDesc, source, author, pubDate) { | |
this.title = title; | |
this.link = link; | |
this.description = description; | |
this.shortDesc = shortDesc; | |
this.source = source; | |
this.author = author; | |
this.pubDate = pubDate; | |
}; | |
return self; | |
}(); | |
SJL.RSSFeed = function () { | |
var self = {}; | |
var model = SJL.RSSFeed.Model; | |
//var util = SJL.RSSFeed.Util; | |
self.url = "http://arupdw-dev-cdn.azurewebsites.net/API/RSSNewsPod"; | |
self.getRssPluginFormated = function () { | |
$.ajax({ | |
type: 'get', | |
url: self.url | |
}).done(function (data) { | |
var xml = data.firstChild; | |
var items = []; | |
$(xml).find("item").each(function () { | |
var el = $(this); | |
var title = el.find("title").text(); | |
var link = el.find("link").text(); | |
var description = el.find("description").text(); | |
var shortDesc = SJL.RSSFeed.Util.trimText(description, 110); | |
var source = el.find("source").text(); | |
var author = el.find("author").text(); | |
var pubDate = el.find("pubDate").text(); | |
items.push(new model.item(title, link, description, shortDesc, source, author, pubDate)); | |
}); | |
self.renderRSS(items); | |
//$('#content').text(items); | |
}).error(function (jqXHR, textStatus, errorThrown) { | |
$('#error').text(jqXHR.responseText || textStatus); | |
}); | |
}; | |
// Functions for View or controller ns... | |
self.rssClick = function(id) { | |
$('#rssBody' + id).toggle("fast", function() {}); | |
}; | |
self.renderRSS = function (items) { | |
$.each(items, function (index, item) { | |
self.renderFeed(item, index); | |
}); | |
}; | |
self.renderFeed = function (item, index) { | |
var html = $('#rss-feed').html(); | |
html += '<div id="title-'+ index + '">' + item.title + '</div>'; | |
html += '<div id="link-'+ index + '">' + item.link + '</div>'; | |
html += '<div id="desc-'+ index + '">' + item.description + '</div>'; | |
html += '<div id="short-desc'+ index + '">' + item.shortDesc + '</div>'; | |
html += '<div id="source-'+ index + '">' + item.source + '</div>'; | |
//html += '<div id="author-'+ index + '">' + item.author + '</div>'; | |
html += '<div id="pubDate-'+ index + '">' + item.pubDate + '</div>'; | |
html += '<hr/>'; | |
$('#rss-feed').html(html); | |
}; | |
return self; | |
}(); | |
SJL.RSSFeed.Util = function () { | |
var self = {}; | |
self.trimText = function (text, len) { | |
// Take into account the '...'. | |
len = len - 3; | |
if (text != undefined && text.length > len) { | |
var tmp = jQuery.trim(text).substring(0, len).split(" ") | |
var text = tmp.slice(0, tmp.length - 1).join(" ") + "..."; | |
} | |
// And replace any rogue commas. | |
return text = text.replace(',...', '...').replace('-...', '...'); | |
} | |
return self; | |
}(); | |
$(function () { | |
SJL.RSSFeed.getRssPluginFormated(); | |
}); |
div { | |
border-style: solid; | |
border-width: 1px; | |
} | |
.hide { | |
border-style: none; | |
display:none; | |
} | |
.show { | |
border-style: none; | |
display:block; | |
} |