
var angular = require('angular');
var _ = require('jsUtil');
require('../modules');
require('../../flowmodulebase/services/OptionService');

var module = angular.module('flow.directives');

module.directive('sinoOptionViewer', function(OptionService) {
  
  
  return {
    restrict: 'E',
    replace: true,
    template: '<span>{{text}}</span>',
    scope: {
      optionId: '='
    },
    link: function(scope, element, attrs) {
      scope.$watch(function() {
        return scope.optionId;
      }, function(optionId) {
        if (optionId) {
          OptionService.get(attrs.optionName).then(function(_options) {
            var options = _options[0];
            var option;
            for (var i = 0, len = options.length; i < options.length; i++) {
              option = options[i];
              if (option.value === optionId) {
                scope.text = option.text;
              }
            }
        });
        }
      });
    }
  }
});
