Advertisement
Guest User

Untitled

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