Mr_Green
Mr_Green

Reputation: 41832

How to refer the store in Controller --> Code.js file?

I am storing the JSON format in to Code.js of store folder.

'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: 'login',
           successProperty: 'success'
           }
    }
});

'model' folder --> Code.js

Ext.define('LoginPage.model.Code', {
   extend: 'Ext.data.Model',
   fields: [
       {name: 'username', type: 'string'},
       {name: 'password', type: 'string'}   
   ]
});

Now how to declare the variable of the store in Code.js of controller folder?

My JSON format is as shown below which is in correct format:

loginResponse.json

{
"login": [
  {
    "username": "venkat",
    "password": "123"
  },
  {
    "username": "admin",
    "password": "345"
  },
  {
    "username": "sam",
    "password": "234"
  },
  {
    "username": "paul",
    "password": "456"
  }
]
}

I tried :

var store = Ext.create('LoginPage.store.Code');  
//showing no errors and also I can't see any items in it
//when checked through Chrome console.

The chrome console shows the following message when I keep the created store variable in console.log(store);

enter image description here

EDIT: The following function is always returning false even if the credentials are correct

checkJson: function(username, password){    
    var store = Ext.create('LoginPage.store.Code');
    var matched = store.query('username', username);
    //console.log(matched);
    if(matched.length && matched[0].get('password') === password) {
         return true;
    }
    else{
        return false;
    }
 }  

matched variable message in Chrome console. (username = "venkat", password = "123")

enter image description here

Upvotes: 0

Views: 180

Answers (3)

alexrom7
alexrom7

Reputation: 864

As everyone said, you first need to know if the store has been loaded. Once the store is loaded you could check the credentials just like this

var match = codeStore.findBy(function(record,id) {
                if(record.get('username')==credentials.username
                       && record.get('password')==credentials.password) {
                    return true;
                }
            });

            if(match != -1) 
                alert('correct');
            else
                alert('incorrect');

I prepared an example with the data you provided but using a local store. I hope it can help you

http://jsfiddle.net/alexrom7/chn8Z/1/

EDIT: I didn't see the question in the title or I don't know if you changed :p. In that case, you can access the code store in your controller file by doing this

Ext.getStore('Code');

Upvotes: 1

Stephen Tremaine
Stephen Tremaine

Reputation: 954

I would start with checking in the network tab of chrome dev tools to make sure that your json file was correctly requested and returned. After that, I would check in store.data.items to see if the data was getting through but wasn't getting parsed correctly. And finally, I would add the success: true property to the json you return in the read call. That's the only way Ext knows everything on the server went as planned.

Upvotes: 0

sushant jain
sushant jain

Reputation: 213

You can use queryBy method. Specified function inside this method will be called with each record in this Store. and you can write your logic inside this function to check if records is exist or not.

store.queryBy(function(m,id){

});

Upvotes: 0

Related Questions