Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExtJs Model Proxy vs. Store Proxy

OK, I'm stuck on what should be a basic task in ExtJs. I'm writing a simple login script that sends a user name and password combination to a RESTful web service and receives a GUID if the credentials are correct.

My question is, do I use a Model Proxy or a Store Proxy?

To my understanding, Models represent a single record, whereas Stores are for handling sets of data containing more than one record. If this is correct then it would seem that a Model proxy is the way to go.

Following Sencha's documentation at http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.Model the code would look something like this:

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');

So far everything is OK, until the next step:

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

The load() method for the Model class is a static call expecting a single unique identifier. Logins typically depend upon two factors, username and password.

So it appears Store proxies are the only way to validate someone's username and password credential combination in ExtJS. Can someone verify and explain? Any help to understand this would be greatly appreciated.

like image 569
mrtedweb Avatar asked Feb 18 '14 05:02

mrtedweb


1 Answers

You just need to know the following:

The store will use it's own proxy if you configured one for this instance and if not he takes the proxy from the model.

So you can easily go with two proxy configurations to enable the multi-CRUD operations on the store and the single-CRUD operations on the Models. Note the the static load method of the Model expects the model id because it is supposed to load a model by just one Id (yes, composite keys are not supported). You will also have to fetch the model instance in the callback (As you did).

Back to your Username/password problem

You may apply your session Model with a custom 'loadSession' method

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);
}

Now call this instead of load.

like image 124
sra Avatar answered Oct 02 '22 20:10

sra