Giter Site home page Giter Site logo

axelson / live_attribute Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dominicletz/live_attribute

0.0 2.0 0.0 8 KB

LiveAttributes for Phoenix.LiveView easier dynamic binding to lively propertiers

Home Page: https://hexdocs.pm/live_attribute/LiveAttribute.html

License: MIT License

Elixir 100.00%

live_attribute's Introduction

LiveAttribute

LiveAttribute makes binding updateable values easier. To use it add it to your LiveView using use LiveAttribute and then use the function assign_attribute(socket, subscribe_callback, property_callbacks) to register attributes.

The attributes will listen to all incoming events and update their assigns of your LiveView automatically, saving you the hassle of implementing independent handle_info() and update_...() calls.

Example using LiveAttribute

defmodule UserLive do
  use Phoenix.LiveView
  use LiveAttribute

  def mount(_params, _session, socket) do
    {:ok, assign_attribute(socket, &Accounts.subscribe/0, users: &Accounts.list_users/0)}
  end

  def handle_event("delete_user", %{"id" => user_id}, socket) do
    Accounts.get_user!(user_id)
    |> Accounts.delete_user()

    {:noreply, socket}
  end
end

Same Example without LiveAttribute

defmodule UserLive do
  use Phoenix.LiveView

  def mount(_params, _session, socket) do
    if connected?(socket), do: Accounts.subscribe()
    {:ok, update_users(socket)}
  end

  defp update_users(socket) do
    users = Accounts.list_users()
    assign(socket, users: users)
  end

  def handle_event("delete_user", %{"id" => user_id}, socket) do
    Accounts.get_user!(user_id)
    |> Accounts.delete_user()

    {:noreply, socket}
  end

  def handle_info({Accounts, [:user, _], _}, socket) do
    {:noreply, update_users(socket)}
  end
end

Installation

If available in Hex, the package can be installed by adding live_attribute to your list of dependencies in mix.exs:

def deps do
  [
    {:live_attribute, "~> 1.0.0"}
  ]
end

live_attribute's People

Contributors

dominicletz avatar

Watchers

James Cloos 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.