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

jquery-state-store

v1.4.5

Published

state management jquery

Downloads

4

Readme

My State Management Library

A simple state management library using jQuery.

Usage

Include jQuery and the library in your HTML file:

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="./jquery-state-store.min.js"></script>

Using CDN

<script src="https://unpkg.com/[email protected]/dist/jquery-state-store.min.js"></script>

Initialize and use the library:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://unpkg.com/[email protected]/dist/jquery-state-store.min.js"></script>
    <title>Jquery State Management</title>
</head>
<body>
    <p data-bind="text:counter">0</p>
    <button id="increment">Increment</button>
    <button id="decrement">Decrement</button>
    
    <script>
        $.useState('counter', 0);

        $(document).ready(function() {
            
            $('#increment').click(function() {
                var currentCounter = $.getState('counter');
                $.setState('counter', currentCounter + 1);
            });

            $('#decrement').click(function() {
                var currentCounter = $.getState('counter');
                $.setState('counter', currentCounter - 1);
            });

        })
    </script>
</body>
</html>

Directives

data-bind:text

Sets the text content of the element to the state value.

<p data-bind="text:counter"></p>

data-bind:html

Sets the HTML content of the element to the state value.

<div data-bind="html:user.name"></div>

data-bind:if

Conditionally displays the element based on the state value.

<div data-bind="if:counter > 0">
    Counter is greater than zero.
</div>

data-bind:data

Sets the specified data attribute of the element to the state value.

<div data-bind="data:title:user.name"></div>

data-bind:model

Binds the state value to the input element's value, making it two-way reactive.

<input type="text" data-bind="model:user.name" />

Example Usage

Here is an example of how to use the directives in your HTML and JavaScript:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>State Management Example</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://unpkg.com/[email protected]/dist/jquery-state-store.min.js"></script>
</head>
<body>
    <div>
        <button id="increment">Increment</button>
        <button id="decrement">Decrement</button>
    </div>
    <p data-bind="text:counter"></p>
    <div data-bind="if:counter > 0">
        Counter is greater than zero.
    </div>
    <input type="text" data-bind="model:user.name" />
    <p data-bind="text:user.name"></p>

    <script>
        $(document).ready(function() {
            // Initialize states
            $.useState('counter', 0);
            $.useState('user', { name: 'John Doe', age: 30 });

            // Example of setting state
            $('#increment').click(function() {
                var currentCounter = $.getState('counter');
                $.setState('counter', currentCounter + 1);
            });

            $('#decrement').click(function() {
                var currentCounter = $.getState('counter');
                $.setState('counter', currentCounter - 1);
            });

            // Apply bindings
            $.applyBindings();
        });
    </script>
</body>
</html>

API

$.useState(name, initialValue)
//Register a new state.

$.getState(name)
//Get the value of the state.

$.setState(name, newValue)
//Set a new value for the state.

$.reactive(name, listener)
//Register a listener that gets called whenever the state changes.

$.useEffect(name, effect)
//Register an effect that gets called with the state value whenever the state changes.

License

MIT