Class.accessors
Class.accessors helps you to manage options (parameters) in a class with easy creation of accessors (get / set).
It provides two methods to your classes:
setOptions ( object
defaultOptions
, object
userOptions
[, bool
accessorize
] )
Allows to extend defaultOptions with userOptions while creating accessors for each option.
  • defaultOptions : The object containing the default options
  • userOptions : The object containing options defined by the user. It will automatically be merged with defaultOptions through Object.extend.
  • accessorize (optional) : Value is false by default. If true, create accessors for each entry in (defaultOptions + userOptions) merged.
var Car = Class.create(Class.accessors, {
    initialize: function(options) {
        this.options = {
            brand : 'Audi',
            color : 'black',
            'ref-model' : 'A5'
        };
        
        // set users options and create accessors
        this.setOptions(this.options, options, true); 
    }
});

var myCar = new Car({
    color: 'red'
});

myCar.getBrand(); // --> Audi
myCar.getColor(); // --> red
myCar.getRefModel(); // --> A5

myCar.setBrand('BMW');
myCar.setRefModel('X5');

myCar.getBrand(); // --> BMW
myCar.getRefModel(); // --> X5
createAccessors ( object
options
, array|object
entries
)
Create accessors for each entries in options
  • options : The object containing the parameters.
  • entries : An array or literal javascript object.
var Car = Class.create(Class.accessors, {
    initialize: function(options) {
        this.options = {
            brand : 'Audi',
            color : 'black',
            'ref-model' : 'A5'
        };
        
        // set users options but NOT CREATE ACCESSORS
        this.setOptions(this.options, options, false); 
        
        // here we create accessors only for the options 'brand' and 'ref-model'
        this.createAccessors(this.options, ['brand', 'ref-model'] );    }
});

var myCar = new Car({
    brand: 'Mercedes-benz'
});

myCar.getBrand(); // --> Mercedes-benz
myCar.getColor(); // --> function getColor is undefined
myCar.getRefModel(); // --> A5
Browser extension
Prototype Xtensions provides an extension of Prototype.Browser to know very simply the name and version of the browser user.

For now, here browsers supported: IE, Firefox, Opera, Safari, Camino, Khtml, Mozilla, AIR, Flock.
if(Prototype.X.Browser.IE >= 7) {
    // IE greater than or equal to 7.0
}

if(Prototype.X.Browser.Firefox >= 3) {
    // You have the last firefox version
}
EventManager
EventManager provides the ability to create customized events for your classes. It is very easy to use:
A simple example :
var Car = Class.create({
    initialize: function() {
        this.events = new EventManager(this);
        this.param  = 'test';
    },

    observe: function(name, callback) {
        this.events.observe(name, callback);
    }

    foo: function(bar) {
        this.events.notify('car:foo', bar);
    }
});

var myCar = new Car();

myCar.observe('car:foo', function(bar) {
    console.log('Car.foo is called, the argument is ' + bar);
    
    // Note you can access on the object :
    console.log(this.param); // --> 'test'
});

myCar.foo('foobar');
Number
Prototype Xtensions provides a set of methods to simplify the calculation of time.
Number.minute()
(1).minute(); // -> 60 (sec.)
Number.hour()
(1).hour(); // -> 3600 (sec.)
Number.day()
(1).day(); // -> 86400 (sec.)
Number.week()
(1).week(); // -> 604800 (sec.)
Number.month()
(1).month(); // -> 2629743.83 (sec.)
Number.year()
(1).year(); // -> 31556926 (sec.)
Number.time()
// -> Current UNIX timestamp in milliseconds
(0).time(); 

// -> UNIX Timestamp + 1 year in milliseconds
(1).year().time(); 

// -> UNIX Timestamp + 1 year in seconds
(1).year().time().toSeconds();
Example practice
// This cookie will expire in 3 months
Cookie.set( 'foo', 'bar', (3).month() );
String
String.ucfirst();
Make a string's first character uppercase
var str = 'MYTEXT';

str.ucfirst(); // --> 'Mytext'
Copyright 2008 Simon Martins - License