0

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

Mr_Green
  • 40,727
  • 45
  • 159
  • 271
  • Looks like the items are there. `totalCount` is `4`. you might need to read the documentation... [Ext.data.Store](http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Store) – CD.. Feb 13 '13 at 12:57
  • I am extending my store to `Ext.data.Store`.. (indentation was not good before) – Mr_Green Feb 13 '13 at 13:01
  • Well, remember that loading data in a store remotely is an async task...so the store will NOT be populated with data immediately upon creation since it needs to make the remote request, handle the response from the remote server, parse the response, and convert the data into model instances. So if you need to do stuff with the store *after* data has loaded, you need to hook into the load event of the store, and do your processing there. – existdissolve Feb 13 '13 at 13:01
  • 1
    Also, I would STRONGLY discourage you from continuing with this approach for login-checking. You should never return a password as part of the JSON response from the server, no matter how strongly hashed and salted it may be. – existdissolve Feb 13 '13 at 13:03
  • @existdissolve ya I am aware of that.. I am just trying to learn here. Thank you for your suggestion :) – Mr_Green Feb 13 '13 at 13:04
  • @existdissolve Can you please provide me a sample.. so that I can understand it clearly. – Mr_Green Feb 13 '13 at 13:06
  • First, read the docs for the load event: http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.AbstractStore-event-load – existdissolve Feb 13 '13 at 13:07
  • Then, add a listener to your store for the load event: listeners: { load: function( store, records, successful, opts ) { console.log( store ) // will have loaded records } } – existdissolve Feb 13 '13 at 13:08
  • @existdissolve still I can't see the items.. though the count is 4, which is correct. I am getting the same console message as I mentioned in my post. – Mr_Green Feb 13 '13 at 13:14
  • nah, it has data. I also pasted the json here. But I think I am doing some silly mistake here.. – Mr_Green Feb 13 '13 at 13:16
  • So if it has data (four records, right?), what's the problem? – existdissolve Feb 13 '13 at 13:17
  • @existdissolve I added the following code as edit.. That function is always returning false even if I give correct username and password credentials. – Mr_Green Feb 13 '13 at 13:19
  • @existdissolve In that function which I added now. I am trying to check each and every username and password present in the store with the username and password entered by the user. – Mr_Green Feb 13 '13 at 13:25
  • Are the model instances in the store correct? E.g., when you look at the items in "data", do they have the values you expect to be there from what you loaded? – existdissolve Feb 13 '13 at 13:26
  • @existdissolve yes everything is correct.. I will share the model pattern also. Please check the updated post. – Mr_Green Feb 13 '13 at 13:33
  • Not sure. Can you post the console.log( matched ) part? – existdissolve Feb 13 '13 at 13:42
  • @existdissolve I added the message showing by 'matched' in my edit section. Please check. – Mr_Green Feb 13 '13 at 14:14
  • You cannot use '===' to check a integer against a string... Use '==' and it should work. Or cast both to strings (var myVar = myVar + '';) – sra Feb 13 '13 at 19:42
  • @sra `match.length is always zero!!` (I can't understand why). So, I don't want to use that code because I can't understand it. Can you please tell me how to compare each set of credentials present in store with the user entered credentials(i.e., username and password). – Mr_Green Feb 14 '13 at 03:58

3 Answers3

1

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');
alexrom7
  • 864
  • 7
  • 12
  • I was not seeing your edit for this whole time :D. Thanks that worked. – Mr_Green Feb 14 '13 at 04:58
  • why only `return true` works in the match variable? why not `return 0` or something else when I tried? – Mr_Green Feb 14 '13 at 05:00
  • I used `data = data` because I'm using a local store. The variable **data** is a json that I just created in the code. The store you created seems fine, it loaded all the records (acording to the console.log(store) you posted). I think the problem in this case is in the Controller file, I will post how it should look like. – alexrom7 Feb 14 '13 at 05:48
  • Because we are using the **findBy** function of the store [link](http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Store-method-findBy) According to the documentation: _**findBy**( fn, [scope], [startIndex] ) : Number Find the index of the first matching Record in this Store by a function. If the function returns true it is considered a match._ So in the function I inserted as a parameter, only has to indicate if there is a match. If there is no match, it will return -1 – alexrom7 Feb 14 '13 at 05:56
0

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){

});

sushant jain
  • 213
  • 1
  • 4
  • 12
0

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.

Stephen Tremaine
  • 934
  • 6
  • 15