ncubica
ncubica

Reputation: 8495

Meteor.Methods in one function, possible architecture solution

So I like to code with anonymous functions, and Meteor.methods break that for me. So I create a Meteor.Methods like this

//SERVER SIDE

Meteor.startup(function () {
    // code to run on server at startup
    //expose server methods.

        Meteor.methods({
            _SERVER_ : function(args){
                try{
                    var funcStr = args.func.split("."); //split on the function parameter
                    var scopeStr = funcStr[0]; //get the scope of the function
                    funcStr.splice(0,1); //remove the scope and get the deep path

                    var path = funcStr.join("."); //join the array and stick it with "."

                    console.log("util.funcString("+ scopeStr +","+ path +")(" + args.data + ");");

                    if( myapp.hasOwnProperty(scopeStr) ) //see if the function exist on myapp object
                    {
                        var scope = myapp[scopeStr]; //get the scope of the function
                        var response = util.funcString(scope, path)(args.data); //execute the function
                        console.log("myapp :" + args.func);

                        return response;

                    }else{
                        return "myapp don't have the method: " + args.func;
                    }

                }catch(e){
                    return "myapp has a wtf moment and its saying:" + e;
                }
            }
        });
    });

So pretty much the function expect a call like this from the client. And its gonna call myapp.page.add

//CLIENT SIDE

    Meteor.call("_SERVER_",{
            func : "pages.add",
            data : page
        },function(err, value){
            insertNewPage(err,value);
        });

The benefit is now I can create a function in the server side like this.

//SERVER SIDE

   myapp.page = (function(){

        var privateVar = "private";

        //private
        function doSomething(){
        }

        //public via the return object
        function add(){
           console.log("called from client side");
        }

        return{
           add : add
        }
    })();

And my app now its more like modular, can be split very simple in different files and creating any namespace you want to.

THE QUESTION DO Im breaking any METEOR rule? its this is not secured? its a bad idea? Any suggestion is welcome, I'm still new in Meteor.

THANKS

Upvotes: 0

Views: 142

Answers (2)

The Software Barbarian
The Software Barbarian

Reputation: 459

What you're doing is obfuscating something Meteor has worked hard to do: making things simpler for developers. Meteor gives you the simplest, most direct way to get things done that I've seen since my early days in C. You just write clean code to do what you want. Meteor does this with web apps, and it's a very beautiful thing.

The bottom line: if you're adding layers of indirection in your Meteor code, you're likely overthinking the problem. Just relax. :-) Keep the client stuff in client/, the server stuff in server/ and the shared stuff outside of them. Enjoy the simplicity.

Upvotes: 1

stubailo
stubailo

Reputation: 6147

You can replace this:

myapp.page = (function(){

    var privateVar = "private";

    //private
    function doSomething(){
    }

    //public via the return object
    function add(){
       console.log("called from client side");
    }

    return{
       add : add
    }
})();

with:

myapp.page = (function(){

    var privateVar = "private";

    //private
    function doSomething(){
    }

    //public via the return object
    function add(){
       console.log("called from client side");
    }

    Meteor.methods({
        "page.add": add
    });
})();

If you want to call your methods with Meteor.call("page.add", arg1, arg2).

Also, since Meteor automatically scopes your variables between files, you don't need the closure around privateVar.

Upvotes: 2

Related Questions