Skip to content

Commit

Permalink
Part 8: Iterators and Filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Young committed Jun 13, 2013
1 parent 0dcc996 commit ff4d6a6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
27 changes: 26 additions & 1 deletion app/scripts/controllers/main.js
@@ -1,11 +1,35 @@
'use strict';

angular.module('djsreaderApp')
.controller('MainCtrl', function($scope, $http, $timeout) {
.controller('MainCtrl', function($scope, $http, $timeout, $filter) {
function storyInCollection(story) {
for (var i = 0; i < $scope.stories.length; i++) {
if ($scope.stories[i].id === story.id) {
return true;
}
}
return false;
}

function addStories(stories) {
var changed = false;
angular.forEach(stories, function(story, key) {
if (!storyInCollection(story)) {
$scope.stories.push(story);
changed = true;
}
});

if (changed) {
$scope.stories = $filter('orderBy')($scope.stories, 'date');
}
}

$scope.refreshInterval = 60;
$scope.feeds = [{
url: 'http://dailyjs.com/atom.xml'
}];
$scope.stories = [];

$scope.fetchFeed = function(feed) {
feed.items = [];
Expand All @@ -19,6 +43,7 @@ angular.module('djsreaderApp')
if (data.query.results) {
feed.items = data.query.results.entry;
}
addStories(feed.items);
}).
error(function(data) {
console.error('Error fetching feed:', data);
Expand Down
10 changes: 10 additions & 0 deletions app/views/main.html
@@ -1,5 +1,15 @@
<h1>djsreader</h1>

<p>Refresh (seconds): <input ng-model="refreshInterval"></p>

<h2>All Stories</h2>

<ul>
<li ng-repeat="item in stories"><a href="{{ item.link.href }}">{{ item.title }}</a></li>
</ul>

<hr />

<h3>Add Another Feed</h3>

<form name="newFeed" novalidate>
Expand Down

0 comments on commit ff4d6a6

Please sign in to comment.