1

I'm trying to do the following:

eventService.emit = function(name, optionalArg1, optionalArg2,... ){
    $rootScope.$broadcast(name, optionalArg1, optionalArg2,...);
};

with an unlimited number of optional arguments. (broadcast "definition": $broadcast(string, args...))

I thought

eventService.emit =$rootScope.$broadcast;

would work but it doesn't($broadcast function may access to $rootscope attributes) and

eventService.emit = function(){
    $rootScope.$broadcast(arguments);
};

doesn't seem to work

Thanks for the help

original code:

services.factory('eventService', function($rootScope, $http){
    var eventObject = {};

    eventObject.emit = function(name){

       $rootScope.$broadcast(name);

    };
    return eventObject;
});
2
  • you can use the typeof operator to see if a variable is defined Commented Aug 9, 2012 at 19:54
  • eventService.emit = function(){ $rootScope.$broadcast(Array.prototype.slice.call(arguments)); }; doesn't seem to work, the event thrown by $broadcast is never catched Commented Aug 9, 2012 at 20:18

3 Answers 3

6

You could try

eventService.emit = function(){

    $rootScope.$broadcast.apply($rootScope, arguments); //you can change "this" to whatever you need
};

Here you are executing $rootScope.$broadcast with the parameters in the arguments "array" (it's not really an array but behaves like one), and using this (the parameter) as this in the function.

Sign up to request clarification or add additional context in comments.

3 Comments

It is probably better to pass $rootScope instead of this. That's more like what the question is asking for - though it may or may not matter in practice.
Yes it's probably right, I really didn't know what would suit the situation better, hence the comment :).
Is is working with the $rootscope instead of 'this'...thx guys
1

You could use apply() (documentation here):

eventService.emit = function(name, optionalArg1, optionalArg2,... )
{
    $rootScope.$broadcast.apply(this, arguments);
};

[1]:

2 Comments

what is the purpose of the first argument??
it seems like a valid solution (when I look at the doc) but I can't make it work....i also tried with the function: 'call': developer.mozilla.org/en-US/docs/JavaScript/Reference/…
0

What I do when I want a lot of options is this:

function myFunction(options){
 if( options["whateverOptionYouWant"] != undefined ){
  //TODO: implement whatever option you want
 }
 if( options["whateverOTHEROptionYouWant"] != undefined ){
  //TODO: implement whatever OTHER option you want
 }
}

and so on for as many options as I need.

Call it like this:

myFunction({ whateverOptionYouWant: "some option variable" });
myFunction();
myFunction({ 
 whateverOptionYouWant: "some option variable", 
 whateverOTHEROptionYouWant: "some other variable"});

1 Comment

it is not really the solution I expected, I would like to use the "arguments" object which is "A king of" array (javascriptweblog.wordpress.com/2011/01/18/…)

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.