controllers.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. angular.module('starter.controllers', [])
  2. .controller('DashCtrl', function($scope) {})
  3. .controller('ChatsCtrl', function($scope, Chats) {
  4. // With the new view caching in Ionic, Controllers are only called
  5. // when they are recreated or on app start, instead of every page change.
  6. // To listen for when this page is active (for example, to refresh data),
  7. // listen for the $ionicView.enter event:
  8. //
  9. //$scope.$on('$ionicView.enter', function(e) {
  10. //});
  11. $scope.chats = Chats.all();
  12. $scope.remove = function(chat) {
  13. Chats.remove(chat);
  14. };
  15. })
  16. .controller('ChatDetailCtrl', function($scope, $stateParams, Chats) {
  17. $scope.chat = Chats.get($stateParams.chatId);
  18. })
  19. .controller('AccountCtrl', function($scope, $state, $resource, Task) {
  20. // $scope.settings = {
  21. // enableFriends: true
  22. // };
  23. $scope.time = new Date();
  24. var task = $resource('http://192.168.1.6:8000/task/:id/');
  25. task.get(function (res) {
  26. $scope.taskList = res;
  27. }, function (err) {
  28. alert(JSON.stringify(err));
  29. $scope.taskList = [{
  30. id:1,title:'任务',description:'123456',created:new Date(),is_completed:false
  31. }]
  32. })
  33. $scope.toAdd = function () {
  34. $state.go('tab.task-detail',{
  35. id:0
  36. })
  37. }
  38. $scope.toDetail = function (item) {
  39. Task.detail = _.clone(item);
  40. $state.go('tab.task-detail',{
  41. id:item.id
  42. })
  43. }
  44. $scope.doRefresh = function () {
  45. getData();
  46. }
  47. function getData() {
  48. Task.getTask.get(function (res) {
  49. $scope.taskList = res;
  50. }, function (err) {
  51. alert(JSON.stringify(err));
  52. }).$promise.finally(function () {
  53. $scope.$broadcast('scroll.refreshComplete');
  54. });
  55. }
  56. })
  57. .controller('TaskDetailCtrl', function ($scope, $state, $stateParams, $resource, $ionicHistory, Task) {
  58. var id = $stateParams.id;//$state.params['id']
  59. var isNew = id == 0 ? true : false;
  60. $scope.data = isNew ? {
  61. title:null,
  62. description:null,
  63. is_completed:null
  64. } : _.clone(Task.detail);
  65. var task = $resource('http://192.168.1.6:8000/task/:id/');
  66. $scope.addTask = function () {
  67. console.log($scope.data);
  68. if (isNew) {
  69. task.save($scope.data,function () {
  70. $ionicHistory.goBack();
  71. });
  72. } else {
  73. Task.getTask.put($scope.data,function () {
  74. $ionicHistory.goBack();
  75. });
  76. }
  77. }
  78. $scope.deleteTask = function (id) {
  79. Task.getTask.delete({id:id}, function () {
  80. $ionicHistory.goBack();
  81. })
  82. }
  83. $scope.completed = function () {
  84. if (!$scope.data.is_completed) $scope.data.is_completed = !$scope.data.is_completed;
  85. }
  86. });