drab v0.2.3 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, arguments_map)

Arguments:

  • event_name(string) - name of the even which runs the function
  • function_name(string) - function name in corresponding Commander module
  • arguments_map(key/value object) - any arguments you want to pass to the Commander function

Returns:

  • no return, does not wait for any answer

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

Returns the socket with the store key => value pair assigned

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_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)

Returns the socket with the store key => value pair assigned.

put_store(socket, :counter, 1)