Flow: How to read a value from an instance of Flow component?
Total.js Flow is really great tool and this tutorial shows you how you can get an instance of component from Total.js Flow directly in a controller.
Follow these steps:
- create your own flow with
analytics
component
- double-click on
analytics
component and type a reference to mystats
and press save (as the picture below)
Then create a controller controllers/default.js:
exports.install = function() {
F.route('/', readfromflow);
};
function readfromflow() {
// FLOW is a global variable defined in FLOW package
var instance = FLOW.findByReference('mystats')[0];
if (!instance)
return this.throw404();
// instance === Flow "Analytics" Instance
// Source-Code: https://github.com/totaljs/flowcomponents/blob/master/analytics/analytics.js#L142
var current = instance.custom.current();
// Renders serialized data in JSON format
self.json(current, true);
}
Where can I read all public methods from Flow components?
Another helpful FLOW methods
FLOW.findByReference('STRING');
FLOW.findByReference(REGULAR_EXPRESSION);
// Finds all instances by reference
// returns {Array of Components}
FLOW.findByName('STRING');
FLOW.findByName(REGULAR_EXPRESSION);
// Finds all instances by name
// returns {Array of Components}
FLOW.findByComponent('STRING');
FLOW.findByComponent(REGULAR_EXPRESSION);
// Finds all instances by component name
// returns {Array of Components}
FLOW.findById('ID');
// Finds an instance by its ID
// returns {Component}
FLOW.hasComponent(name);
// Is component installed?
// returns {Boolean}
FLOW.hasInstance(id);
// Does an instance of component with the given id exists?
// returns {Boolean}