Reputation: 1073
I have a backbone model:
define(function() {
var ContactModel = Backbone.Model.extend({
urlRoot: 'http://www.somedomain.com',
defaults : {
'name' : null,
'email': '',
'phone': '',
},
validate: function(attrs) {
var name_filter = /[a-zA-Z'.,-\s]+/;
var email_filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var errors = [];
if(attrs['name'].length<1){
errors.push({input_tag: 'name', error: 'Please enter your First Name'});
}
if(attrs['phone']==='' && attrs['email']===''){
//messages['name'] = 'You must include a phone number or email';
errors.push({input_tag: 'phone', error: 'You must include a phone number or email'});
}
if(attrs['email']!==''){
if (!email_filter.test(attrs.email)){
errors.push({input_tag: 'email', error: 'Please enter a valid email address'});
}
}
if(errors.length > 0){
return errors;
}
}
});
return ContactModel;
});
In my view, I set the attribute values like this:
this.model.set({
name:$('#name').val(),
phone:$('#phone').val(),
email:$('#email').val(),
});
I do the validation, then save the model with:
this.model.save({
success: function(model){
console.log('successfully saved and model is ');
console.log(model);
},
error: function(){
console.log('there was an error');
}
});
The model gets saved on the server, but success or error callbacks are never hit. What am I doing wrong?
Upvotes: 0
Views: 27
Reputation: 1073
Worked after I changed to:
this.model.save([], {
success: function(model){
console.log('successfully saved and model is ');
console.log(model);
},
error: function(){
console.log('there was an error');
}
});
Upvotes: 1