Skip to content

Commit

Permalink
persisting feed in localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
siddii committed Feb 20, 2014
1 parent 40a1e67 commit 0b9c5f4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
5 changes: 5 additions & 0 deletions app/my-feeds.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ <h3 ng-click="toggleFeed();" class="panel-title">{{feed.title}}</h3>
</div>
</div>
</div>
<div ng-show="feeds.length == 0" class="col-sm-12">
<div class="alert text-center alert-warning fade in">
Sorry, nothing to show here :(
</div>
</div>
</div>
</div>

Expand Down
36 changes: 29 additions & 7 deletions app/scripts/my-feeds.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
angular.module('my-feeds', [
'feeds'
])
.controller('MyFeedsController', ['$scope', '$compile', '$http', '$rootScope', function ($scope, $compile, $http, $rootScope) {
.controller('MyFeedsController', ['$scope', '$compile', '$http', 'storage', function ($scope, $compile, $http, storage) {
$scope.isOrigin = function () {
var url = window.location.href.substring(window.location.href.lastIndexOf('/') + 1);
return window.parent.location.href.indexOf(url) !== -1;
};

$http.get('my-feeds.json').success(function (feeds) {
$scope.feeds = feeds;
});
$scope.$watch('feeds', function (newValue, oldValue) {
if (newValue) {
storage.set('feeds', newValue);
}
}, true);

if (!storage.get('feeds')) {
$http.get('my-feeds.json').success(function (feeds) {
$scope.feeds = feeds;
});
}
else {
$scope.feeds = storage.get('feeds');
}

}])
.directive('feedWidget', ['$compile', function ($compile) {
return {
Expand All @@ -26,12 +38,22 @@ angular.module('my-feeds', [
$scope.collapsed = !$scope.collapsed;
};

$scope.deleteFeed = function (){
if (confirm('Are you sure you want to delete this feed?') && $scope.feeds.indexOf($scope.feed) > -1) {
$scope.deleteFeed = function () {
if (confirm('Are you sure you want to delete this feed?') && $scope.feeds.indexOf($scope.feed) > -1) {
$scope.feeds.splice($scope.feeds.indexOf($scope.feed), 1);
}
}
}]);
}])
.factory('storage', function () {
return {
set: function (name, obj) {
localStorage[name] = angular.toJson(obj);
},
get: function (name) {
return angular.fromJson(localStorage[name]);
}
};
});

function feedPostRender(element) {
$(element).find('a').attr('target', '_blank');
Expand Down

0 comments on commit 0b9c5f4

Please sign in to comment.