GithubHelp home page GithubHelp logo

mgesmundo / dart-observable Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hientrung/dart-observable

0.0 1.0 0.0 106 KB

An observable, computed objects written in Dart with buit-in validation

Home Page: https://pub.dev/documentation/obsobject/latest/obsobject/obsobject-library.html

License: MIT License

Dart 100.00%

dart-observable's Introduction

Obsobject

An observable, computed objects written in Dart with built-in validation

Install

dependencies:
  obsobject: ^1.2.0

Observable

An object will notify to all listeners/observers after its value changed.

Example:

var a = Observable('test');
a.listen(() {
    print(a.value);
});
a.value = 'First test';

Output result:
test
First test

Computed

It's an observable too, but its value will be calculated from other observable object.
It's smart to know which observable object that it's depend on, and auto rebuild value when value of dependencies has changed.

Example:

var a = Observable(false);
var b = Observable(1);
var c = Computed(() => a.value ? b.value*10 : 0);

print(c.value); //result 0, depend on only 'a';
b.value = 2;
print(c.value); //result 0, depend on only 'a'
a.value=true;
print(c.value);//result 20, depend on 'a', 'b'

Computed object is only recalculate if there are listeners on it or when access to its value.
And the calculation is a async process, so observable can change value many times, but Computed just run one

Example:

var a = Observable(0);
var b = Computed(() => a.value);
b.changed(()=>print(b.value));
for(var i=0; i<1000; i++) a.value=i;
print(b.rebuildCount); //result 1

Computed is just give readonly value, thus there are an object Commission used to read+write value

Validator

Built-in validate:

  • required: value can not null, empty string, zero
  • email: value must be valid email address
  • range: check number value, string length, array length
  • pattern: check string matched RegExp
  • contains: value must in array
  • unique: value not exist in array
  • true: value must be true
  • custom: check value by a custom function
  • async: check value by a async function
  • all: combine and check all validators
  • least: combine and check all validators but it stopped at the first invalid
  • not: negative a validator

Observable has a property valid error, they're observables value of validation status.

Validator also has features:

  • condition: used to check something before execute validate
  • message: custom message, default message, support for localize
  • And easy way to custom or extends new validation

Example:

//use with Validator....
var email = Observable('',
            validator: ValidatorLeast([ValidatorRequired(), ValidatorEmail()])
);

//use with Map data config
var email = Observable('',
            validator: Validator.convert({
'least': {
    'validators': {'required': 'Email is required', 'email': true}
}
}));
//current status
print(email.valid);
//current invalid message
print(email.error);
//listen on validation
email.listen(() {
  if (email.valid) {
    //do something
  }
});

View more details in Wiki, API

dart-observable's People

Contributors

hientrung avatar

Watchers

James Cloos 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.