Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mark-5/roles-js
Role::Tiny roles for JavaScript
https://github.com/mark-5/roles-js
Last synced: about 2 months ago
JSON representation
Role::Tiny roles for JavaScript
- Host: GitHub
- URL: https://github.com/mark-5/roles-js
- Owner: mark-5
- Created: 2014-11-03T03:14:44.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2014-11-16T01:22:37.000Z (about 10 years ago)
- Last Synced: 2023-06-03T04:11:35.523Z (over 1 year ago)
- Language: JavaScript
- Size: 250 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Roles.js
Roles.js is a javscript library for [Role::Tiny](https://metacpan.org/pod/Role::Tiny) inspired traits. This includes support for providing methods, requiring methods, detecting conflicting methods, and installing method modifiers.
```
var HasFoo = new Role({
get_foo: function(){ ... },
}, {
after: {initialize: function(){ ... }},
before: {render: function(){ ... }}
});
var View = Backbone.View.extend({ ... });
var ViewWithFoo = Role.apply_roles(View, HasFoo);
```## Class Methods
### new(methods, modifiers)
The constructor to instantiate roles.
```
var MyRole = new Role({my_method: function(){ ... }}, {with: AnotherRole});
```#### methods
An object containing methods the role provides.
#### modifiers
This is an object containing Role::Tiny keywords - with, requires, before, around, after. with and requires can be keyed to either a single role/method name, or an array of roles/methods. Method modifiers are objects with method name keys, and modifier code values.
```
{
with: [Roles...],
requires: [method_names...],
before: {method: function(){ ... } },
around: {method: function(original, arg1, arg2) { ... original.call(this, arg1, arg2) ... }},
after: {method: function(){ ... }},
}
```### apply_roles(Class, ...)
The main entry point to Roles.js. Returns a copy of the given class, with the specified roles applied.
```
var ClassWithRoles = Role.apply_roles(Class, FirstRole, SecondRole);
```### is_role(MaybeRole)
### does_role(RoleOrClass)