如何在控制器中引用商店 - > Code.js文件? [英] How to refer the store in Controller --> Code.js file?
问题描述
'store'文件夹 - > Code.js
Ext.define('LoginPage.store.Code',{
extends:'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'文件夹 - > Code.js
Ext.define('LoginPage.model.Code',{
extends:'Ext.data.Model',
字段:[
{name:'username',type:'string'},
{name:'password',type:'string'}
]
}) ;
现在如何在控制器文件夹的Code.js中声明存储的变量?
我的JSON格式如下所示,格式如下:
loginResponse.json / p>
{
login:[
{
username:venkat
密码:123
},
{
用户名:管理员,
密码:345
} ,
{
username:sam,
password:234
},
{
username:paul ,
密码:456
}
]
}
我试过:
var store = Ext.create('LoginPage.store.Code');
//显示没有错误,并且我通过Chrome控制台检查时看不到任何项目
//。
当我将创建的商店变量保存在中时,Chrome控制台显示以下消息console.log(store);
编辑:以下函数始终返回false,即使凭据正确
checkJson:function(username,password){
var store = Ext.create('LoginPage store.Code');
var matched = store.query('username',username);
//console.log(matched);
if(matched.length&& match [0] .get('password')=== password){
return true;
}
else {
return false;
}
}
匹配 Chrome控制台(用户名 =venkat,密码 =123)
您首先需要知道商店是否已经加载。一旦商店加载,您可以像这样查看凭据
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');
我为您提供的数据准备了一个示例,但使用本地商店。我希望它可以帮助你
http:/ /jsfiddle.net/alexrom7/chn8Z/1/
编辑:我没有看到标题中的问题,或者我不知道你是否改变了:p。在这种情况下,您可以通过执行此操作访问控制器文件中的代码存储区。
Ext.getStore('Code');
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);
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")
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');
这篇关于如何在控制器中引用商店 - > Code.js文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!