3
\$\begingroup\$

I'm trying to improve my Angular directive. I'm pretty new to CoffeeScript, so I think there may be inconsistent code.

'use strict'
class Main extends Directive
  constructor: (VerifyService) ->
    return {
      restrict : 'M'
      link : (scope) ->
        scope.watch 'events', ->
          ACTION =
            booking = () ->
              scope.ctrl.create()

          filter = (control) ->
            if ACTION[control]
              ACTION[control]()
            return

          door = () ->
            control = $(this).data('controller-control')
            filter(control)
            return

          init = () ->
            PROP.element.bind('click', door)
            return
          return

          test = () ->
            if VerifyService.each(PROP)
              init()
            return

          test()
          return
    }

I'm using directives not only to manipulate the HTML, but to use as a bridge between HTML and CONTROLLERS. Please do not question why I'm doing like this; there is a very big reason.

I'm using the GULP-NG-CLASSIFY, which makes available the use of CoffeeScript class syntax.

Inside of the link function (which all directive have) I'm starting my code from the bottom to the top.

First I test if all my HTML properties are available (in this code I only have one):

      test = () ->
        if VerifyService.each(PROP)
          init()
        return

      test()
      return

If all the PROP exists in the HTML, I want initialize the script, which will bind a click in the PROP.element:

      init = () ->
        PROP.element.bind('click', door)
        return
      return

So, in the door, I want get the data from data-controller-control:

      door = () ->
        control = $(this).data('controller-control')
        filter(control)
        return

I need filter and I want get the controller action requested. I need to be sure that the action in my controller exists:

      filter = (control) ->
        if ACTION[control]
          ACTION[control]()
        return

Finally I call the ACTION requested by the user:

      ACTION =
        booking = () ->
          scope.ctrl.create()

In this case I only have one.

\$\endgroup\$
0

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.