﻿Ext.namespace('TNRIS');
/******************************************************
 * TNRIS.FunctionProxy allows a datastore to call ASP.net
 *     page callback methods
 *****************************************************/

TNRIS.FunctionProxy = function(config){
    this.method = 'pageMethod';
    Ext.apply(this, config);
    
    TNRIS.FunctionProxy.superclass.constructor.call(this);
};

Ext.extend(TNRIS.FunctionProxy, Ext.data.DataProxy, 
    /** @scope TNRIS.FunctionProxy */
    {
    load : function(params, reader, callback, scope, arg){
        if(this.fireEvent("beforeload", this, params) !== false){
            var  o = {
                params : params || {},
                request: {
                    callback : callback,
                    scope : scope,
                    arg: arg
                },
                reader: reader
            };

            if ($H(params).keys().length != 0) {
                if (this.method == 'pageMethod') {
                    PageMethods[this.findBy](params, function(result) {
                        this.callbackResponse(o, result);
                    }.bind(this));
                } else {
                    this.activeRequest = CallbackMethods[this.findBy](params, function(result) {
                        this.callbackResponse(o, result);
                    }.bind(this));
                }
            } else {
                if (this.method == 'pageMethod') {
                    PageMethods[this.findAll](function(result) {
                        this.callbackResponse(o, result);
                    }.bind(this));
                } else {
                    this.activeRequest = CallbackMethods[this.findAll](function(result) {
                        this.callbackResponse(o, result);
                    }.bind(this));
                }
            }
        }else{
            callback.call(scope||this, null, arg, false);
        }
    },

    callbackResponse : function(o, response){
        delete this.activeRequest;
        if(response.error){
            this.fireEvent("loadexception", this, o, response);
            o.request.callback.call(o.request.scope, null, o.request.arg, false);
            return;
        }
        var result;
        try {
            if (response.fields.length > 0) {
                o.reader.recordType = Ext.data.Record.create(response.fields);
                /* CONFIG: For records to be defined by the server code, both the store and the reader have to be given a new record definition.  For sorting 
                    to work, functionProxy.store must be defined so the store can be given the correct field list */
                if (this.store != null) {
                    this.store.recordType = o.reader.recordType;
                    this.store.fields = this.store.recordType.prototype.fields;
                }
            }
            result = o.reader.readRecords(response.data);
            if (response.totalRows != -1) {
                result.totalRecords = response.totalRows;
            }
        }catch(e){
            this.fireEvent("loadexception", this, o, response, e);
            o.request.callback.call(o.request.scope, null, o.request.arg, false);
            return;
        }
        this.fireEvent("load", this, o, o.request.arg);
        o.request.callback.call(o.request.scope, result, o.request.arg, true);
    }
});

if (typeof(Sys) !== "undefined") { Sys.Application.notifyScriptLoaded(); }

