angular-component-factory

A very opinionated angular decorator to simplify use of directives as components

Angular Core Dependency: ~1.2.16

Module License: MIT

Added by: Sam Deering

GitHub

Repository

kennethlynne/angular-component-factory

Stats

Stars: 2

Watchers: 2

Forks: 2

Module Description

angular-component-factory Build Status Code Climate Stories in Ready

A very opinionated AngularJS decorator and provider inspired by the web components proposal to simplify use of directives as reusable components.

See demo, and view source.

Usage

To install: bower install angular-component-factory

Then add componentFactory as a dependancy to your module or decorate module using angular.componentFactory.moduleDecorator(<your module>)

###Example:

var demo = angular.module('DemoApp', []);

angular.componentFactory.moduleDecorator(demo);

demo.component('unicornTest', function ($log) {
    $log.log('Demo component says hi!')

    return {
        link: function (scope) {
            scope.message = "I'm a unicorn component!";
        }
    }
});

views/components/unicorn-test/unicorn-test.html:

<p>{{message}}</p>
<unicorn-test-component></unicorn-test-component>

Will now expand into a directive that by convention loads a template from /views/components/unicorn-test/unicorn-test.html, replaces contents by default, and creates a new scope. The object that the constructor returns should be a directive definition object. This will then be decorated for convention over configuration.

One way you might use it:

demo.component('horsePartial');

This will try to fetch a template from /views/components/horse-partial/horse-partial.html and instantiate a directive with a isolate scope. Later on when you want to add some logic, just implement the factory function as you usually do for a directive.

Configuration

If you want to set the view path you may do something like this

demo.config(function (componentFactoryProvider) {
    componentFactoryProvider.setViewPath('some/custom/path/');
});

Views will then be loaded from some/custom/path/unicorn-test/unicorn-test.html

You can also provide setViewPath with a factory function to do more custom handling based on the components name:

componentFactoryProvider.setViewPath(function (componentSnakeCasedName, componentName) {
    return 'components/' + componentSnakeCasedName + '/some-path/views/' + componentName + '.html';
});

This function will be called on every registration to map the component name to a url. The above example will return 'components/example-name/some-path/views/exampleName.html'

The factory will be served with two parameters: the snake cased component name, and the original one, and is expected to return a string path.

The MIT License (MIT)

Copyright (c) 2014 Kenneth Lynne

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Analytics

Module stats last updated: 2015-06-08 00:37:15

Disclaimer: Some data on this page may have been gathered from the authors GitHub respository. If you see any mistakes or outdated information please let us know. Thanks!