Giter Club home page Giter Club logo

marko-context's Introduction

Marko <context>

The <context> tag provides a way for a parent component to implictly pass data to child components from the same package that are nested arbitrarily deep in the component tree.

The basics

To store values into a subtree context, pass attributes to the <context> tag:

<context name="Frank">
    ...
</context>

To get values from a parent context, pass tag params to the <context> tag:

<context({ name })>
    Hello ${name}
</context>

And all together:

<context name="Frank">
    <context({ name })>
        Hello ${name}
    </context>
</context>

Obviously, both setting and getting context in the same component is pretty pointless. The real power comes when using one component to set the context and retrieving it in another component.

Imaging creating a <special-form> that can provide data to a <special-input> without requiring the use of those components to connect things together. That's the power of <context>.

<special-form data=input.formData>
    <fieldset>
        <label>Name</label>
        <special-input field="name"/>
    </fieldset>
</special-form>

Features

Scoped to the package

Packages have their own context. If package-a and package-b both set a name in their context, it won't conflict and package-a will not be able to access the name from package-b.

This means you can name the values in your context without worry of collisions with other packages outside your control. It also forces you to encapsulate the context of your components because outside packages (or an application using your package) will not be able to access your context directly.

Scoped to the subtree

<context name="Frank">
    <context name="Jane">
        <context({ name })>
            <div>${name}</div>
        </context>
    </context>
    <context({ name })>
        <div>${name}</div>
    </context>
</context>

will render:

<div>Jane</div><div>Frank</div>

Works across async boundaries

<context name="Frank">
    <await(daysRemaining from birthdayPromise)>
        <context({ name })>
            <div>Only ${daysRemaining} until your birthday, ${name}!</div>
        </context>
    </await>
</context>

Guaranteed updates

Even if a intermediate component doesn't update, the context will still propagate to any components that are using it.

marko-context's People

Contributors

mlrawlings avatar

Watchers

James Cloos avatar Dylan Piercey avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.