'use strict'; angular.module('warehost') .controller('BlogWebCtrl',function(session,config,alert,NgTableParams,$scope,$rootScope,$http,$stateParams){ $scope.bloglist = []; alert.set({}); $scope.blog = {}; $scope.websiteid = $stateParams.websiteid; function resetBlog(){ $scope.blog = {}; } function load(){ $http.get(config.api+'/web/website/'+$stateParams.websiteid+'/blog').then(function(res){ session.set(res); resetBlog(); $scope.bloglist = res.data.data; var blog; for(blog in $scope.bloglist){ if(blog.ID === $stateParams.blogid) { $scope.blog = blog; return; } } if($scope.bloglist.length > 0 && $stateParams.blogid !== '-1'){ $scope.blog = $scope.bloglist[0]; } }); } function submitresult(res){ session.set(res); alert.set(res); if(res.data.data){ load(); } } resetBlog(); load(); $rootScope.$on('warehost.session',load); $scope.save = function(){ if($scope.blog.ID){ $http.patch(config.api+'/web/website/'+$stateParams.websiteid+'/blog/'+$scope.blog.ID,$scope.blog).then(submitresult); }else{ $http.post(config.api+'/web/website/'+$stateParams.websiteid+'/blog',$scope.blog).then(submitresult); } }; $scope.delete = function(a){ $http.delete(config.api+'/web/website/'+$stateParams.websiteid+'/blog/'+a.ID).then(submitresult); }; });