我几乎是开发sencha的新手,所以我真的希望你可以帮助我。
下面你可以看到我的LogIn功能'onSignInCommand',它实际上正在运行。 通过将登录请求发送到服务器,我得到了以下JSONP文件。 我喜欢将JSON文件提供的用户信息(名称,年龄,...)加载到JSON存储中。我怎么处理这个?
{"msg": "OK",
"user": {
"token": "xyz",
"firstName": "Bertha",
"lastName": "Muster",
"age": "24",
}
}
==========
onSigninCommand: function(view, username, password) {
var data1 = username;
var data2 = password;
var me = this;
loginView = me.getLoginView();
if (username.length === 0 || password.length === 0) {
loginView.showSignInFailedMessage('keine eingabe');
return;
}
console.log('Username: ' + username + '\n' + 'Password: ' + password);
Ext.util.JSONP.request({
url: 'http://127.0.0.1:4712/talentcommunity/getuserinfo',
headers: {
'content-type': 'application/x-www-form-urlencoded ; charset=utf-8'
},
method: 'post',
params: {
user: data1,
pw: data2
},
callbackKey: 'callback',
success: function(response) {
console.log(response);
var loginResponse = response;
if (loginResponse.msg == "OK") {
me.signInSuccess();
} else {
loginView.showSignInFailedMessage('token null.');
}
},
failure: function(response) {
loginView.showSignInFailedMessage('token null.');
}
});
如你所说,我在'onSignInCommand'中编辑了成功函数。但它不执行XXX.store.MyJsonPStore.loadData(jsonResponse)。
商店本身在单独的文件中定义,见下文。
你能帮助我一次吗?
onSigninCommand: function(view, username, password) {
var data1 = username;
var data2 = password;
var me = this;
loginView = me.getLoginView();
if (username.length === 0 || password.length === 0) {
loginView.showSignInFailedMessage('keine eingabe');
return;
}
console.log('Username: ' + username + '\n' + 'Password: ' + password);
Ext.util.JSONP.request({
url: 'http://127.0.0.1:4712/talentcommunity/getuserinfo',
headers: {
'content-type': 'application/x-www-form-urlencoded ; charset=utf-8'
},
method: 'post',
params: {
user: data1,
pw: data2
},
callbackKey: 'callback',
success: function (response) {
console.log(response);
var loginResponse = response;
if (loginResponse.msg == "OK") {
var jsonResponse = Ext.JSON.decode(response.responseText);
XXXne.store.MyJsonPStore.loadData(jsonResponse);
me.signInSuccess();
}
else {
loginView.showSignInFailedMessage('token null.');
}
},
failure: function(response) {
loginView.showSignInFailedMessage('token null.');
}
});
=============
Ext.define('xxx.store.MyJsonPStore', {
extend: 'Ext.data.Store',
alias: 'store.myStore',
requires: [
'xxx.model.user'
],
config: {
model: 'xxx.model.user',
storeId: 'myStore',
proxy: {
type: 'jsonp',
url: 'http://127.0.0.1/talentcommunity/getuserinfo',
autoAppendParams: false,
reader: {
type: 'json'
}
}
}
});
答案 0 :(得分:2)
参见sencha api:create model/store
<强> 1。使用相同的字段名称生成您的模型/商店:
// Set up a model to use in our Store
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
{name: 'username', type: 'string'},
{name: 'password', type: 'string'},
{name: 'age', type: 'int'},
{name: 'token', type: 'string'}
]
});
var myStore = Ext.create('Ext.data.Store', {
model: 'User',
});
<强> 2。在商店上加载数据:
On“Ext.util.JSONP.request”:
...
success: function(response) {
var jsonResponse = Ext.JSON.decode(response.responseText);
myStore.loadData(jsonResponse);
}
但我更喜欢在商店使用代理配置:
var myStore = Ext.create('Ext.data.Store', {
model: 'User',
proxy: {
type: 'ajax',
url : 'http://127.0.0.1:4712/talentcommunity/getuserinfo',
reader: {
type: 'json',
root: 'users'
}
}
}