0

I have two files: main.js and events.js.

In main.js i have defined some functions, which look's like this:

define(["jquery"], function() {

    var a =  {
       function_a: function(){
         some_actions;
       }
    }

    var b =  {
       function_b: function(){
         some_actions;
       }
    }

    return a, b;
});

and in events.js i have some calls for this functions:

require(['jquery','app/main'], function($, a){
    selector.on('click', function(){
        a.function_a();
    });
});

require(['jquery','app/main'], function($, b){
    selector.on('click', function(){
        b.function_b();
    });
});

Something is wrong because i can't call more than one of this functions, can anybody help?

2
  • 1
    return a, b; - this line doesn't work like you seem to think. Commented Feb 11, 2014 at 12:47
  • ok, why? do i need create some array? Commented Feb 11, 2014 at 12:49

1 Answer 1

2

Instead of return a,b which is invalid statement you need to return an object for example: return {'a':a, 'b':b}

Then in your code you can write:

require(['jquery','app/main'], function($, obj){
    selector.on('click', function(){
        obj.a.function_a();
        obj.b.function_b();
    });
});

You could use also array. Then return will be return [a,b] and in require function:

require(['jquery','app/main'], function($, obj){
    selector.on('click', function(){
        obj[0].function_a();
        obj[1].function_b();
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

ok, but this return should be var obj = { 'fn': a, 'fn': b }; return obj; ?
you cannot have same id in JSON. You could use array. I will add update
A minor nitpick: return a,b is not a invalid statement per se - it's just meaningless, as only b value is returned. The fact that a is evaluated too is obviously of little use. ) But overall, very nice explanation, +1 from me.

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.