decorator-js
v0.1.0
Published
Simple decorators for your js objects
Downloads
10
Maintainers
Readme
decorator
Decorate your objects easily.
Great for keeping common view presentation logic in one place.
Example
Given you have some data that needs some presentation logic
var data = {
firstName: 'Daffy',
lastName: 'Duck',
addressLine1: 'Level 10',
addressLine2: '28 Wells St',
city: 'Sydney',
postcode: 2000
country: 'Australia'
}Create a decorator
// file: UserDecorator.js
var assign = require('object-assign');
var d = require('decorator');
// methods you would like accessible on the decorated object
module.exports = assign(d, {
fullName: function() {
return this.firstName + ' ' + this.lastName;
},
formattedAddress: function() {
return this.addressLine1 + join("\r\n")
+ this.addressLine2 + join("\r\n")
+ this.city + join(" ") + this.postCode + join("\r\n")
+ this.country;
}
});Decorate
// file: someOtherFile.js
var UserDecorator = require('../path/to/UserDecorator.js');
// data from a service like a `Store` or `API`
var user = UserDecorator.decorate(data)
user.fullName() // Daffy Duck
user.formattedAddress() // You get the idea...
// Old properties still accessible as normal
user.firstName // Daffy
License
MIT. Copyright (c) 2015 Floating Points Pty Ltd
