Sha256: 29fa8bf901e1992cc510c4d9aad56a8f23d3b6e8a89a883a177f2c9113b37d4a

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

angular.module('EssayApp.directives').directive("ngOptionsClass", ["$parse", "$timeout", ($parse, $timeout) ->
  restrict: 'A',
  scope: true,
  require: 'select',
  link: (scope, elem, attrs, ngSelect)->
    # get the source for the items array that populates the select.
    optionsSourceStr = attrs.ngOptions.split(' ').pop()
    getOptionsClass = $parse(attrs.ngOptionsClass)

    scope.$watch optionsSourceStr, (items)->
      # when the options source changes loop through its items.
      $timeout (->
        angular.forEach items, (item, index)->
          # evaluate against the item to get a mapping object for
          # for your classes.
          classes = getOptionsClass(item)
          option = elem.find("option[value=#{index}]")
          # now loop through the key/value pairs in the mapping object
          # and apply the classes that evaluated to be truthy.
          if angular.isString(classes)
            # console.log option
            angular.element(option).attr('class', classes)
          else
            angular.forEach classes, (add, className)->
              angular.element(option).addClass(className) if add
        ), 1, false
])

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
translation_cms-0.1.5 app/assets/javascripts/app/directives/ng-options-class.js.coffee