嗨,我是骨干的新手。我正在尝试关注http://listen-dom-events-backbone.herokuapp.com/。我编辑了 html 以便输入三个属性:姓名年龄和职业
<form id="addPerson" action="">
<input type="text" placeholder="Name of the person" id="name">
<input type="text" placeholder="Age" id="age">
<input type="text" placeholder="Occupation" id="occ">
<input type="submit" value="Add Person">
</form>
<script id="personTemplate" type="text/template">
<span><strong><%= name %></strong> (<%= age %>) - <%= occupation %></span>
<button class="edit">Edit</button>
<button class="delete">Delete</button>
</script>
我的主干的验证是这样的。
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);
}
});
我这样做是对的还是有问题?