اشتراک در خبرنامه‌ دارکوب وب

با عضویت در خبرنامه‌ی دارکوب وب مقالات و آموزش‌ها هر هفته به ایمیل شما ارسال می‌شود.

ساخت نرم افزار موبایل با استفاده از Ionic + Cordova برای ورد پرس قسمت هفتم

ساخت Favorite Post :

خوب وقت آن است کنترولر favoriteController را ایجاد کنیم ، هم چنین باید در menuControllerفانکشن favorite Post پست را اضافه کنم هم در این قسمت و هم در menuController.html

angular.module('starter')
//Menu Controller7-8-
 .controller('menuController', function ($scope, $http, $sce) {


 $scope.categories = [];
 $http.get("http://darkoobweb.com/api/get_category_index/")
 .then(
 function (dataReceive) {
 $scope.categories = dataReceive.data.categories;
 //for trust html
 $scope.categories.forEach(function (element) {
 element.title = $sce.trustAsHtml(element.title);
 });
 //end
 console.log(dataReceive);
 },
 function (errReceive) {
 console.log(errReceive);
 });
 })
 //Posts Controller on the Menu
 .controller('postsController', function ($scope, $http, $sce, $ionicScrollDelegate, $localStorage) {
 //function doRefresh for refresh the page and get new posts
 $scope.favorites = $localStorage.favorites;
 if (!$scope.favorites) {
 $scope.favorites = [];
 }
 $scope.doRefresh = function () {
 $scope.recent_posts = [];
 $http.get("http://darkoobweb.com/api/get_posts/")
 .then(
 function (dataReceive) {
 $scope.recent_posts = dataReceive.data.posts;
 $scope.recent_posts.forEach(function (element) {
 element.title = $sce.trustAsHtml(element.title);
 element.excerpt = $sce.trustAsHtml(element.excerpt.substr(0, 100) + " ادامه ... ");
 if ($scope.favorites.indexOf(element.id) != -1) {
 element.isFavorite = true;
 } else {
 element.isFavorite = false;
 }
 });
 $scope.$broadcast('scroll.refreshComplete');
 }, function (errReceive) {
 console.log(errReceive);
 });
 };
 $scope.recent_posts = [];
 $scope.offset = 0;
 $scope.countTotal = [];

 $http.get("http://darkoobweb.com/api/get_posts/")
 .then(
 function (dataReceive) {
 $scope.recent_posts = dataReceive.data.posts;
 $scope.recent_posts.forEach(function (element) {
 element.title = $sce.trustAsHtml(element.title);
 element.excerpt = $sce.trustAsHtml(element.excerpt.substr(0, 100) + " ادامه ... ");
 });
 }, function (errReceive) {
 console.log(errReceive);
 });
 /*Functions canLoadMore & loadMore for get more posts
 * loadMore Function if offset number bigger than my tootalCount then return false*/
 $scope.canLoadMore = function () {
 return $scope.offset <= $scope.countTotal;
 };
 /*loadMore function use the address site with offset , and get posts 10 per 10*/
 $scope.loadMore = function () {
 $http.get("http://darkoobweb.com/api/get_posts/?offset=" + $scope.offset)
 .then(function (dataReceive) {
 var newPosts = dataReceive.data.posts;
 $scope.countTotal = dataReceive.data.count_total;

 newPosts.forEach(function (element) {
 element.title = $sce.trustAsHtml(element.title);
 element.excerpt = $sce.trustAsHtml(element.excerpt.substr(0, 100) + " ادامه ... ");
 });
 $scope.recent_posts.push.apply($scope.recent_posts, newPosts);
 $scope.$broadcast("scroll.infiniteScrollComplete");
 $scope.offset += 10;

 }, function (errReceive) {
 console.log(errReceive);
 })
 };

 $scope.searchTitleChange = function () {
 $ionicScrollDelegate.$getByHandle('mainScroll').scrollTop(true);
 };
 $scope.favoritePost = function (post) {

 post.isFavorite = !post.isFavorite;

 if (post.isFavorite == true) {
 $scope.favorites.push(post.id);
 } else {
 $scope.favorites.forEach(function (element, index) {
 if (element == post.id) {
 $scope.favorites.splice(index, 1);
 console.log("splice : " + index);
 }
 })
 }
 $localStorage.favorites = $scope.favorites;
 }

 })
 //Post Controller when we click on the post
 .controller('postController', function ($scope, $http, $sce, $stateParams) {
 $scope.postCommentCount = 0;
 $http.get("http://darkoobweb.com/api/get_post/?id=" + $stateParams.postId)
 .then(function (dataReceive) {
 $scope.postTitle = $sce.trustAsHtml(dataReceive.data.post.title);
 $scope.postCategory = dataReceive.data.post.categories[0].title ? dataReceive.data.post.categories[0].title : "دسته بندی ندارد";
 $scope.postContent = $sce.trustAsHtml(dataReceive.data.post.content);
 $scope.postDate = dataReceive.data.post.date;
 $scope.postAuthorName = dataReceive.data.post.author.first_name + " " + dataReceive.data.post.author.last_name;
 if ($scope.postAuthorName.trim() == "") {
 $scope.postAuthorName = "نام نویسنده موجود نیست";
 }
 if ($scope.postAuthorName === "هومن معین") {
 $scope.postAuthorImage = "http://0.gravatar.com/avatar/9933ab5a356114d1de443a6d84d77f70?s=96&d=mm&r=g";
 } else {
 $scope.postAuthorImage = "http://2.gravatar.com/avatar/e52622892109f50b495c8068748c890f?s=96&d=mm&r=g";
 }
 $scope.postImage = dataReceive.data.post.thumbnail_images.full.url;
 $scope.postCommentCount = dataReceive.data.post.comment_count;
 $scope.postViews = dataReceive.data.post.custom_fields.Views[0];
 $scope.postUrl = dataReceive.data.post.url;


 }, function (errReceive) {
 console.log(errReceive)
 })
 })
 //Category controller for show posts in spacial category
 .controller('categoryController', function ($scope, $http, $sce, $stateParams) {
 $scope.doRefreshCategory = function () {
 $scope.categoriesPosts = [];
 $http.get("http://darkoobweb.com/api/get_category_posts/?id=" + $stateParams.catId)
 .then(function (dataReceive) {
 $scope.categoriesPosts = dataReceive.data.posts;
 $scope.categoriesPosts.forEach(function (element) {
 element.excerpt = element.excerpt = $sce.trustAsHtml(element.excerpt.substr(0, 100) + " ادامه ... ");
 element.title = element.title = $sce.trustAsHtml(element.title);
 $scope.$broadcast('scroll.refreshComplete');
 });
 $scope.categoryTitle = $sce.trustAsHtml(dataReceive.data.category.title);


 }, function (errReceive) {

 })
 };
 $scope.doRefreshCategory();

 $scope.offsetPostCategory = 0;
 $scope.countTotalPostsCategory = [];
 var categoryPostsCount = 0;
 var pageNumber = 1;

 $scope.canLoadMoreCategory = function () {
 if ($scope.offsetPostCategory <= $scope.countTotalPostsCategory) {
 return true;

 }

 };
 /*loadMore function use the address site with offset , and get posts 10 per 10*/
 $scope.loadMoreCategory = function () {

 $http.get("http://darkoobweb.com/?json=get_category_posts&id=" + $stateParams.catId + "&page=" + pageNumber)
 .then(function (dataReceive) {
 var newPost = dataReceive.data.posts;
 $scope.pageCount = dataReceive.data.pages;
 categoryPostsCount = dataReceive.data.count;
 $scope.countTotalPostsCategory = dataReceive.data.category.post_count;

 newPost.forEach(function (element) {
 element.title = $sce.trustAsHtml(element.title);
 element.excerpt = $sce.trustAsHtml(element.excerpt.substr(0, 100) + " ادامه ... ");
 });
 $scope.categoriesPosts.push.apply($scope.categoriesPosts, newPost);
 $scope.$broadcast("scroll.infiniteScrollComplete");
 $scope.offsetPostCategory += 10;
 pageNumber += 1;

 }, function (errReceive) {
 console.log(errReceive);
 })
 }
 })
 //Favorite controller , for favorites.html page
 .controller('favoriteController', function ($http, $sce, $localStorage, $scope) {

 $scope.doRefreshFav = function () {
 $scope.favorites = $localStorage.favorites;
 $scope.favoritePosts = [];
 $scope.favorites.forEach(function (element) {
 $http.get("http://darkoobweb.com/api/get_post/?id=" + element).success(function (dataReceive) {
 $scope.favoritePosts.push(dataReceive.post);
 if ($scope.favoritePosts.length == $scope.favorites.length) {
 $scope.favoritePosts.forEach(function (post) {
 post.excerpt = $sce.trustAsHtml(post.excerpt.substr(0, 100) + " ادامه ... ");
 if ($scope.favorites.indexOf(post.id) != -1) {
 post.isFavorite = true;
 } else {
 post.isFavorite = false;
 }
 })
 }
 })
 .finally(function () {
 $scope.$broadcast("scroll.refreshComplete");
 })
 })
 };
 $scope.doRefreshFav();
 $scope.favoritePost = function (post) {
 post.isFavorite = !post.isFavorite;
 if (post.isFavorite) {
 $scope.favorites.push(post.id);
 } else {
 $scope.favorites.forEach(function (element, index, array) {
 if (element == post.id) {
 $scope.favorites.splice(index, 1);
 console.log("sli : " + index);
 }
 })
 }
 $localStorage.favorites = $scope.favorites;
 }
 });

