Templarian / ui.bootstrap.contextMenu

AngularJS Bootstrap UI Context Menu
MIT License
259 stars 127 forks source link

What is the function signature for the variable text #31

Closed lo-co closed 8 years ago

lo-co commented 8 years ago

Just a question, but I don't see labels showing up here. You have the following code where you expect a string for the text in the first argument but account for a function if the type is not a string (line 25):

 var text = typeof item[0] == 'string' ? item[0] : item[0].call($scope, $scope, event, model);

What is the intention of the function for item[0] (I assume to return a string) and what is this signature?

You have a similar definition below this (line 30):

var enabled = angular.isDefined(item[2]) ? item[2].call($scope, $scope, event, text, model) : true;

It seems to have the same signature as the function call above. What is the intention of this line?

Templarian commented 8 years ago

https://github.com/Templarian/ui.bootstrap.contextMenu#menu-options The first code example shows the signature.

Basically it's an array that is overloaded to allow either a string or function as some people need dynamically named items depending on what they are right clicking.

function ($itemScope, $event, model) {
    return $itemScope.item.name;
}

Note: as shown in the example model is an optional attribute if you need to easily pass in an additional object.

Templarian commented 8 years ago

Sorry forgot to answer the second part of the question. The 2nd line of code is for enable/disabling of the context menu items. Very useful when right clicking items from a list where some have different actions.

Please look at the example codepen in the readme. It demos these abilities.

lo-co commented 8 years ago

OK. Thanks Austin. Now for a question born out of my ignorance - why does the example function signature not resemble the called function signature?

Templarian commented 8 years ago

So, in JavaScript the first parameter is actually the function scope or this.

function foo(p1) {
 // this.bar == 'hello world!'
 // p1 == 'cool right'
}
foo.call({bar: 'hello world!'}, 'cool right')
Templarian commented 8 years ago

Oh wait, I think you found a bug... that 2nd param should be...

item[2].call($scope, $scope, event, model, text) Hmm... that needs to be fixed.

lo-co commented 8 years ago

Perfect...thanks.

Templarian commented 8 years ago

Okay, I've fixed this in v0.9.5, but I'm not going to document it since I'm not sure exactly the purpose of passing in the text would be. It also gets confusing since model is optional.

Templarian commented 8 years ago

v0.9.6 is out now also. Closing this issue.