好的,我被困在ExtJs的基本任务上。我正在编写一个简单的登录脚本,该脚本将用户名和密码组合发送到RESTful Web服务,并在凭据正确的情况下接收GUID。
我的问题是,我使用模型代理还是商店代理?
据我了解,模型代表单个记录,而商店则用于处理包含多个记录的数据集。如果这是正确的,那么似乎应该使用模型代理。
遵循Sencha在http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.Model上的文档后,代码将如下所示:
Ext.define('AuthenticationModel', {
extend: 'Ext.data.Model',
fields: ['username', 'password'],
proxy: {
type: 'rest',
url : '/authentication'
}
});
//get a reference to the authentication model class
var AuthenticationModel = Ext.ModelManager.getModel('AuthenticationModel');
到目前为止,一切正常,直到下一步:
//Use the configured RestProxy to make a GET request
AuthenticationModel.load('???', {
success: function(session) {
console.log('Login successful');
}
});
Model类的load()方法是一个静态调用,需要一个唯一的标识符。登录通常取决于两个因素,用户名和密码。
因此,似乎存储代理是验证ExtJS中某人的用户名和密码凭据组合的唯一方法。有人可以验证和解释吗?任何帮助理解这一点将不胜感激。
最佳答案
您只需要了解以下内容:
如果您为此配置了一个商店,那么商店将使用它自己的代理
实例,如果没有,他从模型中获取代理。
因此,您可以轻松地使用两种代理配置来启用存储上的多CRUD操作和模型上的单CRUD操作。请注意,模型的静态加载方法需要模型id
,因为它应该仅通过一个ID加载模型(是的,不支持复合键)。您还必须在回调中获取模型实例(与您一样)。
返回您的用户名/密码问题
您可以使用自定义“ loadSession”方法应用会话模型
loadSession: function(username,password, config) {
config = Ext.apply({}, config);
config = Ext.applyIf(config, {
action: 'read',
username: username,
password: password
});
var operation = new Ext.data.Operation(config),
scope = config.scope || this,
callback;
callback = function(operation) {
var record = null,
success = operation.wasSuccessful();
if (success) {
record = operation.getRecords()[0];
// If the server didn't set the id, do it here
if (!record.hasId()) {
record.setId(username); // take care to apply the write ID here!!!
}
Ext.callback(config.success, scope, [record, operation]);
} else {
Ext.callback(config.failure, scope, [record, operation]);
}
Ext.callback(config.callback, scope, [record, operation, success]);
};
this.getProxy().read(operation, callback, this);
}
现在调用它而不是加载。