Skip to content

Commit 1673e88

Browse files
Support all CustomStore events via options (#362)
1 parent a63c6d9 commit 1673e88

2 files changed

Lines changed: 78 additions & 9 deletions

File tree

js-test/test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,4 +802,59 @@
802802
store.remove(123)
803803
]).then(done);
804804
});
805+
806+
QUnit.test("store events", function(assert) {
807+
var done = assert.async();
808+
809+
var eventNames = [ "onLoading", "onLoaded", "onInserting", "onInserted", "onUpdating", "onUpdated", "onRemoving", "onRemoved", "onModifying", "onModified" ];
810+
var trace = { };
811+
812+
var options = {
813+
key: "any",
814+
loadUrl: "/",
815+
insertUrl: "/",
816+
updateUrl: "/",
817+
deleteUrl: "/"
818+
};
819+
820+
eventNames.forEach(function(name) {
821+
options[name] = function() {
822+
trace[name] = true;
823+
};
824+
});
825+
826+
var store = createStore(options);
827+
828+
willRespondWithJson({ });
829+
830+
Promise.all([
831+
store.load(),
832+
store.insert({ }),
833+
store.update(123, { }),
834+
store.remove(123)
835+
]).then(function() {
836+
assert.equal(Object.keys(trace).length, eventNames.length);
837+
done();
838+
})
839+
});
840+
841+
QUnit.test("onPush", function(assert) {
842+
var done = assert.async();
843+
844+
assert.expect(0);
845+
846+
var store = createStore({
847+
onPush: function() {
848+
done();
849+
}
850+
});
851+
852+
if("push" in store) {
853+
store.push([
854+
{ type: "insert", data: { } }]
855+
);
856+
} else {
857+
done();
858+
}
859+
});
805860
});

js/dx.aspnet.data.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@
3131
})(function($, CustomStore, dataUtils) {
3232
"use strict";
3333

34-
function createStore(options) {
35-
var store = new CustomStore(createStoreConfig(options));
36-
store._useDefaultSearch = true;
37-
return store;
38-
}
34+
var CUSTOM_STORE_OPTIONS = [
35+
"onLoading", "onLoaded",
36+
"onInserting", "onInserted",
37+
"onUpdating", "onUpdated",
38+
"onRemoving", "onRemoved",
39+
"onModifying", "onModified",
40+
"onPush",
41+
"loadMode", "cacheRawData",
42+
"errorHandler"
43+
];
3944

4045
function createStoreConfig(options) {
4146
var keyExpr = options.key,
@@ -156,10 +161,9 @@
156161
d.resolve(isJSON ? JSON.parse(res) : res);
157162
}
158163

159-
return {
164+
var result = {
160165
key: keyExpr,
161-
errorHandler: options.errorHandler,
162-
loadMode: options.loadMode,
166+
useDefaultSearch: true,
163167

164168
load: function(loadOptions) {
165169
return send(
@@ -250,6 +254,14 @@
250254
}
251255

252256
};
257+
258+
CUSTOM_STORE_OPTIONS.forEach(function(name) {
259+
var value = options[name];
260+
if(value !== undefined)
261+
result[name] = value;
262+
});
263+
264+
return result;
253265
}
254266

255267
function processLoadResponse(d, res, getResolveArgs) {
@@ -367,6 +379,8 @@
367379
}
368380

369381
return {
370-
createStore: createStore
382+
createStore: function(options) {
383+
return new CustomStore(createStoreConfig(options));
384+
}
371385
};
372386
});

0 commit comments

Comments
 (0)