Skip to content

monjudoh/BeautifulProperties.js

Repository files navigation

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.addHook(object,'key','beforeInit',function(val){
  console.log('beforeInit',val);
  return val;
});
BeautifulProperties.Hookable.addHook(object,'key','afterInit',function(val){
  console.log('afterInit',val);
});
BeautifulProperties.Hookable.addHook(object,'key','beforeGet',function(){
  console.log('beforeGet');
});
BeautifulProperties.Hookable.addHook(object,'key','afterGet',function(val){
  console.log('afterGet',val);
  return val;
});
BeautifulProperties.Hookable.addHook(object,'key','beforeSet',function(val,previousVal){
  console.log('beforeSet',val,previousVal);
  return val;
});
BeautifulProperties.Hookable.addHook(object,'key','afterSet',function(val,previousVal){
  console.log('afterSet',val,previousVal);
});
object.key = 1;
object.key = 2;
object.key;

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','beforeInit',function(val,previousVal){
  return val * 2;
});
BeautifulProperties.Hookable.addHook(object,'key','beforeSet',function(val,previousVal){
  return val * 3;
});
object.key = 1;
object.key;//2
object.key = 1;
object.key;//3

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');
BeautifulProperties.Events.on(object,'init:key',function(ev,val){
  console.log(val);// val:1
});
BeautifulProperties.Events.on(object,'change:key',function(ev,val,previousVal){
  console.log(val,previousVal);// val:2,previousVal:1
});
object.key=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.2.0

  • changed
    • It trigger init:{key} event when Observable property is initialized.
  • removed
    • BeautifulProperties.Hookable.addHooks method
    • BeautifulProperties.Internal namespace
    • context property of BeautifulProperties.Events~BindingOptions

0.1.12

  • added
    • Multiple binding is supported in BeautifulProperties.Events.
  • changed
    • context property of BeautifulProperties.Events~BindingOptions is renamed to thisObject.
  • deprecated
    • context property of BeautifulProperties.Events~BindingOptions

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