Skip to content

Instantly share code, notes, and snippets.

@prrraveen
Created July 30, 2015 08:18
Show Gist options
  • Save prrraveen/2dbb1b6ed5363d7e26c2 to your computer and use it in GitHub Desktop.
Save prrraveen/2dbb1b6ed5363d7e26c2 to your computer and use it in GitHub Desktop.
var regi_view = Backbone.View.extend({
el : '#page',
initialize : function()
{
this.render()
this.regi_model = new RegiModel()
this.regi_model.on('invalid',this.on_model_invalid,this)
},
render : function(){
var template = window['JST']['templates/regi.tpl']();
this.$el.html(template)
this.ui_initialize()
},
events: {
"click .submit": "onFormSubmit",
'focus input,textarea': function(e){ this.reset_input_errors(e.target); },
},
ui_initialize : function()
{
$('body').addClass('login-body')
},
onFormSubmit: function(e)
{
var _this = this
$('.login-wrap').find('.form-control').each(function()
{
_this.regi_model.set(this.id, this.value);
})
this.regi_model.save(null, {
success: function(model, response, options)
{
window.tradex.user = new User(response.payload.user)
Backbone.history.navigate('dashboard' , { trigger : true})
},
error: function(model, response, options) {
// debugger
_this.reset_all_errors()
$.map(response.responseJSON, function(val , key){
_this.show_input_errors(target = key , errors = val )
})
// $('.incorrect').html(window['JST']['templates/form_error.tpl']({errors: response.responseJSON}))
}
})
},
on_model_invalid : function(model,errors)
{
var _this = this
_.each(errors,function(error)
{
_this.show_input_errors(error.field, error.error);
})
},
show_input_errors: function(target, errors)
{
var target = $('.login-wrap').find('#'+target);
var errorsHTML = '';
this.reset_input_errors(target);
errorsHTML += this.templates.error({error: errors});
target.addClass('incorrect');
target.parent().prepend(errorsHTML);
},
reset_input_errors: function(target) {
$(target).removeClass('incorrect')
$(target).parent().find('.error').remove();
},
reset_all_errors: function() {
$('.login-wrap').find('.error').remove();
$('.login-wrap').find('.incorrect').removeClass('incorrect')
},
templates :
{
'error' : _.template('<span class="error"><%= error %></span>')
},
on_input_change : function(e){
target = $(e.target)
target.removeClass('incorrect');
target.parent().append('');
}
})//Loginview
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment