new (require("bajaux/mixin/batchLoadMixin"))(target)
- Description:
Applies the
batchLoadmixin to the target Widget.The
batchLoadmixin does not alter the behavior of the target Widget,
but instead defines a behavioral contract. It defines the way it will
handle abaja.comm.Batchpassed to theload()method (thus allowing
multipleWidgets to load and subscribe BajaScript values in a single
network call).It states:
- If my
load()method does receive abatchparameter, and does add
a transaction to it (say, by passing it to
baja.comm.Subscriber#subscribe), then I must notify the caller after I
am through adding transactions to thatBatchand it is safe to commit.
I do this by checking for aprogressCallbackparameter, and passing
COMMIT_READYto it. - If my
load()method does not make use of the batch, it must still
emitCOMMIT_READY, but can do so at any time. (Due to this constraint,
it does not make sense to addbatchLoadMixinto a widget that does not
actually use a batch.)
Widgets that append transactions to a
batchparameter in theload()
function, without marking themselves with this mixin, should be expected
have those loads fail. Likewise, passing a batch to a Widget'sload()
function without checking whether it has thebatchLoadmixin can also
fail.Why is this contract necessary? When passing a batch to
load(), you
aren't guaranteed thatload()will not perform some other asynchronous
work before appending transactions to the batch. If you don't wait for
the transactions to complete, you run the risk of committing the batch
prematurely. Then when the widget gets around to appending transactions
to the already-committed batch, it will fail.To make this easier,
batchLoadMixin.loadWidgetshandles a lot of this
workflow for you.- If my
- Source:
Example
Example implementation of the batchLoad contract.
MyWidget.prototype.doLoad = function (component, params) {
var batch = params && params.batch,
progressCallback = params && params.progressCallback,
promise = this.getSubscriber().subscribe({
comps: component,
batch: batch
});
//I'm done with the batch - let the caller know they can commit it
if (progressCallback) {
progressCallback(batchLoadMixin.COMMIT_READY);
}
return promise;
};
Parameters:
| Name | Type | Description |
|---|---|---|
target |
module:bajaux/Widget |
Members
(static, constant) COMMIT_READY :string
- Description:
Value to be passed to a
progressCallbackparameter to indicate that
a batch given to theload()function can be safely committed.
- Source:
Value to be passed to a progressCallback parameter to indicate that
a batch given to the load() function can be safely committed.
Type:
- string
Methods
(static) loadWidgets(widgets, values, paramsopt) → {Promise}
- Description:
Loads values into the given widgets, passing one
Batchinto theload()
method for each one.Widgets that make use of the
Batchare expected to havebatchLoadMixin.
See documentation for the mixin itself for contractual details.
- Source:
Parameters:
| Name | Type | Attributes | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
widgets |
Array.<module:bajaux/Widget> | the widgets to load |
|||||||||||||
values |
Array.<*> | values to load into the widgets |
|||||||||||||
params |
Object |
<optional> |
Properties
|
Returns:
promise to be resolved when all widgets have completed
loading
- Type
- Promise