Note that there are some explanatory texts on larger screens.

plurals
  1. POGrouping javascript method functionality into a javascript "class"
    primarykey
    data
    text
    <p>Yes, I understand that JavaScript doesn't have "classes" per se, so I'll just throw that out there up front and move on.</p> <p>I have an app that has a left sidebar that lists a bunch of tags. I'm manipulating these tags via JavaScript and AJAX to add, remove, and update the tags.</p> <p>When I currently manipulate the tags, it goes something to the effect of (note this code is a dumbed-down version of what I'm doing, but conveys the point; also, I'm using the jQuery library):</p> <pre><code># HTML page... &lt;div id="tags"&gt; &lt;div class="tag" id="tag_1"&gt;tag text&lt;/div&gt; &lt;div class="tag" id="tag_2"&gt;tag text&lt;/div&gt; &lt;div class="tag" id="tag_3"&gt;tag text&lt;/div&gt; ... &lt;/div&gt; # Javascript to create new tag $("#tags").append("&lt;div class='tag' id='tag_15'&gt;text&lt;/div&gt;") # update a tag $("#tag_10").replace("&lt;div class='tag' id='tag_10'&gt;updated tag&lt;/div&gt;") # remove a tag $("#tag_10").remove() </code></pre> <p>As a side note, I'm using the Rails framework and some of the JS is created on the server via js.erb files and sent back to the client to evaluate. I'm aware of some concerns with servers generating JS, but it's OK in this situation.</p> <p>So I decided that I should group this functionality into a JavaScript "class" that controls the tags on my page.</p> <p>The issue is that I have a single tags sidebar, so I don't need multiple Tag objects...I just need a bunch of named methods that allow me to manipulate the tags on my page in an orderly fashion.</p> <p>I would like to be able to do things like:</p> <pre><code>Tag.append({ text: "tag text", id: &lt;id of tag&gt; }) =&gt; the append functionality above Tag.update(&lt;id to update&gt;, { &lt;hash of options&gt; }) Tag.remove(&lt;id of tag to remove&gt;) </code></pre> <p>I don't understand how I would achieve this functionality with JavaScript.</p> <p>My thoughts are doing something like:</p> <pre><code># "class" definition function Tag() { var methods = { init : function() { ... } append : function(options) { var element = &lt;...build DIV element...&gt;; $("#tags").append(element); } remove : function(domid) { $(domid).remove(); } } } # usage var tag = Tag.new() tag.remove("tag_10") ... </code></pre> <p>However, if I do it this way, will I have to create a new <code>var tag = Tag.new()</code> each time I want to reference the Tag class (since I'm sending some code back via the server)?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload