Advertisement
Guest User

Untitled

a guest
Mar 29th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  app.User = Backbone.Model.extend({
  2.  
  3.             default: {
  4.                 userName: '',
  5.                 password: ''
  6.             },
  7.  
  8.             type: 'post',
  9.             dataType: 'json',
  10.             //contentType: 'application/json',
  11.  
  12.             url: '/auth/ajaxLogin',
  13.  
  14.             validate: function (attribute) {
  15.                 if (attribute.userName.length === 0 || attribute.password.length === 0) {
  16.                     return 'Please, make sure that email or password is not empty!';
  17.                 }
  18.             },
  19.  
  20.             parse: function (response) {
  21.  
  22.                 if(response.message)
  23.                 {
  24.                     return warning.setAndShow(response.message);
  25.                 }
  26.  
  27.                 return window.location = (response.redirect);
  28.             }
  29.         });
  30.  
  31.         var user = new app.User();
  32.  
  33.         /**
  34.          * Displaying errors View:
  35.          */
  36.         app.warning = Marionette.ItemView.extend({
  37.  
  38.             el: '#warning',
  39.             template: false,
  40.  
  41.             ui: {
  42.                 textHolder: '#warningMessage'
  43.             },
  44.  
  45.             events: {
  46.                 'click .close': 'hideMe'
  47.             },
  48.  
  49.             setAndShow: function (message) {
  50.                 this.ui.textHolder.text(message);
  51.                 this.$el.show();
  52.             },
  53.  
  54.             hideMe: function () {
  55.                 this.$el.hide();
  56.             }
  57.         });
  58.  
  59.         var warning = new app.warning();
  60.  
  61.         /**
  62.          * Login View:
  63.          */
  64.         app.loginView = Marionette.ItemView.extend({
  65.  
  66.             el: '#loginForm',
  67.             template: false,
  68.             model: user,
  69.  
  70.             initialize: function () {
  71.                 this.ui.userName = $('#userName');
  72.  
  73.                 if (this.ui.userName.val() == '') {
  74.                     this.ui.userName.focus();
  75.                 }
  76.  
  77.                 //listen to errors in validation:
  78.                 this.listenTo(this.model, 'invalid', this.handleError);
  79.             },
  80.  
  81.             ui: {
  82.                 password: '#password',
  83.                 buttonSubmit: '#submitButton'
  84.             },
  85.  
  86.             events: {
  87.                 'click @ui.buttonSubmit': 'validateUser'
  88.             },
  89.  
  90.             handleError: function (model, error, options) {
  91.  
  92.                 return warning.setAndShow(error);
  93.             },
  94.  
  95.             validateUser: function (e) {
  96.  
  97.                 e.preventDefault();
  98.  
  99.                 this.model.save({
  100.                     userName: this.ui.userName.val().trim(),
  101.                     password: this.ui.password.val().trim()
  102.                 }, {
  103.                     validate: true
  104.                 });
  105.             }
  106.         });
  107.  
  108.         var loginView = new app.loginView();
  109.  
  110.         app.start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement