5.2.0 ā€¢ Published 4 years ago

babel-skeleton v5.2.0

Weekly downloads
11
License
MIT
Repository
github
Last release
4 years ago

šŸ’€ babel skeleton

Babel skeleton is a JavaScript library for structure web applications

Build Status Coverage Status Downloads Release

Table of content:


šŸ“¦ Installation

npm install babel-skeleton

šŸ•¹ļø Usage

Create a project

./node_modules/.bin/skeleton new my-project
cd my-project

šŸ’» Web browser

Run on web browser

npm run start

šŸ“± Mobile

Install android

npm run android:install

Run on device

npm run android:build

Gradle must be installed, device must be detected and SDK build tools must be installed and licenses must be accepted (ANDROID_HOME/tools/bin/sdkmanager --licenses)

šŸ“œ Scripts

The created project contain following scripts:

ScriptFeature
npm run startStart to dev
npm run skeletonRun babel-skeleton for generate component or service
npm run devBuild entry points and watch
npm run prodOptimise dev
npm run buildBuild
npm run cordovaRun cordova
npm run resRun cordova-res for generate icon and splashscreen
npm run androidDeploy on device
npm run android:buildBuild and deploy
npm run android:installInstall android
npm testPass tests
npm run test:coverageGenerate report
npm run test:coverallsSend report to coveralls

Travis configuration is setup for pass tests and push report to Coveralls after a build success


šŸ‘Øā€šŸ’» Development

The skeleton provide Component for render template, Service for share data with notification and RouterComponent for navigation and lifecycle

šŸ“œ Scripts

The skeleton contain following scripts:

ScriptFeature
npm run skeleton new nameGenerate a project
npm run skeleton generate component nameGenerate a component
npm run skeleton generate service nameGenerate a service

šŸš¦ Routing

Associate a Component to an URL using the RouterComponent

RouterComponent provide lifecycle hook for routed components

index.js

import { RouterComponent } from "babel-skeleton";

RouterComponent
  .add('/foo', 'foo', FooComponent)
  .add('/bar/:id', 'bar', BarComponent)
  .run(new AppComponent)

By default the first component will be rendered, rewrited URL is allowed and matching component will be routed

index.html

<html>
<head></head>
<body>
  <!-- Entry point component -->
  <app></app>
    <script src="./dist/index.js"></script>
</body>
</html>

You can run the component you want but his selector have to be found in the index.html

app.component.html

Hello app
<!-- Router component -->
<router></router>

Routed component will be rendered by the RouterComponent, his tag must exists in the entry point template

šŸ“‘ RouterComponent

āž• Add a route

add(path, name, component)
Param
{String} path Route path
{String} name Route name
{Component} component Component class or instance
Return
{RouterComponent} Router instance
Throw
{ReferenceError} Path or name exists

āž• Run the entry point

run(component)
Param
{Component} component Component instance
Return
{RouterComponent} Router instance

āž• Navigate to a Route

navigate(name, param= null)
Param
{String} name Route name
{Object} param Route param
Throw
{ReferenceError} Route not found

āž• Retrieve the current Route or a Route parameter value

get(paramName= null)
Param
{String} paramName Route param name
Return
{Mixed} Active Route or param name value
Throw
{ReferenceError} Route parameter name not found

šŸ° Components

Generate a component

npm run skeleton generate component foo

Following files have been generated in app/foo:

  • foo.component.js
  • foo.component.html
  • foo.component.scss

Component have at least a selector and a template

foo.component.js

import { Component } from  'babel-skeleton';
import { template } from  './foo.component.html';

export class FooComponent extends Component {

  constructor() {
    super({ 
        selector: "foo", 
        template: template 
    });
    this.counter = 0;
  }

  increment() {
    return counter++;
  }

}

Template use ES6 strings with access to attributes and methods

foo.component.html

<!-- Interpollate "counter" attribute -->
<h1>${counter}</h1>

<!-- Trigger "increment" method -->
<button onclick="increment()">Click</button>

Component is updated if an event handler return a value

SCSS file is generated and free to you to import it

foo.component.scss

foo {}

Child Component can be embeed

bar.component.js

import { Component } from  'babel-skeleton';
import { template } from  './bar.component.html';
import { BazComponent } from  './baz/baz.component.html';
import { QuxComponent } from  './qux/qux.component.html';

export class BarComponent extends Component {

  constructor() {
    super({ 
        selector: "bar", 
        template: template,
        components: [
          new BazComponent,
          new QuxComponent,
        ]
    });
  }

}

Child component selector have to be found in the template

bar.component.html

<baz></baz>
<qux></qux>

Lifecycle hooks are triggered by the router

baz.component.js

import { Component } from  'babel-skeleton';
import { template } from  './baz.component.html';

export class BazComponent extends Component {

    constructor() {
        super({
            selector: "baz", 
            template: template 
        });
    }

    /**
     * Called after the component is attached
     */
    onInit() { }

    /**
     * Called after the component is displayed
     * 
     * @param {HTMLElement} element Updated element
     */
    onUpdate(element) { }

    /**
     * Called after the component is detached
     */
    onDestroy() { }

    /**
     * Called after the user trigger the back button
     * You can cancel the back navigation by returning false
     * 
     * @returns {Boolean}
     */
    onBack() { }

    /**
     * Called after the user trigger the pause button
     */
    onPause() { }

    /**
     * Called after the user trigger the resume button
     */
    onResume() { }

}

šŸ“‘ Component

āž• Update the Component template

update()
Return
{Component} Component instance
Throw
{ReferenceError} Component selector not found

šŸ’« Services

Generate a service

npm run skeleton generate service baz

File baz.service.js have been generated in app/baz:

Service share data and can notify for changes

baz.service.js

import { Service } from  'babel-skeleton';

export const BazService = new  class  extends  Service {

  constructor() {
    super();
    this.data = [];
  }

  post(data){
    this.data.push(data);
    this.notify();
  }

}

Attach or detach callables to trigger when notify is called

baz.component.js

import { Component } from  'babel-skeleton';
import { template } from  './baz.component.html';
import { BazService } from  './baz.service';

export class BazComponent extends Component {

    constructor() {
        super({ selector: "baz", template: template });
        this.observer = (service) => {
          alert(`BazService has ${service.data.length} items`)
        }
    }

    onInit() { 
      BazService.attach(this.observer);
    }

    onDestroy() { 
      BazService.detach(this.observer);
    }

}

šŸ“‘ Service

āž• Attach a callable triggered when notify is called

attach(callable)
Param
{Function} callable Function to attach
Return
{Service} Service instance

āž• Detach a callable

detach(callable)
Param
{Function} callable Function to attach
Return
{Service} Service instance

āž• Call all attached callables

notify()
Return
{Service} Service instance

5.2.0

4 years ago

5.1.4

4 years ago

5.1.3

4 years ago

5.1.2

4 years ago

5.1.1

4 years ago

5.1.0

4 years ago

5.0.2

4 years ago

5.0.1

4 years ago

5.0.0

4 years ago

4.1.0

4 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.4.8

5 years ago

2.4.7

5 years ago

2.4.6

5 years ago

2.4.5

5 years ago

2.4.4

5 years ago

2.4.3

5 years ago

2.4.2

5 years ago

2.4.1

5 years ago

2.4.0

5 years ago

2.3.2

5 years ago

2.3.1

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.4

5 years ago

2.1.3

6 years ago

2.1.2

6 years ago

2.0.1

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.1

6 years ago

1.0.0

8 years ago