Note that there are some explanatory texts on larger screens.

plurals
  1. POusing underscore templates with backbone.js
    primarykey
    data
    text
    <p>I'm trying to create a page using backbone.js and underscore.js. I want the page to have a button, that when pressed, adds the contents of a form to an empty tag on the page using a template that I have defined.</p> <p>Here's my empty tag where I want the template stuff to go after the button is pressed:</p> <pre><code>&lt;div id="playView"&gt;&lt;/div&gt; &lt;input type="button" id="addQuestion" value="Add Question" /&gt; </code></pre> <p>Here's my template where the form data should go:</p> <pre><code>&lt;script id="viewTemplate" type="text/template"&gt; &lt;div id="stemView"&gt;{{ Stem }}&lt;/div&gt; &lt;label for="answer1"&gt; a)&lt;/label&gt;&lt;div&gt;{{ answer1View }}&lt;/div&gt; &lt;label for="answer2"&gt; b)&lt;/label&gt;&lt;div&gt;{{ answer2View }}&lt;/div&gt; &lt;label for="answer3"&gt; c)&lt;/label&gt;&lt;div&gt;{{ answer3View }}&lt;/div&gt; &lt;label for="answer4"&gt; d)&lt;/label&gt;&lt;div&gt;{{ answer4View }}&lt;/div&gt; &lt;/script&gt; </code></pre> <p>And here's the form that the user will fill out:</p> <pre><code>&lt;form&gt; &lt;textarea id="stem"&gt;&lt;textarea&gt; &lt;textarea id="answer1"&gt;&lt;/textarea&gt; &lt;textarea id="answer2"&gt;&lt;/textarea&gt; &lt;textarea id="answer3"&gt;&lt;/textarea&gt; &lt;textarea id="answer4"&gt;&lt;/textarea&gt; &lt;/form&gt; </code></pre> <p>And here is the relevant backbone.js code. My question is, what do I do in the event? How do I get the data from the form, then into the viewTemplate, and then into the empty div tag(playView)?</p> <pre><code>wizard.PlayView = Backbone.View.extend({ initialize: function () { this.model.bind('change', _.bind(this.render, this)); }, el: '#playView', events: { 'click button#addQuestion': 'addQuestion' } addQuestion: function () { $(this.el).append(); } }); </code></pre>
    singulars
    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