Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

composable views #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

composable views #43

wants to merge 3 commits into from

Conversation

yoshuawuyts
Copy link
Member

Docs on how to make composable views (aka higher-order views or templates). Hope this is useful!

@yoshuawuyts yoshuawuyts requested a review from a user January 14, 2018 02:55
_note: There's not an exact word for what we're doing here. Because it's a
pattern, and not an API the exact word also doesn't matter too much. This is
also not at all the only way to compose functions - so don't worry too much
about getting the terminology right - we're also just making it up as we go._

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 classy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i really like this too!


var app = choo()
app.route('/', template(main)) // 1.
app.route('/bar', template(bar)) // 2.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be foo?

}
```

1. We create a route `'/'` which calls the `template` function, and passes it
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

points 1, 2, 5 and 6 are redundant imo

that share a lot of the same layout. If you find that you're repeating the same
layout in a lot of code, it can be beneficial to make it reusable instead.

The most common way to create reusable templates, is to create a function that
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant comma

takes a view as an argument and returns a view. Inside the function the
childView is called, and wrapped with some HTML. The end result is a nicely
composed function that's also known as a "composable view", "higher order view",
"template".
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or a "template" instead of the last comma

childView is called, and wrapped with some HTML. The end result is a nicely
composed function that's also known as a "composable view", "higher order view",
"template".

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rewrite this entire paragraph:

The easiest way to create a reusable view in Choo is to create a function that takes a view (meaning a snippet of HTML) and then inserts it into another, function-internal view. This makes it easy to create functions that construct HTML that you always want to keep static, such as a menu bar, but at the same time construct dynamic HTML defined by whatever you pass it as an argument. Functions like these are called "composable functions" and, funnily enough, are a fundamental piece of functional programming!

or something like that

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function-internal is an absolutely stupid phrase as i just noticed

argument.

_note: There's not an exact word for what we're doing here. Because it's a
pattern, and not an API the exact word also doesn't matter too much. This is
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/word/terminology

@yoshuawuyts
Copy link
Member Author

Updated with all feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants