Giter Club home page Giter Club logo

bindoc-templates's Introduction

@bindoc/templates

Provides an easy way for dynamically creating components by data in angular apps.

Usage

For complete sources see: Demo Code;

npm install --save @bindoc/templates 

BdTemplate

  • BdTemplate.type is used to match templates given by TemplateProvide
  • BdTemplate.init(config: BdTemplateData) is used to initialize the provided data object to fill the component custom fields.
  // custom template implementation 
  
  import {Component} from "@angular/core";
  import {BdTemplate, BdTemplateData} from "@bindoc/templates";
  
  @Component({
    template: `
      <h1>Some template</h1>
      <span>{{message}}</span>
    `
  })
  export class SampleComponent extends BdTemplate {  
    public message: string;
     
    public init(config: BdTemplateData): void {
      this.message = config.data.message;
    }
  }
  

Insert by BdTemplate

  // data class for sample template
  
  import {BdTemplateData} from "@bindoc/templates";
  
  export class SampleData implements BdTemplateData {
    
    public type: string = 'SampleData';
    public data: { message?: string };
  
    constructor(message: string) {
      this.data = { message: message };
    }
  }
  
  
  
  // demo.component.ts 
  
  import {Component, Type} from "@angular/core";
  import {BdTemplate} from "@bindoc/templates";
  import {SampleComponent, SampleData} from "./sample-template.component";
  
  @Component({
    template: `
      <bd-dynamic-template  [data]="sampleData" 
                            [template]="sampleTemplate">
      </bd-dynamic-template>
    `
  })
  export class BdDynamicTemplateDemoComponent {
  
    public sampleData: SampleData = new SampleData('Some distinct sample data added through extension class');
    public sampleTemplate: Type<BdTemplate> = SampleComponent;
  }

Insert by BdTemplateProvider

  • BdTemplateProvider.getTemplates() provides an array of all possible templates.
  
  import {BdTemplate, BdTemplateProvider} from "@bindoc/templates";
  import {Type} from "@angular/core";
  import {SampleComponent} from "./sample-template.component";
  
  export class SampleTemplateProvider implements BdTemplateProvider {
  
    public getTemplates(): Type<BdTemplate>[] {
      return [SampleComponent];
    }
  }

After setting up templates and a template provider the templates can be inserted into views. For filling a template with dynamic content a data class is needed.

  • BdTemplateData.type the type is used to match against templates type.
  // data class for sample template
  import {BdTemplateData} from "@bindoc/templates";

  export class SampleData implements BdTemplateData {
    
    public type: string = 'SampleData';
  
    constructor(public message: string) {
  
    }
  }
  
  
  
  // demo.component.ts 
  
  import {Component} from "@angular/core";
  import {SampleData} from "./sample-template.component";
  import {SampleTemplateProvider} from "./sample-template-provider.model";
  
  @Component({
    template: `
      <bd-dynamic-template-factory  [data]="sampleData"
                                    [templateProvider]="sampleTemplateProvider">
      </bd-dynamic-template-factory>
    `
  })
  export class BdDynamicTemplateDemoComponent {
  
    public sampleData: SampleData = new SampleData('Some distinct sample data added through extension class');
    public sampleTemplateProvider: SampleTemplateProvider = new SampleTemplateProvider();
  }

bindoc-templates's People

Contributors

svenseemann 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.