ExtJs模型代理与商店代理

mrt*_*web 7 model extjs store restful-authentication extjs4

好吧,我坚持认为应该是ExtJs中的基本任务.我正在编写一个简单的登录脚本,它将用户名和密码组合发送到RESTful Web服务,如果凭据正确,则会收到GUID.

我的问题是,我使用模型代理还是商店代理?

据我所知,Models表示单个记录,而Stores用于处理包含多个记录的数据集.如果这是正确的,那么看起来模型代理就是要走的路.

根据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');
Run Code Online (Sandbox Code Playgroud)

到目前为止一切正常,直到下一步:

//Use the configured RestProxy to make a GET request
AuthenticationModel.load('???', {
    success: function(session) {
        console.log('Login successful');
    }
});
Run Code Online (Sandbox Code Playgroud)

Model类的load()方法是一个期望单个唯一标识符的静态调用.登录通常取决于两个因素,用户名和密码.

因此,似乎存储代理是在ExtJS中验证某人的用户名和密码凭证组合的唯一方法.有人可以验证和解释吗?任何帮助理解这一点将不胜感激.

sra*_*sra 7

您只需要知道以下内容:

如果您为此实例配置了一个代理,那么商店将使用它自己的代理,如果没有,他将从模型中获取代理.

因此,您可以轻松使用两个代理配置来启用商店上的多CRUD操作和模型上的单CRUD操作.请注意,Model的静态加载方法需要模型,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);
}
Run Code Online (Sandbox Code Playgroud)

现在调用它而不是加载.