mardi 2 juin 2015

The optimal javascript design pattern for collection of commands

I'm working on large enterprise social project, this project has a lot of social actions (like, dislike, rate, bookmark ...) on a lot of different items (news, announcements, documents .... ). I need to build one custom JavaScript library to work with these command from client side using ajax.

For technologies and code every thing is fine and I wrote all functional requirements. my issue is with design pattern and best practices. it is difficult to maintain and read this library. this is the library JavaScript module:

var social = (function () {

var _commandsIndex = 0;

var _initiateCommand = function (event) {

    if (event) {
        if (event.preventDefault) {
            event.preventDefault();
        } else {
            event.returnValue = false;
        }
    }

    _commandsIndex++;

    event.commandID = _commandsIndex;

    return false;
};

var _onSuccess = function (event) {

    //use event.commandID to hide notification
};

var _onFailure = function (event) {
    //use event.commandID to change then hide notification message
};

var like = function (event) {
    _initiateCommand(event);

    //call ajax then send event to _onSuccess & _onFailure callback

    return false;
};

var dislike = function () {
    _initiateCommand(event);

    //call ajax then send event to _onSuccess & _onFailure callback

    return false;
};

return {
    like: dislike,
    dislike: dislike,
    bookmark: bookmark,
    ...
};

})();

I decide to rebuild this library. so the following are my questions:

  1. what is the fit design pattern to such this functionality?
  2. How can avoid rebated _initiateCommand(event); and return false; on every command? can I inherit functions?
  3. other recommendations

Aucun commentaire:

Enregistrer un commentaire