GithubHelp home page GithubHelp logo

snitko / dart-attributable Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 1.0 16 KB

Define getters/setters for attributes on your Dart classes and invoke callbacks when they change

License: MIT License

Dart 100.00%

dart-attributable's Introduction

Attributable

Allows you to easily define getters/setters for attributes on your class and invoke callbacks when those attributes change.

Usage

A simple example may look like this:

import 'package:attributable';

class Button extends Object with Attributable {

  final List attribute_names = ['caption', 'color', 'enabled'];

  final Map attribute_callbacks = {
    'default' : (attr_name, self) => print("Attribute $attr_name now has a new value: ${self.attributes[attr_name]}"), 
    'caption' : (attr_name, self) => print("Caption is now '${self.caption}'"), 
    'enabled' : (attr_name, self) => print("Enabled is now set to '${self.enabled}'") 
  };

  // Don't forget noSuchMethod() definition here (see below) or it won't work!

}

The default callback is invoked if no other callback for the given attribute is defined. Obviously, you may omit it, so nothing happens when your attribute changes.

So now we can create instances of our class and set attributes on them:

main() {

  var button = new Button();

  button.caption = "New caption"; // => Caption is now 'New caption' 
  button.enabled = true;          // => Enabled is now set to 'true' 
  button.color   = 'green';       // => Attribute color now has a new value: green

}

There's also a method which allows updating many attributes all at once:

button.updateAttributes({ 'caption': 'New caption', 'color': 'green'});

Needless to say, callbacks will also be invoked for the attributes listed. The updateAttributes() is slightly more powerful, see documenation to learn more.

IMPORTANT: at this point, in order for this to work, it is not enough to simply mix this abstract class into your class, you also have to define noSuchMethod() callback manually in your class, so that it looks something like the following:

noSuchMethod(Invocation i) {  
  try {
    return prvt_noSuchGetterOrSetter(i);
  } on NoSuchAttributeException {
    super.noSuchMethod(i);
  }
}

The Invocation object gets passed into the prvt_noSuchGetterOrSetter(i) which then checks if any attribute with such a name are defined on the class. If not, then we return control to your class and it gets to call its original noSuchMethod() method, which, most likely, will generate an exception. Of course, if your class has its own noSuchMethod() functionality, you'd have take it into account and construct the method accordingly.

See /example/button.dart for an example (you can run it from the terminal).

dart-attributable's People

Contributors

snitko avatar

Watchers

 avatar  avatar  avatar

Forkers

floitschg

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.