groupJS logo

About groupJS

GroupJS is an implementation pattern of grouping objects for so-called "instances" purpose. Instead of putting all attributes in a single object, it is recommended to create an object as a group object to manage and connect other objects called members. In the group object, there is a member list to maintain the relationship between group object and its members. Group object acts as a communicator between members. It also provides a single interface outside the group. Thus, object gets its own freedom of evolving its function without changing interaction of another object (class).

Why do I need groupJS

(As you may already know that javascript takes prototypal inheritance, it creates flexible programming style. However, I am not a big fans of coding with .prototype. all the time (say composition). This method drains up my brain. Thus, I started looking for an implementation pattern fitting myself.

At first I jumped in the area of Backbone.js and its best friend marionette. They are kind of "Event Driven" as far as I understand, which turn object having receiving message function. They are surly a brilliant set. But I think they are still too flexible in terms of coding style. That is why I came up with a simple idea: "Create a object group to bridge other objects.")

Documentation

Take a look at basic code

var obj = {
    create: function(name){
        var newObj = Object.create(this);
        if (!name && this.hasOwnProperty('name')) {
            name = this.name; //name from originate during instance
        }
        newObj.name = name;
        return newObj;
    },
    extend: function() {
        for(var i=0; i<arguments.length; i++)
            var extObj = arguments[i];
            for(var key in extObj)
                this[key] = extObj[key];
    },
    command: function() {
        var self = this;
        return function(cmd, opt) {
            return self[cmd](opt);
        };
    },
    thisObj: function() { //for debug
        return this;
    },
};

var group = obj.create('group');
group.extend({
    create: function(name) {
        var newObj = obj.create.apply(this, arguments);
        //all members should recreated within new group
        newObj._buildMemberList();

        return newObj;
    },
    _buildMemberList: function() {
        if (!this._memberList){ //base group
            this._memberList = {};
        }
        else if (!this.hasOwnProperty('_memberList')) { //inherited group
            var prototypeMemberList = this._memberList;
            this._memberList = {}; //in object level memberList
            for (var key in prototypeMemberList) {
                var memberCmd = prototypeMemberList[key];
                var newMember = memberCmd('create');
                newMember.group = this; //member

                this._memberList[key] = newMember.command();
            }
        }
    },
    join: function(member) {
        //add new member in command interface
        var newMember = member.create(member.name);
        newMember.group = this;
        this._memberList[member.name] = newMember.command();
    },
    call: function(memberName, methodName, opt) {
        var memberCmd = this._memberList[memberName];
        return memberCmd(methodName, opt);
    },
});

Usages

Object Inheritance

Group

Examples

  1. Check the test cases and you might get some idea
  2. Check example folder
  3. In node.js npm install groupjs

    var Grp = require('groupjs'); 
  4. with requirejs require(['groupjs'], function(Grp){});

Build

grunt

Release

To npm, change version number
npm publish ./

Register in bower
bower register groupjs git://github.com/mainnote/groupJS.git

Change version in bower, 
git tag -a 0.0.27 -m "Tagging 0.0.27"

Test

grunt test
or
grunt watch

Contributors

George Zhang < service@mainnote.com >