meteorhacks/flow-components

Name: flow-components

Owner: meteorhacks

Description: Build your Meteor app with Components.

Created: 2014-12-05 04:55:46.0

Updated: 2018-02-16 08:12:42.0

Pushed: 2016-04-26 00:04:02.0

Homepage:

Size: 606

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Flow Components

This project is still work in progress and does not have unit and integration tests. We are directly pushing changes to master. There's no plan to change current APIs, but we don't have a guarantee.

Build your Meteor app with Components.

Flow Components has borrowed a lot of concepts from React's component model and applies them on top Blaze. It's not a 1-to-1 mapping of React.

We've also added some handy features which will help you control reactivity when building a large application.

Table of Contents
Why?

When we started building Kadira.io, we had no idea how to architect a Meteor app. After working on it for almost 1.5 years, we realized we were doing it wrong.

So, we thought a lot and played with a lot of UI frameworks and concepts. That includes React and Flux. After a lot of iterations and experiments, Flow Components is our component framework which is a part of the Flow Architecture for Meteor.

Getting Started

Let's create a very simple component. It's a typical Hello World example.

First add Flow Components into your app.

or add meteorhacks:flow-components

Then create directory on the client directory of your Meteor app and put these files.

Here's the JavaScript file, (component.js) which is the component.

