| 1 |
define(["dojo", "dojox", "dojo/io/script", "dojo/data/util/sorter"], function(dojo, dojox) { |
| 2 |
|
| 3 |
return dojo.declare("dojox.data.SnapLogicStore", null, { |
| 4 |
Parts: { |
| 5 |
DATA: "data", |
| 6 |
COUNT: "count" |
| 7 |
}, |
| 8 |
|
| 9 |
url: "", |
| 10 |
|
| 11 |
constructor: function(args){ |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
if(args.url){ |
| 26 |
this.url = args.url; |
| 27 |
} |
| 28 |
this._parameters = args.parameters; |
| 29 |
}, |
| 30 |
|
| 31 |
_assertIsItem: function(item){ |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
if(!this.isItem(item)){ |
| 37 |
throw new Error("dojox.data.SnapLogicStore: a function was passed an item argument that was not an item"); |
| 38 |
} |
| 39 |
}, |
| 40 |
|
| 41 |
_assertIsAttribute: function( attribute){ |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
if(typeof attribute !== "string"){ |
| 47 |
throw new Error("dojox.data.SnapLogicStore: a function was passed an attribute argument that was not an attribute name string"); |
| 48 |
} |
| 49 |
}, |
| 50 |
|
| 51 |
getFeatures: function(){ |
| 52 |
|
| 53 |
|
| 54 |
return { |
| 55 |
'dojo.data.api.Read': true |
| 56 |
}; |
| 57 |
}, |
| 58 |
|
| 59 |
getValue: function(item, attribute, defaultValue){ |
| 60 |
|
| 61 |
|
| 62 |
this._assertIsItem(item); |
| 63 |
this._assertIsAttribute(attribute); |
| 64 |
var i = dojo.indexOf(item.attributes, attribute); |
| 65 |
if(i !== -1){ |
| 66 |
return item.values[i]; |
| 67 |
} |
| 68 |
return defaultValue; |
| 69 |
}, |
| 70 |
|
| 71 |
getAttributes: function(item){ |
| 72 |
|
| 73 |
|
| 74 |
this._assertIsItem(item); |
| 75 |
return item.attributes; |
| 76 |
}, |
| 77 |
|
| 78 |
hasAttribute: function(item, attribute){ |
| 79 |
|
| 80 |
|
| 81 |
this._assertIsItem(item); |
| 82 |
this._assertIsAttribute(attribute); |
| 83 |
for(var i = 0; i < item.attributes.length; ++i){ |
| 84 |
if(attribute == item.attributes[i]){ |
| 85 |
return true; |
| 86 |
} |
| 87 |
} |
| 88 |
return false; |
| 89 |
}, |
| 90 |
|
| 91 |
isItemLoaded: function(item){ |
| 92 |
|
| 93 |
|
| 94 |
return this.isItem(item); |
| 95 |
}, |
| 96 |
|
| 97 |
loadItem: function(keywordArgs){ |
| 98 |
|
| 99 |
|
| 100 |
}, |
| 101 |
|
| 102 |
getLabel: function(item){ |
| 103 |
|
| 104 |
|
| 105 |
return undefined; |
| 106 |
}, |
| 107 |
|
| 108 |
getLabelAttributes: function(item){ |
| 109 |
|
| 110 |
|
| 111 |
return null; |
| 112 |
}, |
| 113 |
|
| 114 |
containsValue: function(item, attribute, value){ |
| 115 |
|
| 116 |
|
| 117 |
return this.getValue(item, attribute) === value; |
| 118 |
}, |
| 119 |
|
| 120 |
getValues: function(item, attribute){ |
| 121 |
|
| 122 |
|
| 123 |
this._assertIsItem(item); |
| 124 |
this._assertIsAttribute(attribute); |
| 125 |
var i = dojo.indexOf(item.attributes, attribute); |
| 126 |
if(i !== -1){ |
| 127 |
return [item.values[i]]; |
| 128 |
} |
| 129 |
return []; |
| 130 |
}, |
| 131 |
|
| 132 |
isItem: function(item){ |
| 133 |
|
| 134 |
|
| 135 |
if(item && item._store === this){ |
| 136 |
return true; |
| 137 |
} |
| 138 |
return false; |
| 139 |
}, |
| 140 |
|
| 141 |
close: function(request){ |
| 142 |
|
| 143 |
|
| 144 |
}, |
| 145 |
|
| 146 |
_fetchHandler: function( request){ |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
var scope = request.scope || dojo.global; |
| 156 |
|
| 157 |
if(request.onBegin){ |
| 158 |
|
| 159 |
request.onBegin.call(scope, request._countResponse[0], request); |
| 160 |
} |
| 161 |
|
| 162 |
if(request.onItem || request.onComplete){ |
| 163 |
var response = request._dataResponse; |
| 164 |
|
| 165 |
if(!response.length){ |
| 166 |
request.onError.call(scope, |
| 167 |
new Error("dojox.data.SnapLogicStore: invalid response of length 0"), |
| 168 |
request); |
| 169 |
return; |
| 170 |
}else if(request.query != 'record count'){ |
| 171 |
|
| 172 |
|
| 173 |
var field_names = response.shift(); |
| 174 |
|
| 175 |
var items = []; |
| 176 |
for(var i = 0; i < response.length; ++i){ |
| 177 |
if(request._aborted){ |
| 178 |
break; |
| 179 |
} |
| 180 |
|
| 181 |
items.push({attributes: field_names, values: response[i], _store: this}); |
| 182 |
} |
| 183 |
|
| 184 |
if(request.sort && !request._aborted){ |
| 185 |
items.sort(dojo.data.util.sorter.createSortFunction(request.sort, self)); |
| 186 |
} |
| 187 |
}else{ |
| 188 |
|
| 189 |
items = [({attributes: ['count'], values: response, _store: this})]; |
| 190 |
} |
| 191 |
|
| 192 |
if(request.onItem){ |
| 193 |
for(var i = 0; i < items.length; ++i){ |
| 194 |
if(request._aborted){ |
| 195 |
break; |
| 196 |
} |
| 197 |
request.onItem.call(scope, items[i], request); |
| 198 |
} |
| 199 |
items = null; |
| 200 |
} |
| 201 |
|
| 202 |
if(request.onComplete && !request._aborted){ |
| 203 |
request.onComplete.call(scope, items, request); |
| 204 |
} |
| 205 |
} |
| 206 |
}, |
| 207 |
|
| 208 |
_partHandler: function( request, part, response){ |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
if(response instanceof Error){ |
| 219 |
if(part == this.Parts.DATA){ |
| 220 |
request._dataHandle = null; |
| 221 |
}else{ |
| 222 |
request._countHandle = null; |
| 223 |
} |
| 224 |
request._aborted = true; |
| 225 |
if(request.onError){ |
| 226 |
request.onError.call(request.scope, response, request); |
| 227 |
} |
| 228 |
}else{ |
| 229 |
if(request._aborted){ |
| 230 |
return; |
| 231 |
} |
| 232 |
if(part == this.Parts.DATA){ |
| 233 |
request._dataResponse = response; |
| 234 |
}else{ |
| 235 |
request._countResponse = response; |
| 236 |
} |
| 237 |
if((!request._dataHandle || request._dataResponse !== null) && |
| 238 |
(!request._countHandle || request._countResponse !== null)){ |
| 239 |
this._fetchHandler(request); |
| 240 |
} |
| 241 |
} |
| 242 |
}, |
| 243 |
|
| 244 |
fetch: function( request){ |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
request._countResponse = null; |
| 255 |
request._dataResponse = null; |
| 256 |
request._aborted = false; |
| 257 |
request.abort = function(){ |
| 258 |
if(!request._aborted){ |
| 259 |
request._aborted = true; |
| 260 |
if(request._dataHandle && request._dataHandle.cancel){ |
| 261 |
request._dataHandle.cancel(); |
| 262 |
} |
| 263 |
if(request._countHandle && request._countHandle.cancel){ |
| 264 |
request._countHandle.cancel(); |
| 265 |
} |
| 266 |
} |
| 267 |
}; |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
if(request.onItem || request.onComplete){ |
| 272 |
var content = this._parameters || {}; |
| 273 |
if(request.start){ |
| 274 |
if(request.start < 0){ |
| 275 |
throw new Error("dojox.data.SnapLogicStore: request start value must be 0 or greater"); |
| 276 |
} |
| 277 |
content['sn.start'] = request.start + 1; |
| 278 |
} |
| 279 |
if(request.count){ |
| 280 |
if(request.count < 0){ |
| 281 |
throw new Error("dojox.data.SnapLogicStore: request count value 0 or greater"); |
| 282 |
} |
| 283 |
content['sn.limit'] = request.count; |
| 284 |
} |
| 285 |
|
| 286 |
content['sn.content_type'] = 'application/javascript'; |
| 287 |
|
| 288 |
var store = this; |
| 289 |
var handler = function(response, ioArgs){ |
| 290 |
if(response instanceof Error){ |
| 291 |
store._fetchHandler(response, request); |
| 292 |
} |
| 293 |
}; |
| 294 |
|
| 295 |
var getArgs = { |
| 296 |
url: this.url, |
| 297 |
content: content, |
| 298 |
|
| 299 |
timeout: 60000, |
| 300 |
callbackParamName: "sn.stream_header", |
| 301 |
handle: dojo.hitch(this, "_partHandler", request, this.Parts.DATA) |
| 302 |
}; |
| 303 |
|
| 304 |
request._dataHandle = dojo.io.script.get(getArgs); |
| 305 |
} |
| 306 |
|
| 307 |
if(request.onBegin){ |
| 308 |
var content = {}; |
| 309 |
content['sn.count'] = 'records'; |
| 310 |
content['sn.content_type'] = 'application/javascript'; |
| 311 |
|
| 312 |
var getArgs = { |
| 313 |
url: this.url, |
| 314 |
content: content, |
| 315 |
timeout: 60000, |
| 316 |
callbackParamName: "sn.stream_header", |
| 317 |
handle: dojo.hitch(this, "_partHandler", request, this.Parts.COUNT) |
| 318 |
}; |
| 319 |
|
| 320 |
request._countHandle = dojo.io.script.get(getArgs); |
| 321 |
} |
| 322 |
|
| 323 |
return request; |
| 324 |
} |
| 325 |
}); |
| 326 |
|
| 327 |
}); |
| 328 |
|