Last active
August 29, 2015 14:02
-
-
Save myfashionhub/5f7f40f1e9a028e22f2a to your computer and use it in GitHub Desktop.
Circular reference in ArticleView render
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// REFACTOR 1 | |
function loadFeed(url) { | |
var feed = new google.feeds.Feed(url); | |
feed.setNumEntries(1); | |
feed.load(function(result) { | |
for (var i = 0; i < result.feed.entries.length; i++) { | |
var $article = $('<div>').addClass('article'); | |
var $date = $('<div>').addClass('date').append(result.feed.entries[i].publishedDate); | |
var $link = $('<a>').attr('href', result.feed.entries[i].link); | |
var $title = $('<h3>').append(result.feed.entries[i].title); | |
$title.wrapInner($link); | |
var $extract = $('<p>').addClass('extract').html(result.feed.entries[i].content); | |
$extract.children().last().remove(); | |
$article.append($date).append($title).append($extract); | |
$('.frontpage').append($article); | |
} | |
}); | |
return feed; | |
} | |
// ORIGINAL | |
function loadFeed(url) { | |
var feed = new google.feeds.Feed(url); | |
feed.setNumEntries(1); | |
feed.load(); | |
return feed; | |
} | |
function ArticleModel(obj) { | |
this.feed = obj; | |
// this.title = result.feed.entries[0].title; | |
// this.link = feed.entries[0].link; | |
// this.publishedDate = feed.entries[0].publishedDate; | |
// this.content = feed.entries[0].content; | |
} | |
function ArticleView(model){ | |
this.model = model; | |
this.el = undefined; | |
} | |
ArticleView.prototype.render = function(result) { | |
function aFunc(result) { | |
for (var i = 0; i < result.feed.entries.length; i++) { | |
var $article = $('<div>').addClass('article'); | |
var $date = $('<div>').addClass('date').append(result.feed.entries[i].publishedDate); | |
var $link = $('<a>').attr('href', result.feed.entries[i].link); | |
var $title = $('<h3>').append(result.feed.entries[i].title); | |
$title.wrapInner($link); | |
var $extract = $('<p>').addClass('extract').html(result.feed.entries[i].content); | |
$extract.children().last().remove(); | |
$article.append($date).append($title).append($extract); | |
$('.frontpage').append($article); | |
} | |
} | |
this.model.feed.load(aFunc); | |
console.log(this.model); | |
}; | |
function populateFrontpage() { | |
// Do this for every url in the interests | |
url = "http://www.npr.org/rss/rss.php?id=1001"; | |
var feedItem = loadFeed(url); | |
var articleModel = new ArticleModel(feedItem); | |
var articleView = new ArticleView(articleModel); | |
articleView.render(); | |
} | |
$(function(){ | |
populateFrontpage(); | |
}) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Leafthru</title> | |
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> | |
<script type="text/javascript" src="https://www.google.com/jsapi"></script> | |
<script type="text/javascript">google.load("feeds", "1");</script> | |
</head> | |
<body> | |
<h2>RSS feed</h2> | |
<div class="frontpage"></di | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment