Skip to content

Commit

Permalink
Delay querying store until after subclass computeProperties() methods…
Browse files Browse the repository at this point in the history
… have run.

Fixes ibm-js#442.
  • Loading branch information
wkeese committed Mar 21, 2016
1 parent 40a420c commit 67ae90d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
19 changes: 9 additions & 10 deletions Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,18 @@ define([
return renderItems;
},

/**
* If the store parameters are invalidated, queries the store, creates the render items and calls initItems()
* when ready. If an error occurs a 'query-error' event will be fired.
* @param props
* @param isAfterCreation
* @protected
*/
computeProperties: function (props, isAfterCreation) {
// If this call is upon widget creation but `this.store` is not available, don't bother querying store
computeProperties: dcl.after(function (args) {
// Runs after the subclass computeProperties() methods run and possibly set this.query and this.source.
// If this call is upon widget creation but `this.source` is not available, don't bother querying store.
// If the store parameters are invalidated, queries the store, creates the render items
// and calls initItems() when ready. If an error occurs a 'query-error' event will be fired.
// If this call is upon widget creation but `this.store` is not available, don't bother querying store.

var props = args[0], isAfterCreation = args[1];
if (("source" in props || "query" in props) && (this.source || !isAfterCreation)) {
this.queryStoreAndInitItems(this.processQueryResult);
}
},
}),

/**
* Queries the store, creates the render items and calls initItems() when ready. If an error occurs
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,49 @@ define([
return d;
},

"set query in subclass": function () {
// This test confirms that the subclass computeProperties() method can set this.query,
// and Store will wait until after that to run the query against the store.

var MyStoreSubclass = register("my-store-subclass", [HTMLElement, Widget, Store], {
idToQuery: "place holder",

createdCallback: function () {
this.storeQueries = [];
},

computeProperties: function (oldVals) {
if ("idToQuery" in oldVals) {
this.query = {
id: this.idToQuery
};
}
},

queryStoreAndInitItems: dcl.superCall(function (sup) {
return function () {
this.storeQueries.push(this.query);
sup.apply(this, arguments);
};
})
});

var myStore = new MyStoreSubclass({
source: new M({
data: [
{ id: "foo", name: "Foo" },
{ id: "bar", name: "Bar" }
],
model: null
}),
idToQuery: "foo"
});

// The new MyStoreSubclass() automatically calls deliver(), so the query has already executed.
// Now make sure that it just executed once.
assert.deepEqual(myStore.storeQueries, [{ id: "foo" }]);
},

StoreFuncRange: function () {
var d = this.async(15000);
var store = new C();
Expand Down

0 comments on commit 67ae90d

Please sign in to comment.