menuContent.html:

<ion-view>

 <!--Navigation bar with buttons heart and menu-->
 <ion-nav-bar class="bar-assertive">
 <!--Hamberger icon for menu-->
 <ion-nav-buttons side="right">
 <button class="button button-icon button-clear ion-navicon" menu-toggle="right"></button>
 </ion-nav-buttons>
 <!--Heart icon for like-->
 <ion-nav-buttons side="left">
 <button class="button button-icon button-clear ion-heart" ui-sref="main.favorites"></button>
 </ion-nav-buttons>
 </ion-nav-bar>

 <!--Header of navigation we select color bar-assertive (red) and set a title-->
 <ion-header-bar class="bar bar-assertive">
 <div class="title">
 <center>آخرین پست ها</center>
 </div>
 </ion-header-bar>

 <!--Create search place with search icon-->
 <div class="bar bar-subheader item-input-insert">
 <label class="item-input-inset">
 <i class="icon ion-ios-search placeholder-icon"></i>
 <input type="search" placeholder="جستوجو..." ng-model="searchTitle" ng-change="searchTitleChange()"/>
 </label>
 </div>

 <!--Create place for show posts-->
 <ion-content class="has-subheader" delegate-handle="mainScroll">
 <!--For refresh and get new posts-->
 <ion-refresher pulling-text="بروز رسانی..." on-refresh="doRefresh()">
 </ion-refresher>
 <ion-list can-swipe="true">
 <ion-item class="item item-thumbnail-left
 item-text-wrap item-icon-left" ng-repeat="post in recent_posts | filter : {title:searchTitle}" href="#/main/post/{{post.id}}">
 <img ng-src="{{post.thumbnail}}" alt=""/>
 <h2 ng-bind-html="post.title"></h2>
 <p ng-bind-html="post.excerpt"></p>
 <button class="button icon ion-heart button-clear" ng-class="{'button-assertive':post.isFavorite}"></button>
 <ion-option-button class="button-dark" ng-click="favoritePost(post);">
 <i class="icon ion-heart"></i>
 </ion-option-button>
 </ion-item>
 </ion-list>
 <!--Refresh for see more posts-->
 <ion-infinite-scroll ng-if="canLoadMore()" on-infinite="loadMore()">
 </ion-infinite-scroll>
 </ion-content>

</ion-view>

من ۳ پست را پست محبوب می کنم سپس به صفحه صفحات محبوب میرم تا ببینم پست های من آمده اند یا نه.

wpionpic31

در تصویر سمت چپ می بینید که پست های مورد علاقه خود را انتخاب کردم ، سپس با کلیک بر روی قلب در بالا سمت چپ به صفحه پست های محبوب رفتم که در تصویر سمت راست مشاهده می کنید .

درباره نویسنده

پوریا

سلام ، من پوریا هستم با جاوا آشنایی دارم به هوش مصنوعی و امنیت علاقه مندم و امید وارم آموزش هایی که در وب سایت قرار می دم برای شما مفید باشه ، موفق باشید {^}

نظر دهید

سوالات، انتقادات و پیشنهادات خود را در مورد این مطلب برای ما بفرستید. :-)