component = FlowComponents.define('hello-component', function(props) {
nsole.log("A component created!");
is.name = props.name;

is.setRandomGreeting();
 change the greeting for every 300 millis
tInterval(this.setRandomGreeting.bind(this), 300);


onent.prototype.setRandomGreeting = function() {
r greetings = ["awesome", "nice", "cool", "kind"];
r randomGreeting = greetings[Math.floor(greetings.length * Math.random())];
is.set("greeting", randomGreeting)


onent.state.message = function() {
turn this.name + ", you are " + this.get("greeting") + "!";

Now we need to create our template(view.html). It's name should be identical to the name of the component which is hello-component.

plate name="hello-component">
iv class="hello-component">
Welcome Message: {{state$message}}
div>
mplate>

Then, let's add a CSS file for our component.

lo-component {
nt-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial;
nt-size: 16px;
rgin: 10px;

That's all. We've created our first component. We can render it anywhere we like. Here's how to do it.

render component="hello-component" name="Arunoda"}}
render component="hello-component" name="Sacha"}}

Then, this is how it's looks like:

Flow Components in Action

Check this repo for the complete source code.

States

State is a variable which is reactive. It can hold any JavaScript literal or an object. This is very similar to a template helper, but it's integrated into the component.

There are couple of ways, you can get a state. This is the first way.

Creating states using component.state.
component = FlowComponents.define('my-component');
onent.state.message = function() {
turn "some value";

Above function is running in a reactive context. So, you can use any kind of reactive variables, Session variables and MiniMongo APIs inside it.

Context of the above function is the component itself. That's the main difference from a template helper.

Creating states using this.set

You can also set an state with this.set API. This is an API of the component. So, you can use it anywhere withing the component. This is how to implement the above example via this.set.

component = FlowComponents.define('my-component', function() {
is.set("message", "some value");

this.set with large object

this.set is a useful function, but it does value cloning and equality checks. So, if you need to set a very large object this will be an issue. But there is a solution to avoid that. Here's how to do it:

component = FlowComponents.define('my-component', function(params) {
r fireAway = true;
is.set("veryLargeObject", params.obj, fireAway);

Now, flow component does not do any cloning or equality checks. It simply invalidate all the computations looks for this key.

Accessing states inside a template

You can access the state anywhere in the template as below:

plate name='my-component'>
ssage is: {{ state$message }}
mplate>

To access the state, just prefix it with state$. You can even access the state inside nested templates in a given component.

Accessing states inside a component with this.get

You can also access the state inside the component with this this.get API like this:

onent.state.messageWithName = function() {
r message = this.get("message");
turn "Arunoda, " + message;

this.get is reactive and available anywhere inside the component.

Actions

Components don't handle DOM events directly. But a component has actions. You can think actions are a kind of way to trigger tasks. To handle DOM elements, you need to call an action within a template event.

This is how to create an action:

component = FlowComponents.define('my-component');
onent.action.changeMessage = function(someValue) {
is.set("message", someValue);

Context of the message is the component itself. You can also access reactive content inside that, but the action won't re-run again for a change in reactive content.

There are few ways to call an action. Let's look at them.

Via an DOM event handler

You can call an action inside an event handler. But, that event handler needs to be registered for a template of the given component. This is how to do that.

late['my-component'].events({
lick button": function() {
var message = $('.textbox').val();
FlowComponents.callAction("changeMessage", message);


Via Props (aka: Action Passing)

You can also pass an action to a child component via props. Then the child component can call that action just like invoking a JavaScript function. This is how to do it.

Let's say we are rendering a component called input inside our main component:

render component="input" onSubmit=action$changeMessage }}

Then inside the input component, it calls the onSubmit property like this:

component = FlowComponents.define("input", function(props) {
is.onSubmit = props.onSubmit;


onent.action.submitMessage = function(message) {
is.onSubmit(message);

We also describe this method as “action passing”. This is the basic building block for creating nested components.

Actions and Promises

We've have es6-promise support for Actions. When you call an actions either via FlowComponents.callAction or action passing, you'll get a promise always. Based on that promise you can model your component.

Action definition, does not need to return a promise all the time. If you return a promise, Flow Component will pass it to the caller. If not, it'll create a empty promise.

See:

component = FlowComponents.define("input", function(props) {
is.onSubmit = props.onSubmit;


onent.action.submitMessage = function(message) {
r self = this;
lf.set('loading', true);

r promise = this.onSubmit(message);
omise.catch(function(err) {
self.set('errorMessage', err.message);
.then(function() {
self.set('loading', false);
;

Checkout this sample repo for a complete example.

Disable Promises Temporarily

Sometime we need to have actions simply sending the plain return rather than wrapping the return as a promise. For those scenarios, we can temporarily disable promise support like this.

component = FlowComponents.define("input", function(props) {
is.onNewTooltip = props.onNewTooltip;
is.dataFn = props.dataFn;
is.autorun(this.formatTooltips);


onent.prototype.formatTooltips = function() {
r data = this.dataFn();
r formattedData = this.noPromises(function() {
var self = this;
return data.map(function(item) {
  return self.onNewTooltip(item);
});
;

turn formattedData;

Props

Props is a way to pass values when rendering the component. A prop can be any valid JavaScript literal, object or a function. This is how to do it:

render component="input" text="This is great" }}

Then you can access it inside the component like this:

component = FlowComponents.define("input", function(props) {
nsole.log("Text is: ", props.text);

You can set any number of props you like:

render component="input"
  text="This is great"
  backgroundColor="#736634"
  fontSize=20

You can pass a state into a prop like this:

render component="input" text=state$message }}

We've previously talked about how to pass an action like this:

render component="input" onSubmit=action$message }}
Prototypes

A Prototype is very similar to a prototype is JavaScript. Prototype is a common function (or property) you can access anywhere inside a component. We've used prototypes in the component we wrote in the Getting Started section.

component = FlowComponents.define('hello-component', function(props) {
 see how we are using the prototype
is.setRandomGreeting();
tInterval(this.setRandomGreeting.bind(this), 300);


his is a prototype
onent.prototype.setRandomGreeting = function() {
r greetings = ["awesome", "nice", "cool", "kind"];
r randomGreeting = messages[Math.floor(messages.length * Math.random())];
is.set("greeting", randomGreeting)

In that, we've created a setRandomGreeting prototype and call in when a component is creating.

We've also calling it for every 300 milliseconds via the setInterval.

Extending Components with Mixins

Sometimes we create similar kind of components. Then, we've to copy and paste a lot of code including prototypes, states and actions. It's a bad way to manage it.

That's why Mixins are going to help us. With mixins we can group a set of common code and extend it with an existing components. Let's say we need to add component level subscriptions to Flow, this is how to do it :)

onentSubs = {
ototype: {},
tion: {},
ate: {}


alls when the component is creating
onentSubs.created = function() {
is._subs = [];


alls when the component is rendered
onentSubs.rendered = function() {};

alls when the component is destroyed
onentSubs.destroyed = function() {
is.stopSubscriptions();


onentSubs.prototype.subscribe = function() {
r sub = Meteor.subscribe.apply(null, arguments);
is._subs.push(sub);
turn sub;


onentSubs.prototype.ready = function() {
r ready = true;
is._subs.forEach(function(sub) {
ready = ready && sub.ready();
;

turn ready;


onentSubs.prototype.stopSubscriptions = function() {
is._subs.forEach(function(sub) {
sub.stop();
;
is._subs = [];


onentSubs.state.isReady = function() {
turn this.ready();


onentSubs.action.stopSubscriptions = function() {
is.stopSubscriptions();

Now you can extend your component with the mixin we've created. Check this:

component = FlowComponents.define("my-component", function() {
 you can use like to this subscribe
is.subscribe("mysubscription");


xtend your component with Mixins
onent.extend(ComponentSubs);

ou can use it in an action like this
onent.action.loadMore = function() {
is.stopSubscriptions();
is.subscribe("mysubscription", {limit: 200});

You can use isReady state inside the template like this:

plate name="my-component">
#if state$isReady}}
<!-- do something with your data -->
else}}
Loading...
/if}}
mplate>
Extending Components with Nested Components

We can use Mixins to extend the functionalities for the component. But we can't use that to extend the user interface. That's where we can use nested components.

There is no special APIs for that. But you can create a component which uses few other components inside that.

You can also accept an component name from the props and render that. For an example, let's say we are building a loading component. So, we can allow to customize the loading spinner. Here is is:

component = FlowComponents.define("loading", function(props) {
is.set("loadingComponent", props.loadingComponent);

This is the UI for that:

f state$loadingComponent}}
> render component=state$loadingComponent }}
se}}
ading...
f}}
Life Cycle Events

A Component has few different events. Here they are:

You may need to use these events to customize your components. We need to use the created event almost every time. So, that's the callback you passed as the second argument when creating the components.

component = FlowComponents.define("hello", function(props) {
nsole.log("Component created with props:", props);

In the created event callback you can get the props as the first argument.

For other two events, they can be access anywhere inside the component like with:

Check this example:

component = FlowComponents.define("hello", function() {
is.onRendered(function() {
console.log("Rendered to the screen.");
;

is.onDestroyed(function() {
console.log("Component destroyed.");
;

Context of the callback you've passed to a life cycle event is the component itself. So, because of that something like this is possible.

component = FlowComponents.define("hello", function() {
is.onRendered(function() {
console.log("Rendered to the screen.");

this.onDestroyed(function() {
  console.log("Component destroyed.");
});
;

Autoruns

Sometimes we may need to use autoruns inside a component. So, when you start an autorun it needs to stop when the component destroyed. We've a simple way to do that. See:

component = FlowComponents.define("hello", function() {
is.autorun(function() {
var posts = Posts.fetch();
this.set("postCount", posts.length);
;

Note: Context of the callback for this.autorun is also the component. That's why calling this.set is possible inside the autorun.

State Functions

State functions is a powerful tool which helps you build components while minimizing re-renders. Before we start, let's see why need it. Look at this usage of nested components:

component = FlowComponents.define("parent", function() {
r self = this;
tInterval(function() {
var usage = Math.ceil(Math.random() * 100);
this.set("cpuUsage", usage);
 100);

This is the template of parent:

plate name="parent">
> render component="gauge" value=state$cpuUsage }}
mplate>

As you can see this is pretty straight forward. Parent component change the CPU usage for every 100 millis and then guage component will print it. So, what's the issue here?

Since we get the state as state$cpuUsage, it's getting change every 100 millis. So, the gauge component will get changed at that time too.

That means existing gauge component will be destroyed and re created again. Which is very expensive and we don't need to do something like this. That's where state functions are going to help you. Before that, let's look at how we've implemented our gauge component.

component = FlowComponents.define("guage", function(props) {
is.set('value', props.value);

This is the template:

plate name="guage">
lue is: {{ state$value }}
mplate>
Converting it to use State Functions

Let's change the parent template like this:

plate name="parent">
> render component="gauge" value=stateFn$cpuUsage }}
mplate>

Note that, we only change state$cpuUsage into stateFn$cpuUsage. With that, we wrap the cpuUsage state into a function. So, when it's get changed, it won't re-render the component.

This is how to access the state it inside the gauge component.

component = FlowComponents.define("guage", function(props) {
is.autorun(function() {
// see now it's a function
var value = props.value();
this.set("value", value);
;

As you can see, now value prop is a function. Now it's only reactive within the autorun we've defined. So, now we can actually, control the reactivity as we need.

Writing this.autorun for every prop seems like a boring task. That's why we introduced this.setFn. See how to use it. It does the exact same thing we did in the previous example.

component = FlowComponents.define("guage", function(props) {
is.setFn("value", props.value);

Content Blocks

We can use content blocks to write nested components. Here's an example for a loading component which uses content blocks.

ender component="loading" loaded=stateFn$loaded }}
>render component="data-viewer"}}
se}}
ading...
ender}}

This is how we can implement the loading component

component = FlowComponents.define('loading', function(props) {
is.setFn("loaded", props.loaded);

Here's the template:

plate name="loading">
#if state$loaded}}
{{> flowContentBlock }}
else}}
{{> flowElseBlock }}
/if}}
mplate>
Referencing Child Components

This API is experimental.

So, now we know how to use child components and we've seen some examples. Most of the time you can interact with them by passing actions and passing state functions.

But sometimes, you may need to refer them individually access their states. Let's look at our myForm component.

component = FlowComponents.define("myForm", function() {



onent.action.updateServer = function(name, address) {
teor.call("update-profile", name, address);

This is the template for myForm.

plate name="myFrom">
> render component="input" id="name" }}
> render component="input" id="address" }}
utton>Submit</button>
mplate>

Here's the event handler for submit.

late['myForm'].events({
lick button": function() {
var name = FlowComponents.child("name").getState("value");
var address = FlowComponents.child("address").getState("value");

FlowComponents.callAction('updateServer', name, address);


See, we could access individual child component by their id and get the state called value. But this API has following characteristics:

Refer All Child Components

This API is experimental.

This API is just like FlowComponents.child(), but it gives all the child components inside the current component.

allChildComponents = FlowComponents.children();
hildComponents.forEach(function(child) {
nsole.log(child.getState("value"));

Organizing Large Components

Sometimes our components could have a large number of states, actions and prototypes. So, it's super hard to put them all in a single file. Luckily, there's a way to put those functions in different files. This is how to do it.

First create the component and name it like component.js

component = FlowComponents.define("myForm", function() {


Then create a file for states with the name component_states.js.

component = FlowComponents.find("myForm");
onent.state.title = function() {


It's very important to define the component before accessing it using .find(). That's why we've a naming convention like above.

Likewise you can group actions, states and prototypes in anyway you like organize your component.

Accessing Component DOM

We've designed components in a way that to reduce the direct interfacing with the DOM. But in practice, it's hard to do. So, if you want to access the DOM directly inside the component, here are the APIs for that. All these API are scoped to the template of the component.

How Flow Components different from XXX

Let's compare flow with some other components and UI related frameworks

Blaze

Flow Component does not replace or Blaze. Instead it's build on top of the Blaze. We are using Blaze templates to render the user interfaces. When using Flow Components, you may don't need to use template helpers and template instances anymore. But still, we use a lot of cool features of Blaze.

React

React is a completely different UI framework. There is no built in support for React with flow components. But, there are a few ways to integrate React with Meteor. Flow does not interfere with them.

If there is a way to render react components inside a Blaze template, then it's possible to use React with Flow. That's because we are using Blaze templates to render the UI.

Polymer / WebComponents

Answer for this is just the same as for React.

Ionic / Meteoric

Answer for this is just the same as for React.

Angular

It might be possible to use Angular with Flow Components. But we haven't try that yet.

Blaze 2

There is an ongoing proposal for Blaze 2. It has an it's own component system. It's still in the design phase. Try to look at it.

We designed Flow Components for our internal use at MeteorHacks. We've build few projects with Flow. So, it's unlikely we'll switch to a new component system unless it has all of our features.

Usage Guidelines

We use Flow Components alot at MeteorHacks specially on Kadira and BulletProof Meteor. We follow some simple rules to manage components and do inter component communication.

Here are the guidelines: https://meteorhacks.hackpad.com/Flow-Components-Guideline-gAn4odmDRw3


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.