npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gondel/plugin-data

v1.2.8

Published

Provide auto-bindings for data attributes located on your gondel DOM node

Downloads

375

Readme

Data Plugin for Gondel (@gondel/plugin-data)

This plugin makes it easier to use data-attributes within gondel components by dual-bind them automatically to a property. It supports preshipped serializers but also custom implementation e.g. for encryptions or special parsing for each data property.

Installation

$ npm i --save @gondel/plugin-data

# or with yarn
$ yarn add @gondel/plugin-data

Example

<div data-t-name="Search"
     data-endpoint="https://my-endpoint.com/api/v1"
     data-lang="de"
     config="{someJSON: 'goes here'}" data-result-count="0">
    <!-- some common search code here ... -->
</div>
@Component('Search')
class Search extends GondelBaseComponent {
    @data _dataEndpoint: string;

    @data('lang')
    language: 'de' | 'en';

    @data('config', JSONSerializer)
    config: { someJSON: string };

    @data('result-count', SomeCustomSerializer)
    resultCount: number;

    async someMethodToWorkWith() {
        const results = await call(`${this._dataEndpoint}/${this.language}/search?query=bla`);
        this.resultCount = results.length || 0; // <= will write to 'data-result-count'
    }
}

API

@data()

Signatures

@data: void
@data(attributeKey: string, serializer?: Serializer | ISerializer): void

For basic bindings we recommend using the decorator as the simple @data decorator. It will bind the property key to the HTML in a pretty simple way:

  • @data('my-property') someVar will become data-my-property
  • @data dataMyProperty will be connected to data-my-property
  • @data _dataPrivateProp will be connected to data-private-prop

If you won't follow the rules, the package will throw a reasonable error at you to correct your prop key naming. If you don't want to represent the data attribute from the markup the same way in your code please check the section below for how to use custom attribute keys for props. This decorator will expose a non-configurable enumerable property on your gondel component which is bound to the dedicated attribute.

If you want to use it in an advanced or special way with serializers you can use the decorator expression. Use it with your desired data-attribute key to create the binding between your class property and the HTML - that's it!

// place this inside your component
@data('my-value')
myValue: string;

// and it will be bound to:
// <node data-my-value="..."></node>

Serializers

If you don't want to care about parsing and serializing the value everytime it changes or you read it from the HTML you can plug serializers directly into the decorator which will handle this process for you :D.

Built-in serializers

You can import the serializers below from the plugin package.

  • NumberSerializer
  • BooleanSerializer
  • JSONSerializer

Create a custom serializer

// first create a simple object ...
const CustomSerializer: ISerializer = {
    serialize: (unserializedValue: string): any => serializedValue,
    deserialize: (serializedValue: any): string => unserializedValue,
}

// ... then use it like this:
@data('some-property', CustomSerializer)
value: string;

// 🎉 voilà!