Almog

Almog

How to pass variables from child template/view into main template view?

Hi, So I’m very new to Elixir and Phoenix not even sure if this is possible but wanted to know how this can be done if it can be done.

I have my main index HTML or admin HTML which has the following at the bottom

<script type="text/javascript">
                 $(function() {
                     var editor = editormd("editormd", {
                          path : "/js/admin/",
                          readOnly:false,
                     });

                 });
             </script>

I then have the following inside my loaded template/view

<div id="editormd">
                        <%= label f, :content, class: "control-label" %>
                            <%= textarea f, :content, class: "" %>
                                <%= error_tag f, :content %>
                        </div>

I’m using the editormd across different views and I need a way to set read-only var to true or false depending on the template

Maybe something like

<div id="editormd" readonly="true">

then

  var editor = editormd("editormd", {
                              path : "/js/admin/",
                              readOnly:myvar,
                         });

Would pick that up and set the correct setting, it would need to reload as well?

Most Liked

peerreynders

peerreynders

Have your tried something like

<div id="editormd" data-read-only="false">
 $(function() {
  var TARGET_ID = 'editormd';
  var selector = '#' + TARGET_ID;
  var data = $(selector).data('readOnly'); /* i.e. 'data-read-only' is accessed with 'readOnly' - kebab-case to lower camelCase */
  var readOnly = (typeof data) === 'boolean' ? data : true;
  var config = {
     path : '/js/admin/',
     readOnly: readOnly,
  };
  var editor = editormd(TARGET_ID, config);
});

jQuery.data() - HTML5 data-* Attributes

joaquinalcerro

joaquinalcerro

Check if drab library can help you out.

Best regards,

supernova32

supernova32

+1 for data attributes. This is the easiest way to change a JS var from an Elixir view.

Alternatively you can use StimulusJS. It’s a great way to structure your JS code.

If you want to take a look at a non trivial example, have a look at these examples from AlloyCI (JS code, Elixir template code)

The Charts JS controller is used to create a dynamically generated line chart, based on the amount of builds a project has had, and it is used across admin view, and regular user view.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement