Giter Site home page Giter Site logo

databinder's Introduction

DataBinder

Not Supported => Use Angular and Polymer Instead

AngularDart and Polymer.Dart have very robust implementation of data bindings.

Overview

Data binding is a technique used to maintain synchronization of data. And it's a key component of most MVP/MVVM frameworks.

DataBinder is a Dart library implementing data binding via object mirrors. Though the primary focus of the library is to be used by frameworks, it can be used directly as well.

Core principles

Do all bindings dynamically => no compilation

I strongly believe that runtime binding is more advantageous than compilation.

  • No extra steps in the build process
  • Easier to understand
  • Templates can be built dynamically or retrieved from a database

Provide extension points for frameworks

To be a good foundation for building MV* frameworks Databinder should support custom binders.

How to use it

Add the databinder dependency to your pubspec.yaml

name:  sampleapp
dependencies:
  databinder: any

Define a view model

class Person {
  String firstName, lastName;
  bool married;

  Person(this.firstName, this.lastName);

  toUpperCase(e){
    firstName = firstName.toUpperCase();
    lastName = lastName.toUpperCase();
  }

  toLowerCase(e){
    firstName = firstName.toLowerCase();
    lastName = lastName.toLowerCase();
  }

  get children {
	return [new Person("Sam", "Smith"), new Person("Liz", "Smith")]
  }
}

Just 3 methods

  • bind - registers model and DOM watchers
  • unbind - unregisters all the registred watchers
  • digest - triggers all the registered model watchers

One-way data binding

<fieldset id="one-way">
  First name {{firstName}}
  Last name {{lastName}}
</fieldset>

To bind:

var person = new Person("Jim", "Smith");
var element = query("#one-way");
var binder = new DataBinder.root(element, person);
binder.bind();

To unbind:

binder.unbind();

Two-way data binding

<fieldset id="two-way-form">
  First name <input data-bind="value:firstName"/>
  Last name <input data-bind="value:lastName"/>
</fieldset>

To bind:

var person = new Person("Jim", "Smith");
var element = query("#two-way-form");
var binder = new DataBinder.root(element, person);
binder.bind();

To unbind:

binder.unbind();

Binding actions

<div id="actions">
  <button data-action="click:toUpperCase">TO UPPER CASE</button>
  <button data-action="click:toLowerCase">to lower case</button>
</div>

To bind:

var person = new Person("Jim", "Smith");
var element = query("#actions");
var binder = new DataBinder.root(element, person);
binder.bind();

To unbind:

binder.unbind();

Conditionals

<div data-template="true" data-if="married">
  {{firstName}} {{lastName}} is married
</div>

To bind:

var person = new Person("Jim", "Smith");
person.married = true;

var element = query("#actions");
var binder = new DataBinder.root(element, person);
binder.bind();

You'll see:

Jim Smith is married

Repeaters

<div data-template="true" data-iterate="child in children">
  {{child.firstName}} is a child of {{firstName}}
</div>

You'll see:

Sam is a child of Jim
Liz is a child of Jim

More control over binding

var binder = new DataBinder.root(element, person);

is the equivalent of:

var boundObjects = new BoundObjects();
boundObjects.register("", person);

var scope = new Scope(boundObjects);

var transformations = new Transformations.standard();

var binder = new DataBinder(element, scope, transformations);

When creating a data binder you can configure the objects you want to bind, the scope, and the list of available transformations. All these objects are extendable.

Example app

Check out the example application that comes with the package to see the library in action.

databinder's People

Contributors

vsavkin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

shailen

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.