Giter Site home page Giter Site logo

bindable.scala's Introduction

bindable.scala

Latest version Scaladoc Scala CI

bindable.scala is a library of type classes for creating user-friendly Binding.scala components.

Motivation

When creating a component that accepts parameters or “holes”, it is difficult to determine the types of those parameters.

For example, the following component accepts two Binding as parameters:

@dom def myComponent1(title: Binding[String], children: Binding[BindingSeq[Node]]) = {
  <div title={title.bind}>
    {children.bind}
  </div>
}

By typing parameters as Bindings, myComponent1 allows partial rendering whenever the value of title or children is changed. Unfortunately, it is too verbose to use myComponent1 for simple use cases when the parameters are constants.

// Does not compile
@dom def myUseCases1 = myComponent1("My Title", <img/>).bind

// Compiles, but too verbose
@dom def myUseCases2 = myComponent1(Constant("My Title"), Constant(Constants(<img/>))).bind

In this library, we introduced two type classes, Bindable and BindableSeq, to allow heterogeneous types of parameters for a component.

Usage

Step 1: adding the following settings into your build.sbt

addCompilerPlugin("org.spire-math" %% "kind-projector" % "latest.release")

libraryDependencies += "com.thoughtworks.binding" %%% "bindable" % "latest.release"

Step 2: creating a component that accepts bindable parameters

import com.thoughtworks.binding.bindable._
import org.scalajs.dom.raw._
@dom def myComponent2[Title: Bindable.Lt[?, String], Children: BindableSeq.Lt[?, Node]](title: Title, children: Children) = {
  <div title={title.bind}>
    {children.bindSeq}
  </div>
}

Step 3: using the component with any parameters that can be converted to Binding or BindingSeq

import com.thoughtworks.binding._, Binding._
@dom def myUseCases3 = myComponent2("My Title", <img/>).bind
@dom def myUseCases4 = myComponent2(Constant("My Title"), Constant(Constants(<img/>))).bind
@dom def myUseCases5 = myComponent2("My Title", <img/><img/>).bind
@dom def myUseCases6 = myComponent2("My Title", Binding(<img/><img/>)).bind

Unlike use cases of myComponent1, all the above use cases of myComponent2 compile now, with the help of the Bindable and BindableSeq type classes.

Links

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.