Code Monkey home page Code Monkey logo

gonex's Introduction

Gonex - get your Phoenix variables in your Javascript

Actions Status Hex version badge

Gonex is a super simple implementation of gon but for Phoenix projects and it's a convenient way to send some data from your controller to your JavaScript.

Installation

The package can be installed by adding gonex to your list of dependencies in mix.exs:

def deps do
  [
    {:gonex, "~> 0.1.0"}
  ]
end

Usage

First in your layout view:

defmodule MyAppWeb.LayoutView do
  use MyAppWeb, :view
  
  # Import Gonex view helpers
  import Gonex.View
  
  def render("app.html", assigns) do
    ~E"""
    <!-- ... -->
    <%= render_gon(assigns.conn, "myAppGon") %>
    <!-- ... -->
    """
  end
end

Then in your controller:

defmodule MyAppWeb.PageController do
  use MyAppWeb, :controller
  
  # Import Gonex controller helpers.
  # Alternatively you can import this in your MyAppWeb :controller definition 
  # and make Gonex available within all of your controllers
  import Gonex.Controller
  
  def index(conn, _params) do
    conn
    |> put_gon(greeting: "Hello, World!") # put something useful here
    |> render("index.html")
  end
end

Finally in you JavaScript:

alert(window.myAppGon.greeting); // "Hello, World!"

Sometimes you will need to have a basic (or initial) set of variables across all of your views. This can be easily achieved with the custom plug:

defmodule MyAppWeb.PutGonPlug do
  def init(opts), do: opts
  
  def call(conn, _opts) do
    Gonex.Controller.put_gon(conn, %{
      env: Application.get_env(:my_app, :env),
      locale: Gettext.get_locale(MyAppWeb.Gettext)
    })
  end
end

Then in your router:

defmodule MyAppWeb.Router do
  use MyAppWeb, :router

  pipeline :browser do
    # ...
    plug MyAppWeb.PutGonPlug
    # ...
  end
end

Documentation

The docs can be found at https://hexdocs.pm/gonex.

gonex's People

Contributors

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