GithubHelp home page GithubHelp logo

jbking / beautifulproperties.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from monjudoh/beautifulproperties.js

0.0 2.0 0.0 744 KB

Home Page: http://monjudoh.github.io/BeautifulProperties.js/

License: GNU General Public License v2.0

HTML 2.33% JavaScript 94.90% CSS 2.77%

beautifulproperties.js's Introduction

BeautifulProperties.js - Extension of ECMAScript5 property.

Links

Features

LazyInitializable

BeautifulProperties.LazyInitializable.define can define a property, it's initialized at first access.

var proto = {};
BeautifulProperties.LazyInitializable.define(proto,'boundFunction',function(){
  return (function () {
    console.log(this);
  }).bind(this);
});
var object1 = Object.create(proto);
object1.a = 1;
var boundFunction1 = object1.boundFunction;
boundFunction1();// {a:1,boundFunction:fn}
var object2 = Object.create(proto);
object2.a = 2;
var boundFunction2 = object2.boundFunction;
boundFunction2();// {a:2boundFunction:fn}

Hookable

BeautifulProperties.Hookable.define supports hooks for setting/getting property,replace or modify value.

hooks

var object = {};
BeautifulProperties.Hookable.define(object,'key');
BeautifulProperties.Hookable.addHooks(object,'key',{
  beforeGet : function(val){
    console.log('beforeGet');
  },
  afterGet : function(val){
    console.log('afterGet',val);
    return val;
  },
  beforeSet : function(val,previousVal){
    console.log('beforeSet',val,previousVal);
    return val;
  },
  afterSet : function(val,previousVal){
    console.log('afterSet',val,previousVal);
  }
});
object.key = 1;
object.key = 2;
object.key;//1

modify getting value

var object = {};
BeautifulProperties.Hookable.define(object,'key');
BeautifulProperties.Hookable.addHook(object,'key','afterGet',function(val){
  return val * 2;
});

object.key = 1;
object.key;//2

modify setting value

var object = {};
BeautifulProperties.Hookable.define(object,'key');
BeautifulProperties.Hookable.addHook(object,'key','beforeSet',function(val,previousVal){
  return val * 2;
});
object.key = 1;
object.key;//2

Events

var object = {};
BeautifulProperties.Events.on(object,'eventType',function(ev){
  console.log('event handler is called.');
});
BeautifulProperties.Events.trigger(object,'eventType');

event bubbling.

A event bubble up to the prototype of the object.

var proto = {};
var object = Object.create(proto);
BeautifulProperties.Events.on(proto,'eventType',function(ev){
  console.log('event handler is called.');
});
BeautifulProperties.Events.trigger(object,'eventType');

controlling event bubbling.

var ancestor = {};
var object = {};
BeautifulProperties.Events.Ancestor.setRetriever(object,function(){
  return ancestor;
});
BeautifulProperties.Events.on(ancestor,'eventType',function(ev){
  console.log('event handler is called.');
});
BeautifulProperties.Events.trigger(object,'eventType');

Observable

BeautifulProperties.Observable.define supports key/value observation.

var object = {};
BeautifulProperties.Observable.define(object,'key');
object.key=1;
BeautifulProperties.Events.on(object,'change:key',function(ev,val,previousVal){
  console.log(val,previousVal);// val:2,previousVal:1
});
object.key=2;

Installation and usage

In browsers:

<script src="BeautifulProperties.js"></script>

In an AMD loader like RequireJS:

require(['BeautifulProperties'], function(BeautifulProperties) {
});

Changelog

0.1.11

  • fixes
    • event.previousTarget,event.currentTarget in BeautifulProperties.Events.Ancestor~ancestorRetriever.

0.1.10

  • added
    • BeautifulProperties.Events.Event#previousTarget property
  • changed
    • BeautifulProperties.Events.Ancestor~ancestorRetriever callback
      • has 2 params.
      • If the ancestorRetriever returns undefined,Ancestor.retrieve method returns the prototype of the target object.

0.1.9

  • Export as global,AMD,CJS.
  • Many refacotoring.
  • deprecated
    • BeautifulProperties.Hookable.addHooks method

    • BeautifulProperties.Internal namespace is deprecated.

  • removed
    • BeautifulProperties.getRaw method (deprecated since 0.1.6)
    • BeautifulProperties.setRaw method (deprecated since 0.1.6)

Author

monjudoh https://github.com/monjudoh

Contributors

beautifulproperties.js's People

Contributors

monjudoh avatar

Watchers

MURAOKA Yusuke avatar 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.