Note that there are some explanatory texts on larger screens.

plurals
  1. POValidating Data in form before adding data to backbone collection
    primarykey
    data
    text
    <p>Hi i am a newbie to backbone. I am trying to follow <a href="http://listen-dom-events-backbone.herokuapp.com/" rel="nofollow">http://listen-dom-events-backbone.herokuapp.com/</a>. I edited the html so as to input three attributes : Name age and occupation</p> <pre><code>&lt;form id="addPerson" action=""&gt; &lt;input type="text" placeholder="Name of the person" id="name"&gt; &lt;input type="text" placeholder="Age" id="age"&gt; &lt;input type="text" placeholder="Occupation" id="occ"&gt; &lt;input type="submit" value="Add Person"&gt; &lt;/form&gt; &lt;script id="personTemplate" type="text/template"&gt; &lt;span&gt;&lt;strong&gt;&lt;%= name %&gt;&lt;/strong&gt; (&lt;%= age %&gt;) - &lt;%= occupation %&gt;&lt;/span&gt; &lt;button class="edit"&gt;Edit&lt;/button&gt; &lt;button class="delete"&gt;Delete&lt;/button&gt; &lt;/script&gt; </code></pre> <p>And the Validation of my backbone is something like this.</p> <pre><code>App.Views.AddPerson = Backbone.View.extend({ el: '#addPerson', events: { 'submit': 'submit' }, submit: function(e) { e.preventDefault(); var newPersonName = $(e.currentTarget).find('input[type=text]').val(); var newage = $(e.currentTarget).find(age).val(); var newocc = $(e.currentTarget).find(occ).val(); var person = new App.Models.Person({name: newPersonName, age: newage, occupation: newocc}); // Only when the data exists it has to be added to the collection // This is what i tried // Method 1: var attributes = person.attributes(); validate: function(){ if(attributes.newage ==null){alert("Please Enter Age")} if(attributes.newocc ==null){alert("Please enter occupation")} } //Method 2 var attributes = person.attributes(); validate: function(attributes){ if(attributes.newage !=null){person.set({age: newage});} if(attributes.newocc !=null){person.set({occupation: newocc}); } // Only if everything above is correct this value should be returned this.collection.add(person); } </code></pre> <p>});</p> <p>Am i doing this right or there is something wrong ?</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.
    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