Reputation: 1735
I'm learning backbone and have a very simple application that creates a contact list. So far I have first name, last name and email address. I want to create a validation such that it checks if the email address already exists in the collection before creating a new one. This is how I'm creating collection.
this.collection.create({
FirstName: this.first_name.val(),
LastName: this.last_name.val(),
Email: this.email_address.val()
},
{
wait: true,
success: function (resp) {
console.log('successfully saved to the database');
},
error: function (errors) {
console.log('email address exists');
}
});
How can I achieve this?
Upvotes: 0
Views: 761
Reputation: 14990
If the main thing in the collection that has to be unique is the email address you could "pluck" the email address and ensure that none are returned that match the new one you want to add.
var emails = this.collection.pluck("Email")
if(!_.contains(emails, targetEmailAddress)
{
//you have a new email address so go ahead and create the model
}
Upvotes: 2