Git Product home page Git Product logo

alpine-observe-other-components's Introduction

Alpine โ€” Observe Other Components

A lightweight observer function for Alpine.js that lets you monitor state on other Alpine components, including parent components.

GitHub file size in bytes GitHub tag (latest by date)

About

Useful when you want to watch updates on child elements, or when you need to watch state in general.

Watch everything

Add x-alpine-observe attribute with the function name. Each component with this attribute will watch all other components and run your function every time.

<div x-data="{text:'bar'}" @click="text = 'baz'" x-text="text"></div>

<!-- The component below is watching the component above for changes -->
<div x-data="{foo:'bar', go(details) { this.foo = details.data[0].text }}" x-alpine-observe="go" x-text="foo"></div>

Demo

Watch only specific components

Add x-alpine-observe attribute with an object to specify a selector. Only components with this selector will be monitored for changes. Note that changes occur on initial load so the example below will first show 1, not 0.

<div
  class="counters"
  x-data="{text:'bar'}"
  @click="text = text === 'baz' ? 'bar' : 'baz'"
  x-text="text">Watched</div>

<div
  x-data="{text:'bar'}"
  @click="text = text === 'baz' ? 'bar' : 'baz'"
  x-text="text">Not watched</div>

<!-- The component below is watching only the component above with a class of counters for changes -->
<div
  x-data="{count:0, go(details) { this.count++ }}"
  x-alpine-observe="{selector: '.counters', callback: 'go'}"
  x-text="count"></div>

Demo

Watch parent components

Similar to above, you can scope the child to only watch the parent for changes.

<div 
  id="parent" 
  x-data="{text: 'parent'}">
  <div>
    Parent: <button
      @click="text = text === 'parent' ? 'also parent' : 'parent'"
      x-text="text"></button>
  </div>
  <div>
    Nested <button 
      x-data="{ text: 'child', getDataFromParent(details) { this.text = `${details.data[0].text}` } }"
      x-alpine-observe="{selector: '#parent', callback: 'getDataFromParent'}"
      @click="text = text === 'child' ? 'also child' : 'child'">
      <span x-text="text"></span>
  </button>
  </div>
</div>

Demo

Installation

Include the following <script> tag in the <head> of your document (before Alpine):

<script src="https://cdn.jsdelivr.net/gh/kevinbatdorf/alpine-observe-other-components@latest/dist/index.js"></script>

Manual

If you wish to create your own bundle:

npm install kevinbatdorf/alpine-observe-other-components --save

Then add the following to your script:

import 'alpine-observe-other-components'
import 'alpinejs'

License

Copyright (c) 2020 Kevin Batdorf

Licensed under the MIT license, see LICENSE.md for details.

alpine-observe-other-components's People

Contributors

dependabot[bot] avatar kevinbatdorf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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