J
J
JIakki2015-09-20 15:36:49
JavaScript
JIakki, 2015-09-20 15:36:49

How to improve controllers?

There are two controllers

app.controller 'showDialogCtrl', ['$scope', '$mdDialog', '$mdSidenav', '$mdUtil',  ($scope, $mdDialog, $mdSidenav, $mdUtil) ->
  $scope.showChange = (event, block, img, name) ->
    $mdDialog.show
      locals:
        parent:
          img: img
          name: name
          block: block
      controller: "CategoriesCtrl",
      templateUrl:'change.html',
      targetEvent: event,
      clickOutsideToClose:true
  $scope.showAdd = (event, block) ->
    $mdDialog.show
      locals:
        parent:
          block: block
      controller: "CategoriesCtrl",
      templateUrl:'add.html',
      targetEvent: event,
      clickOutsideToClose:true
  $scope.showAddToSource = (event) ->
    $mdDialog.show
      controller: "addToSouceCtrl",
      templateUrl:'add-to-source.html',
      targetEvent: event,
      clickOutsideToClose:true
  $scope.showAddToCategory = (from, to, event) ->
      controller: "addToCategoryCtrl",
      templateUrl:'add-to-category.html',
      targetEvent: event,
      clickOutsideToClose:true
  $scope.showSettings = (id) ->
    return $mdUtil.debounce( ()->
      $mdSidenav(id).open()
    ,100 )()
]
app.controller 'CategoriesCtrl', ['$scope', '$mdDialog', 'parent', "$http", 'localData', ($scope, $mdDialog, parent, $http, localData) ->
  $scope.default = 'tag.svg'
  $scope.category = parent
  $scope.category.newName = parent.name

  $scope.change = ->
    do $mdDialog.hide;
    $http.post "/changeCategory",
      icon : $scope.category.img
      newName : $scope.category.newName
      block: $scope.category.block
      name: $scope.category.name
    .success ->
      do location.reload
  $scope.loadIcon = ->
    $scope.Icons = localData.Icons
    $scope.changeCheck = true	
  $scope.openMenu = ($mdOpenMenu, event) ->
    originatorEv = event;
    $mdOpenMenu event;
  $scope.changeIcon = (icon) ->
    $scope.default = $scope.category.img = icon
    $scope.changeCheck = false
  $scope.add = () ->
    do $mdDialog.hide;
    $http.post "/add",
      icon : $scope.default
      name : $scope.category.name
      block: $scope.category.block
    .success ->
      do location.reload
  $scope.delete = (check) ->
    if check
      $http.post "/delete",
        oldName: $scope.category.oldName
        block: $scope.category.block
      .success ->
        do location.reload
]

How can it be improved?
Maybe something extra that should not be in the controller.
I would be grateful for advice!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-09-20
@JIakki

Maybe something extra that should not be in the controller

1) no scopes in controllers, use controller as syntax.
2) split everything into directives, try to make the whole application consist of directives, or separate components .
3) name the controller methods ... more logically ...
4) eliminate code duplication, you have a lot of very monotonous code there.
5) move work with data to services.
6) use classes (es6 or coffee)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question