Operations Channel
Channels are ways for Flow.js to talk to external APIs -- primarily the underlying Epicenter APIs.
The primary use cases for the Operations Channel are:
publish: Call an operation.subscribe: Receive notifications when an operation is called.
For example, use publish() to call an operation (method) from your model:
Flow.channel.operations.publish('myMethod', myMethodParam);For reference, an equivalent call using Flow.js custom HTML attributes is:
<button data-f-on-click="myMethod(myMethodParam)">Click me</button>You can also use subscribe() and a callback function to listen and react when the operation has been called:
Flow.channel.operations.subscribe('myMethod',
function() { console.log('called!'); } );Use subscribe(*) to listen for notifications on all operations.
To use the Operations Channel, simply initialize Flow.js in your project.
Configuration Options
silent
- String
Determine when to update state. Defaults to false: always trigger updates.
Possible options are:
true: Never trigger any updates. Use this if you know your model state won't change based on operations.false: Always trigger updates.[array of operation names]: Operations in this array will not trigger updates; everything else will.{ except: [array of operation names] }: Operations in this array will trigger updates; nothing else will.
To set, pass this into the Flow.initialize() call in the channel.run.operations field:
Flow.initialize({
channel: {
run: {
model: 'myModel.py',
account: 'acme-simulations',
project: 'supply-chain-game',
operations: { silent: true }
}
}
});To override for a specific call to the Operations Channel, pass this as the final options parameter:
Flow.channel.operations.publish('myMethod', myMethodParam, { silent: true });readOnly
- Boolean
Allow using the channel for reading data (subscribing), but disallow calls to publish. Defaults to false: allow both subscribing and publishing. If a function is provided, the function should return a Boolean value to override.
Methods
refresh
Force a check for updates on the channel, and notify all listeners.
Parameters
executedOpns: String|Array Operations which just happened.response: Any Response from the operation.force: Boolean Ignore allsilentoptions and force refresh.options: Object (Optional) Overrides for the default channel options.
Return Value
- **
notify
Alert each subscriber about the operation and its parameters. This can be used to provide an update without a round trip to the server. However, it is rarely used: you almost always want to subscribe() instead so that the operation is actually called in the model.
Example
Flow.channel.operations.notify('myMethod', myMethodResponse);Parameters
operation: String Name of operation.value: String|Number|Array|Object Parameter values for the callback function.
Return Value
- **
publish
Call the operation with parameters, and alert subscribers.
Example
Flow.channel.operations.publish('myMethod', myMethodParam);
Flow.channel.operations.publish({
operations: [{ name: 'myMethod', params: [myMethodParam] }]
});Parameters
operation: String|Object For one operation, pass the name of operation (string). For multiple operations, pass an object with fieldoperationsand value array of objects, each withnameandparams:{operations: [{ name: opn, params:[] }] }.params: String|Number|Array|Object (Optional) Parameters to send to operation. Use for one operation; for multiple operations, parameters are already included in the object format.options: Object (Optional) Overrides for the default channel options.options.silent: Boolean Determine when to update state.
Return Value
- $promise: Promise to complete the call.
subscribe
Subscribe to changes on a channel: Ask for notification when operations are called.
Example
Flow.channel.operations.subscribe('myMethod',
function() { console.log('called!'); });Parameters
operations: String|Array The names of the operations. Use*to listen for notifications on all operations.subscriber: Object|Function The object or function being notified. Often this is a callback function.
Return Value
- String: An identifying token for this subscription. Required as a parameter when unsubscribing.
unsubscribe
Stop receiving notification when an operation is called.
Parameters
operation: String|Array The names of the operations.token: String The identifying token for this subscription. (Created and returned by thesubscribe()call.)
Return Value
- **
unsubscribeAll
Stop receiving notifications for all operations. No parameters.
Return Value
- **