drab v0.3.0 Drab.Core

Drab Module with the basic communication from Server to the Browser. Does not require any libraries like jQuery, works on pure Phoenix.

defmodule DrabPoc.JquerylessCommander do
  use Drab.Commander, modules: [] 

  def clicked(socket, payload) do
    socket |> console("You've sent me this: #{payload |> inspect}")
  end
end

See Drab.Commander for more info on Drab Modules.

Running Elixir code from the Browser

There is the Javascript method Drab.run_handler() in global Drab object, which allows you to run the Elixir function defined in the Commander.

Drab.run_handler(event_name, function_name, argument)

Arguments:

  • event_name(string) - name of the even which runs the function
  • function_name(string) - function name in corresponding Commander module
  • argument(anything) - any argument you want to pass to the Commander function

Returns:

  • no return, does not wait for any answer

Example:

<button onclick="Drab.run_handler('click', 'clicked', {click: 'clickety-click'});">
  Clickme
</button>

The code above runs function named clicked in the corresponding Commander, with the argument %{"click" => "clickety-click}"

Summary

Functions

Asynchronously broadcasts given javascript to all browsers displaying current page

Sends the log to the browser console for debugging

Broadcasts the log to the browsers console for debugging

Synchronously executes the given javascript on the client side and returns value

Returns the value of the Plug Session represented by the given key

Returns the value of the Plug Session represented by the given key or default when key not found

Returns the value of the Drab store represented by the given key

Returns the value of the Drab store represented by the given key or default when key not found

Saves the key => value in the Store. Returns unchanged socket

Functions

broadcastjs(socket, js)

Asynchronously broadcasts given javascript to all browsers displaying current page.

console(socket, log)

Sends the log to the browser console for debugging

console!(socket, log)

Broadcasts the log to the browsers console for debugging

execjs(socket, js)

Synchronously executes the given javascript on the client side and returns value.

get_session(socket, key)

Returns the value of the Plug Session represented by the given key.

counter = get_session(socket, :userid)

You must explicit which session keys you want to access in :access_session option in use Drab.Commander.

get_session(socket, key, default)

Returns the value of the Plug Session represented by the given key or default when key not found

counter = get_session(socket, :userid, 0)

You must explicit which session keys you want to access in :access_session option in use Drab.Commander.

get_store(socket, key)

Returns the value of the Drab store represented by the given key.

uid = get_store(socket, :user_id)
get_store(socket, key, default)

Returns the value of the Drab store represented by the given key or default when key not found

counter = get_store(socket, :counter, 0)
put_store(socket, key, value)

Saves the key => value in the Store. Returns unchanged socket.

put_store(socket, :counter, 1)
tokenize_store(socket, store)