0

In the code below I want to make generic functions available through the constructor. The object is for handling calls to an API. But if I call this._isEmpty(obj) from events.POST it always returns false. Placing the body of the _isEmpty() function does give the desired result. How do I solve this? Any tips on improving this structure are welcome ofcourse. I looked up some similar questions on SO but the questions/answers I found do not have similar nesting.

I make the following call:

var assystBridge = new global.assystBridge();
response.setStatus(assystBridge.events.POST(request, response));

Object:

var assystBridge = Class.create();
assystBridge.prototype = {
    initialize: function() {
        // Generic functions go here
        function _isEmpty(obj){
            // Checks whether an object has properties
            // Used for checking whether any data was posted or not
            // Returns true or false
            return Object.keys(obj).length === 0;
        }
    },
    events: {
        POST: function(request, response){
            var data = request.body.data;
            var responseText = '';
            if(this._isEmpty(data)){
            //if(Object.keys(data).length === 0){   
                response.setBody({"response":"NO data was posted", "data": data});
                return 400;
            } else {
                return 201;
            }

        },
        GET: function(request, response){

        },
        DELETE: function(request, response){

        }
    },
    type: 'assystBridge'
};
4
  • How do you call events.POST? Do you instantiate the assystBridge class and then call instance.events.POST? I am asking in order to establish what the meaning of "this" is in events.POST when you call it. Commented Sep 20, 2016 at 15:42
  • Updated the question Commented Sep 20, 2016 at 15:45
  • I am surprised that you don't get an error when you call response.setStatus(assystBridge.events.POST(request, response)); if somehow assystBridge doesn't inherit an _isEmpty function from somewhere else, the code should have normally broken cause the way it is written _isEmpty is not a member of the class or the prototype, it is just private to the initialize method. Commented Sep 20, 2016 at 16:02
  • It is on a Java backend that uses Rhino to implement a JS interface. It has some quirks so you are quite right in your assesment Commented Sep 20, 2016 at 16:04

2 Answers 2

1

If you move the generic function out of the initialize() method and have it be one of the properties of the prototype, then you can reference it via assystBridge.prototype._isEmpty().

For example:

assystBridge.prototype = {
    _isEmpty: function(obj) {
        // Checks whether an object has properties
        // Used for checking whether any data was posted or not
        // Returns true or false
        return Object.keys(obj).length === 0;
    },
    events: {
        POST: function(request, response){
            var data = request.body.data;
            var responseText = '';
            if(assystBridge.prototype._isEmpty(data)){
                response.setBody({"response":"NO data was posted", "data": data});
                return 400;
            } else {
                return 201;
            }

        },
    },
    type: 'assystBridge'
};
Sign up to request clarification or add additional context in comments.

1 Comment

So I made a top-level property genericFunctions which will hold all the generic functions, and added var gf = assystBridge.prototype.genericFunctions to events.POST. Now gf._isEmpty() works. Thanks!
1

Besides what already has been said and meanwhile got accepted too, you even might move a step further, creating a factory for assystBridge, thus being in much better control of handling function scope appropriately ...

var createAssystBridge = (function () { // - immediately invoked function expression
                                        //   acting as a generator for a factory that
                                        //   will be returned, thus creating a closure ...

    // ... due to shared code, that will be preserved within this closure.

    function isEmpty(obj) {
        // Checks whether an object has properties
        // Used for checking whether any data was posted or not
        // Returns true or false
        return Object.keys(obj).length === 0;
    }

    function postData(request, response) {
        var data = request.body.data;
        var responseText = '';
        if (isEmpty(data)){
            response.setBody({"response":"NO data was posted", "data": data});
            return 400;
        } else {
            return 201;
        }
    }
    function getData(request, response) {

    }
    function deleteData(request, response) {

    }

    function initializeAssystBridge() {

    }

    // returning the factory

    return function assystBridgeFactory() {
        var
            assystBridge = Class.create();
          //assystBridge = Object.create({});

        assystBridge.prototype = {              //  - assign shared code
            initialize: initializeAssystBridge, //    with every call
            events: {                           //    of this factory.
                POST    : postData,             //
                GET     : getData,              //
                DELETE  : deleteData            //
            },
            type: 'assystBridge'
        };
        return assystBridge;
    };

}());

var assystBridge = createAssystBridge();

1 Comment

Very interesting. I'll give this a go. tnx!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.