需要帮助来解决我的Ionic App中的缓存问题。将存储中保存的条目发布到离线视图。
我有一个带缓存的示例Controller:
.controller('PostCtrl', function($scope, $stateParams, DataLoader, $ionicLoading, $rootScope, $sce, CacheFactory, $log, Bookmark, $timeout ) {
if ( ! CacheFactory.get('postCache') ) {
CacheFactory.createCache('postCache');
}
var postCache = CacheFactory.get( 'postCache' );
$scope.itemID = $stateParams.postId;
var singlePostApi = $rootScope.url + 'posts/' + $scope.itemID;
$scope.loadPost = function() {
// Fetch remote post
$ionicLoading.show({
noBackdrop: true
});
DataLoader.get( singlePostApi ).then(function(response) {
$scope.post = response.data;
$log.debug($scope.post);
// Don't strip post html
$scope.content = $sce.trustAsHtml(response.data.content.rendered);
// $scope.comments = $scope.post._embedded['replies'][0];
// add post to our cache
postCache.put( response.data.id, response.data );
$ionicLoading.hide();
}, function(response) {
$log.error('error', response);
$ionicLoading.hide();
});
}
if( !postCache.get( $scope.itemID ) ) {
// Item is not in cache, go get it
$scope.loadPost();
} else {
// Item exists, use cached item
$scope.post = postCache.get( $scope.itemID );
$scope.content = $sce.trustAsHtml( $scope.post.content.rendered );
// $scope.comments = $scope.post._embedded['replies'][0];
}
// Sharing
$scope.sharePost = function( link ){
window.plugins.socialsharing.share('Check this post here: ', null, null, link);
}
// Bookmarking
$scope.bookmarked = Bookmark.check( $scope.itemID );
$scope.bookmarkItem = function( id ) {
if( $scope.bookmarked ) {
Bookmark.remove( id );
$scope.bookmarked = false;
} else {
Bookmark.set( id );
$scope.bookmarked = true;
}
}
// Pull to refresh
$scope.doRefresh = function() {
$timeout( function() {
$scope.loadPost();
//Stop the ion-refresher from spinning
$scope.$broadcast('scroll.refreshComplete');
}, 1000);
};
})
并且没有兑现控制器:
.controller('PostsCtrl', function( $scope, $http, DataLoader, $ionicLoading, $timeout, CacheFactory, $ionicSlideBoxDelegate, $rootScope, $log) {
var postsApi = $rootScope.url + 'posts';
$scope.moreItems = false;
$scope.loadPosts = function() {
$ionicLoading.show({
noBackdrop: true
});
// Get all of our posts
DataLoader.get( postsApi ).then(function(response) {
$scope.posts = response.data;
$scope.moreItems = true;
$log.log(postsApi, response.data);
$ionicLoading.hide();
}, function(response) {
$ionicLoading.hide();
$log.log(postsApi, response.data);
});
}
// Load posts on page load
$scope.loadPosts();
paged = 2;
// Load more (infinite scroll)
$scope.loadMore = function() {
if( !$scope.moreItems ) {
return;
}
var pg = paged++;
$log.log('loadMore ' + pg );
$timeout(function() {
DataLoader.get( postsApi + '?page=' + pg ).then(function(response) {
angular.forEach( response.data, function( value, key ) {
$scope.posts.push(value);
});
if( response.data.length <= 0 ) {
$scope.moreItems = false;
}
}, function(response) {
$scope.moreItems = false;
$log.error(response);
});
$scope.$broadcast('scroll.infiniteScrollComplete');
$scope.$broadcast('scroll.resize');
}, 1000);
}
$scope.moreDataExists = function() {
return $scope.moreItems;
}
// Pull to refresh
$scope.doRefresh = function() {
$timeout( function() {
$scope.loadPosts();
//Stop the ion-refresher from spinning
$scope.$broadcast('scroll.refreshComplete');
}, 1000);
};
})
.controller('CategoriesCtrl', function( $scope, $http, DataLoader, $ionicLoading, $timeout, CacheFactory, $ionicSlideBoxDelegate, $rootScope, $log ) {
var categoriesApi = $rootScope.url + 'categories';
$scope.moreItems = false;
$scope.loadCategories = function() {
$ionicLoading.show({
noBackdrop: true
});
// Get all of our categories
DataLoader.get( categoriesApi ).then(function(response) {
$scope.categories = response.data;
$scope.moreItems = true;
$log.log(categoriesApi, response.data);
$ionicLoading.hide();
}, function(response) {
$log.log(categoriesApi, response.data);
$ionicLoading.hide();
});
}
// Load posts on page load
$scope.loadCategories();
paged = 2;
// Load more (infinite scroll)
$scope.loadMore = function() {
if( !$scope.moreItems ) {
return;
}
var pg = paged++;
$log.log('loadMore ' + pg );
$timeout(function() {
DataLoader.get( categoriesApi + '?page=' + pg ).then(function(response) {
angular.forEach( response.data, function( value, key ) {
$scope.categories.push(value);
});
if( response.data.length <= 0 ) {
$scope.moreItems = false;
}
}, function(response) {
$scope.moreItems = false;
$log.error(response);
});
$scope.$broadcast('scroll.infiniteScrollComplete');
$scope.$broadcast('scroll.resize');
}, 1000);
}
$scope.moreDataExists = function() {
return $scope.moreItems;
}
// Pull to refresh
$scope.doRefresh = function() {
$timeout( function() {
$scope.loadCategories();
//Stop the ion-refresher from spinning
$scope.$broadcast('scroll.refreshComplete');
}, 1000);
};
})
如何向帖子控制器添加缓存? 这是缓存工厂:
.factory('Bookmark', function( CacheFactory ) {
if ( ! CacheFactory.get('bookmarkCache') ) {
CacheFactory.createCache('bookmarkCache');
}
var bookmarkCache = CacheFactory.get( 'bookmarkCache' );
return {
set: function(id) {
bookmarkCache.put( id, 'bookmarked' );
window.plugins.toast.showShortCenter(
"Bookmarked", function(a){}, function(b){}
);
},
get: function(id) {
bookmarkCache.get( id );
console.log( id );
},
check: function(id) {
var keys = bookmarkCache.keys();
var index = keys.indexOf(id);
if(index >= 0) {
return true;
} else {
return false;
}
},
remove: function(id) {
bookmarkCache.remove(id);
window.plugins.toast.showShortCenter(
"Removed", function(a){}, function(b){}
);
}
}
})
.factory('$localstorage', ['$window', function($window) {
return {
set: function(key, value) {
$window.localStorage[key] = value;
},
get: function(key, defaultValue) {
return $window.localStorage[key] || defaultValue;
},
setObject: function(key, value) {
$window.localStorage[key] = JSON.stringify(value);
},
getObject: function(key) {
return JSON.parse($window.localStorage[key] || '{}');
}
}
}]);
答案 0 :(得分:1)
而不是goodVoteLayer.bindPopup('<object data="http://www.youtube.com/embed/W7qWa52k-nE" width="560" height="315"></object>', {
maxWidth : 560
});
使用CacheFactory
。
文档here