0

I created a simple login page using extjs MVC to understand MVC architecture of extjs. As you can see below, I am trying to get the json data into the store and then I will check each username and password in that data with the entered login credentials. The thing in which I am confused right now is that, how to check the username and password from the retrieved json data present in store folder into the view folder? (Below code is only the related code with the problem)

I aware that this could invoke security threats, as I am checking on client side.

'view' folder --> Code.js

function checkJson(username, password){
    //if matched, return true.
    //else, return false.
}

'model' folder --> Code.js

Ext.define('AM.model.User', {
   extend: 'Ext.data.Model',
   fields: ['name', 'email']
});

'store' folder --> Code.js

Ext.define('LoginPage.store.Code', {
   extend: 'Ext.data.Store',
   model: 'LoginPage.model.Code',
   autoLoad: true,


   proxy: {
        type: 'ajax',
        api: {
            read: 'data/loginResponse.json',
            update: 'data/checkCredentials.json'  //Contains:  {"success": true}
        },
        reader: {
            type: 'json',
            root: 'loginResponse',
            successProperty: 'success'
        }
   }
});

loginResponse.json

{
"form": {
   "login": [
     {
       "username": "venkat",
       "password": "123"
     },
     {
       "username": "admin",
       "password": "345"
     }
   ]
}
Mr_Green
  • 40,727
  • 45
  • 159
  • 271

1 Answers1

1

You should put your checking part of the code to the Controller (views are for presentation). In view define some form with login and password fields. In Controller catch click event on form OK (Login) button, get form values (login + password), then use Ext.data.Store.query() method to find wether credentials fits or not like:

Look here for examples how to use controllers in MVC to catch events;

In your Controller put:

init: function() {
    this.control({
        '#form_ok_button': { // this is the `id` property of your form's Login button
            click: function(button) {
                 var fValues = button.up('form').getValues(); // Assume your button is bound to the form
                 // Or you can use `Controller.refs` property (http://docs.sencha.com/ext-js/4-1/#!/api/Ext.app.Controller-cfg-refs) to get form
                 var matched = store.query('username', fValues.username);
                 if(matched.length && matched[0].get('password') === fValues.password) {
                     // login OK!
                 }
            }
        }
    });
},

How to use refs (in Controller):

refs: [
   { ref: 'usernameField', selector: '#username_field' }, // username field id is "username_field"
   { ref: 'passwordField', selector: '#password_field' }, // password field id is "password_field"
],

init: function() {  
   this.control({
       '#form_ok_button': {
            click: function() {
                // with `refs` autogetters are created for every `ref`:
                var username_field = this.getUsernameField();
                var password_field = this.getPasswordField();
            }
        }
   })
}

You can read about referencing here.

For every Store in Ext.app.Controller.stores array autogetters are created too (in your case for Code store use this.getCodeStore() inside controller).

Here is the flow:

  1. You get username and password field values with this.getUsernameField() and this.getPasswordField();
  2. You query() store for username
  3. If username exist in store, you check if password fits.
s.webbandit
  • 16,332
  • 16
  • 58
  • 82
  • Thanks for response. What is `form` here? – Mr_Green Feb 13 '13 at 08:26
  • +1 Thank you for making me understand the structure clearly :). The thing is that my button is not bound to any form, it is just present in a container. The container also has two textfields in which the username and password are present. I am getting those textfield values and trying to match in a function i.e `function checkJson(username, password)` as shown in my post. – Mr_Green Feb 13 '13 at 08:50
  • Also, I am getting an error `Uncaught ReferenceError: store is not defined ` how to define the store variable?(`var store = ?`) please help I am new. – Mr_Green Feb 13 '13 at 08:56
  • Thanks, I just added indentation for my understanding. (hope you don't mind) – Mr_Green Feb 13 '13 at 09:31
  • Thank you for letting me know great stuff in extjs but I think you haven't understand what I am trying to get here. I want to check each and every username and password values present in the 'store' folder, Code.js (retrieved from json) in a function present in 'controller` folder (changed from 'view' folder to 'controller' folder on your good suggestion). The function is `function checkJson(username, password)` in 'controller' folder. This function returns true if username and password matches, else false. Sorry, if I made you confused earlier by my poor english. :) – Mr_Green Feb 13 '13 at 09:40
  • You want to check check each and every username and password values present in the 'store' against what? – s.webbandit Feb 13 '13 at 09:50
  • against the user entered values in the text fields (ie. username and password textfields) – Mr_Green Feb 13 '13 at 09:51
  • Sorry, ya it does. :). The thing is that I can't understand how you are calling the `store` in the 'controller' folder without even declaring it as variable. when I tried your code, I am getting error --> `Uncaught ReferenceError: store is not defined `. I am not sure how to declare it.. `var store = ?`. – Mr_Green Feb 13 '13 at 09:54
  • Explanation is in my answer - "For every Store in ..." – s.webbandit Feb 13 '13 at 09:56
  • I tried `var store = this.getCodeStore();`, I am getting error --> `Uncaught TypeError: Object [object Object] has no method 'getCodeStore'` . (The store is extended to `Ext.data.Store`.) – Mr_Green Feb 13 '13 at 10:07
  • I even tried `var store = this.getStore()` which is showing error --> `Uncaught TypeError: Cannot call method 'indexOf' of undefined ` Please help. – Mr_Green Feb 13 '13 at 10:10
  • Atlast, I tried `var store = Ext.create('LoginPage.store.Code');` which is showing `0` items when I checked in Chrome console. Is my 'store' folder Code.js file is ok? or I am declaring the variable wrong. – Mr_Green Feb 13 '13 at 10:18
  • Please check my edited post. Added the 'model' folder's **Code.js** file too. The 'controller' folder's **Code.js** file is similar to what you explained here. – Mr_Green Feb 13 '13 at 10:28
  • post your `Controller.js` file. Before read carefully - http://docs.sencha.com/ext-js/4-1/#!/guide/mvc_pt1, http://docs.sencha.com/ext-js/4-1/#!/guide/mvc_pt2, http://docs.sencha.com/ext-js/4-1/#!/guide/mvc_pt3 – s.webbandit Feb 13 '13 at 12:35
  • sorry for late response.. Please check the following link http://stackoverflow.com/q/14852700/1577396. I added all the files here.. If you get confused please tell me. I will look after those links which you provided now.. But I don't have time right now to go through them. As I want to submit this asap. – Mr_Green Feb 13 '13 at 12:50
  • please have a look at this link http://stackoverflow.com/q/14854089/1577396. (I deleted the previous link as it was messy to understand) – Mr_Green Feb 13 '13 at 12:55