Code Monkey home page Code Monkey logo

gulp-polymer-expr's Introduction

gulp-polymer-expr Build Status

Syntactic SUGAR for JS expressions in Polymer data binding annotation , which is transpiled into computed binding functions on the component.

Use any valid JS expressions, for examples:

  • [[ index + 1 ]]
  • [[ ok ? 'YES' : 'NO' ]]
  • message.split('').reverse().join('')
  • _.sortBy(users, ['join', 'age'])

Overview

Polymer data binding system has a limitation that only property path and computed func/property is allowed in place, although this principle is good as a fundamental building blocks that gives good performant result, productivity-wise, it is insufficient where in most cases where simple path is insufficient and computed property is a boilerplate code to write, even all you need is as simple as [[ index+1 ]] .

Purpose of this plugin is to enable you using inline JavaScript expression that are widely adopted in many other MVVM systems.

Which kind of expression?

Valid JavaScript expression can be used, with a few regulations:

  • No assignment operators (i.e. a = b, a += 1, a-- and so on);
  • No new, delete, or void operators;
  • No function literals (i.e. anything that involves the function keyword);
  • Path (or complex path) to host element's property is ok, including index and item alike properties created dynamical in sub template like dom-repeat;
  • Global objects or *Global function call is ok.

Install

$ npm install —save-dev gulp-polymer-expr

Usage

This allows you to use any valid JS expression in one-way data binding with square brackets ( [[ *(expr)* ]] )

<dom-module id="x-custom">

  <template>
    My name is <span>{{first + ' ' + last}}</span>
  </template>

  <script>
    Polymer({
      is: 'x-custom',
      properties: {
        first: String,
        last: String
      }
    });
  </script>

</dom-module>

Comparing to vanilla Polymer binding version:

<dom-module id="x-custom">

  <template>
    My name is <span>{{fullName}}</span>
  </template>

  <script>
    Polymer({
      is: 'x-custom',
      computeFullName: function(first, last) {
        return first + ' ' + last;
      },
      properties: {
        first: String,
        last: String,
        fullName: {
          type: String,
          // when `first` or `last` changes `computeFullName` is called once
          // and the value it returns is stored as `fullName`
          computed: 'computeFullName(first, last)'
        }
      }
    });
  </script>

</dom-module>

To make the above works, your gulp task shall looks like this:

const gulp = require('gulp');
const polymerExpr = require('gulp-polymer-expr');

gulp.task('compile', () => {
	gulp.src('components/**/*.html')
		.pipe(polymerExpr())
		.pipe(gulp.dest('dist'))
);

Use of global objects

You can use global function call or object access in expression, e.g. if your component’s binding is using lodash to sort a list of data, where list is a valid path to the local scope, the binding [[ _.sortBy(list, 'created')]] will be transpired into computed binding [[ __c_0(list) ]] and yields the following computed function:

Polymer({
__c_0: function(list) {
  return _.sortBy(list, ‘created’);
},
...
});

Note that how _.sortBy is recognized as a global object and thus doesn’t get precompiled as a parameter to the computed function.

API

polymerExpr([options])

options

N/A

License

MIT © Garry Yao

gulp-polymer-expr's People

Contributors

garryyao avatar

Stargazers

言川 avatar

Watchers

James Cloos 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.