salix/front/core/directives/id.js

28 lines
860 B
JavaScript
Raw Normal View History

2018-02-10 15:18:01 +00:00
import ngModule from '../module';
2017-02-07 13:34:26 +00:00
import {kebabToCamel} from '../lib/string';
/**
* Registers the element controller into the scope as a property whose name is
* the directive value transformed to lowerCamelCase.
2018-02-10 15:18:01 +00:00
*
* @return {Object} The directive
2017-02-07 13:34:26 +00:00
*/
export function directive() {
return {
restrict: 'A',
link: function($scope, $element, $attrs) {
let id = kebabToCamel($attrs.vnId);
let controller = $element[0].$ctrl ?
$element[0].$ctrl : $element.controller($element[0].tagName.toLowerCase());
2017-05-16 10:37:48 +00:00
if (!id)
throw new Error(`vnId: Attribute can't be null`);
2017-05-16 10:37:48 +00:00
if (!controller)
throw new Error(`vnId: Can't find controller for element '${id}'`);
$scope[id] = controller;
2017-02-07 13:34:26 +00:00
}
};
2017-05-16 10:37:48 +00:00
}
2018-02-10 15:18:01 +00:00
ngModule.directive('vnId', directive);