File size: 5,667 Bytes
1abfaca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Copyright (c) 2008-present The Open Source Geospatial Foundation
 *
 * Published under the BSD license.
 * See https://github.com/geoext/geoext2/blob/master/license.txt for the full
 * text of the license.
 */

/*
 * @requires GeoExt/Version.js
 */

/**
 * A data proxy for use with OpenLayers.Protocol objects.
 *
 * @class GeoExt.data.proxy.Protocol
 */
Ext.define('GeoExt.data.proxy.Protocol', {
    extend: 'Ext.data.proxy.Server',
    requires: [
        'GeoExt.Version'
    ],
    alias: 'proxy.gx_protocol',

    config:  {

        /**
         * The protocol used to fetch features.
         *
         * @cfg {OpenLayers.Protocol}
         */
        protocol: null,

        /**
         * Abort any previous request before issuing another.
         *
         * @cfg {Boolean}
         */
        abortPrevious: true,

        /**
         * Should options.params be set directly on options before passing it
         * into the protocol's read method?
         *
         * @cfg {Boolean}
         */
        setParamsAsOptions: false,

        /**
         * The reader to use.
         *
         * @cfg {Ext.data.reader.Reader}
         */
        reader: {}

    },

    /**
     * We need to override this as the OpenLayers classes passed as configs
     * loose their class-nature and seem to be copied by ExtJS somewhere.
     *
     * We deal with this elsewhere in a different manner and should see if
     * we can either share code or get rid of this special handling all
     * together. The problem isn't reproducible for other 'classes' with a
     * similar inheritance strategy as OpenLayers 2 has.
     *
     * TODO Find a way to have this functionality shared or get rid of it.
     *
     * @param {Object} config the configuration as passed by the user.
     */
    initConfig: function(config){
        var me = this,
            cfg = config || {},
            prefix = me.$configPrefixed ? '_' : '',
            olConfigs = [
                'protocol'
            ];
        Ext.each(olConfigs, function(olConfig){
            if (cfg[olConfig]) {
                me[prefix + olConfig] = cfg[olConfig];
                delete cfg[olConfig];
            }
        });
        me.callParent([cfg]);
    },

    model: 'Ext.data.Model',

    /**
     * The response returned by the read call on the protocol.
     *
     * @property {OpenLayers.Protocol.Response}
     * @private
     */
    response: null,

    /**
     * Send the request.
     *
     * @param {Ext.data.Operation} operation The Ext.data.Operation object.
     * @param {Function} callback The callback function to call when the
     *     operation has completed.
     * @param {Object} scope The scope in which to execute the callback.
     * @private
     */
    doRequest: function(operation, callback, scope) {
        var me = this,
            operationParams,
            params,
            request,
            o,
            cb,
            options;

        if(GeoExt.isExt5){
            callback = callback || operation.getCallback();
            scope = scope || operation.getScope();
            operationParams = operation.getParams() || {};
        } else {
            operationParams = operation.params || {};
        }

        params = Ext.applyIf(operationParams, me.extraParams || {});

        //copy any sorters, filters etc into the params so they can be sent over
        //the wire
        params = Ext.applyIf(params, me.getParams(operation));

        o = {
            params: params || {},
            operation: operation,
            request: {
                callback: callback,
                scope: scope,
                arg: operation.arg || operation.config.arg
            },
            reader: me.getReader()
        };
        cb = OpenLayers.Function.bind(me.loadResponse, me, o);
        if (me.getAbortPrevious()) {
            me.abortRequest();
        }
        options = {
            params: params,
            callback: cb,
            scope: me
        };
        Ext.applyIf(options, operation.arg || operation.config.arg);
        if (me.getSetParamsAsOptions() === true) {
            Ext.applyIf(options, options.params);
            delete options.params;
        }

        me.response = me.getProtocol().read(options);
    },

    /**
     * Called to abort any ongoing request.
     *
     * @private
     */
    abortRequest: function() {
        var me = this;
        if (me.response) {
            me.getProtocol().abort(me.response);
            me.response = null;
        }
    },

    /**
     * Handle response from the protocol.
     *
     * @param {Object} o
     * @param {OpenLayers.Protocol.Response} response
     * @private
     */
    loadResponse: function(o, response) {
        var me = this,
            operation = o.operation,
            scope = o.request.scope,
            callback = o.request.callback,
            result;
        if (response.success()) {
            result = o.reader.read(response.features || response);
            Ext.apply(operation, {
                response: response,
                resultSet: result
            });

            if (GeoExt.isExt4) {
                operation.commitRecords(result.records);
            } else {
                operation.setRecords(result.records);
            }

            operation.setSuccessful();
            operation.setCompleted();
        } else {
            me.setException(operation, response);
            me.fireEvent('exception', this, response, operation);
        }
        if (typeof callback == 'function') {
            callback.call(scope || me, operation);
        }
    }
});