Created
December 18, 2015 18:49
-
-
Save demetriusnunes/2eafead8242ca2811a2d to your computer and use it in GitHub Desktop.
Concise component definition for AngularJS 1.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
angular.module('Component', []) | |
.factory('Component', function () { | |
return function(bindings, template, linkFn, options) { | |
var directive = { | |
scope: parseBindings(bindings), | |
template: parseTemplate(template), | |
link: linkFn | |
}; | |
return angular.extend({}, directive, options); | |
}; | |
function parseBindings(bindings) { | |
return bindings.reduce(function(scope, binding) { | |
var name = binding.slice(1); | |
scope[name] = binding.slice(0, 1); | |
return scope; | |
}, {}); | |
} | |
function parseTemplate(template) { | |
return angular.isArray(template) ? template.join('') : template; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
angular.module('app', ['Component']) | |
.directive('personItem', function (Component) { | |
return Component( | |
['@name', '@age', '@highlight', '&onClick'], | |
['<div ng-click="onClick($event)"', | |
' ng-class="{ highlight: highlight === \'true\' }">', | |
' <h1>{{:: name}}</h1>', | |
' <p>{{:: age }}</p>', | |
'</div>']); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PersonItem is a typical dumb component, but if need be, the optional 3rd and 4th parameters can be used to make it smart.