Coder Social home page Coder Social logo

jonahbron / ng-polymer-elements Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gabiaxel/ng-polymer-elements

0.0 2.0 0.0 4.37 MB

Extendible AngularJS two-way binding support for Polymer core and paper elements

Home Page: https://gabiaxel.github.io/ng-polymer-elements/

License: MIT License

JavaScript 3.61% CSS 94.84% Python 1.51% Shell 0.05%

ng-polymer-elements's Introduction

ng-polymer-elements

Introduction

Web components make is possible to write encapsulated pieces of view and logic independently from any framework. The Polymer project contains a platform for pollyfilling and writing web components, and two sets of web components: core elements and paper elements.

Although there are many similarities between the ways AngularJS and Polymer implement two-way binding, it's not possible to use two-way binding of AngularJS model with web components out of the box, and even one-way binding is limited only to strings.

ng-polymer-elements overcomes this by applying directived to Polymer web components, and each directive is in charge of mapping attributes that reference AngularJS models to the web component's properties.

For example, the following code binds the scope's "myText" property to Polymer's paper-input "inputValue" property using the ng-model attribute, very similarly to how it looks for a basic text input:

<paper-input ng-model="myText"></paper-input>

Installation and Bootstrapping

The project is available through Bower: bower install ng-polymer-elements

Add the script ng-polymer-elements.js or ng-polymer-elements.min.js to your page.

Important : ng-polymer-elements requires that the Polymer platform be loaded and ready before running AngularJS bootstrapping. If you are using autmatic bootstrapping (ng-app="myModule" in your HTML) then ng-polymer-elements will handle this for you, but if you use manual bootstrapping you need to make sure it happens after the 'polymer-ready' event has been fired and wrap the target DOM element:

window.addEventListener('polymer-ready', function() {
    angular.bootstrap(wrap(document), ['myModule']);
});

Add the ng-polymer-elements module to your application dependencies:

angular.module('myModule', ['ng-polymer-elements']);

Available Component Support

The following components support two-way binding of primitive values using ng-model:

For core-list ng-model is used to bind the list data, and ng-tap is used to bind the tap event, and exposes the event as "$event".

<core-list ng-model="arrayData" ng-tap="onTap($event)">
    <template>
        <div>{{text}}</div>
    </template>
</core-list>
$scope.arrayData = [{text: 'one'}, {text: 'two'}, {text: 'three'}];

$scope.onTap = function(event) {
    var tappedItem = event.details.data;
};

See /example/index.html for coverage of most supported components.

Supporting Additional Components

You can easily add mapping for additional components, or change the provided mappings, by setting window.NG_POLYMER_ELEMENTS_EXTENDED_MAPPINGS with the new mapping definition object. The keys in the top level are the names of the directives (which are the same as the web components) in camel case. The values are object containing a key for each attribute that will be used to bind AngularJS models, and the value of each such key is an object with a single entry where the key is the type (primitive, object, array or event) and the value is the web component's property to bind.

In the following example we add support for my-component, with binding of the value in the "ng-model" attribute to my-component's "itemValue", and the event in the "ng-click" attribute to my-component's "item-clicked" event:

window.NG_POLYMER_ELEMENTS_EXTENDED_MAPPINGS = {
    myComponent: {
        ngModel: {
            primitive: 'itemValue'
        },
        ngClick: {
            event: 'item-clicked'
        }
    }
};

The above definition will allow us to write the following:

<my-component ng-model="someValue" ng-click="doSomething($event)"></my-component>

ng-polymer-elements's People

Contributors

gabiaxel avatar jonahbron avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.