Skip to content

Conversation

@adamcoulombe
Copy link
Contributor

Adds a .size() function to the $.Callback() object to return the list length so we can know if/how many functions have been added

@jaubourg
Copy link
Member

jaubourg commented Jan 4, 2013

list can be undefined when the Callbacks instance is disabled: https://github.com/adamcoulombe/jquery/blob/6d4ad66c572779adacdc36aa7452d2354efe62bf/src/callbacks.js#L155

Having some unit test would probably have shown the issue ;)

@adamcoulombe
Copy link
Contributor Author

Ah thanks! Will do unit tests in the future. Patched it and had a go at it with QUnit this time.

@mikesherov
Copy link
Member

@adamcoulombe, you still need to add a unit test showing the usage of the function if you want it to have a shot of being added to the library.

@dmethvin
Copy link
Member

dmethvin commented Jan 8, 2013

I'm not inclined to land this without a use case. @jaubourg what do you think? I am concerned that the caller would need/want to know more about the state of the list and that leads us down a road we don't want to follow.

@jaubourg
Copy link
Member

jaubourg commented Jan 8, 2013

I have nothing against the patch per se. I hear your concern but we already have has in the API... maybe getting the size of the list is pushing it a bit... like just knowing if there are callbacks attached should be enough?

@adamcoulombe
Copy link
Contributor Author

The main use case is just knowing whether a $.Callbacks object has had functions added to it. Like @jaubourg pointed out, you can use has to know whether a certain single function was added, but no way to just check if there are functions or not.

http://jsbin.com/ibibon/1/edit

Knowing the list length seems like the simplest and most flexible way of doing this. The other way I was thinking, if you're not into the idea of adding size, was to be able to alternatively pass no arguments to has which would just check if there are functions or not plain and simple.

@jaubourg
Copy link
Member

jaubourg commented Jan 8, 2013

I kinda like the idea of using has without an argument. I see value in the information (are there callbacks attached) but the length of the internal array does seem too much of an internal implementation information to me.

removed .size() and instead modified the .has() method to alternatively
accept no arguments to check if there are any callbacks attached or not

updated unit tests with .has() method
@adamcoulombe
Copy link
Contributor Author

src/callbacks.js Outdated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use if (fn) and swap if/else case

Also updated comments with descriptions that reflect updated .has()
method
@adamcoulombe
Copy link
Contributor Author

thanks @staabm I've updated the logic to use if ( fn ) { ... but using return list && list.length > 0 returns undefined on a disabled list

@dmethvin
Copy link
Member

I like the idea of using .has() for this ... @jaubourg did you want to land this?

src/callbacks.js Outdated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want a boolean but still more compact an expression, you could go for:

return !!( list && list.length && ( !fn || jQuery.inArray(fn,list) > -1 ) );

Unless other think it too cryptic (and provided I got the logic right, which is not a given :P)

@jaubourg
Copy link
Member

Yes. There is value in this and having .has() handling it is prefect imo (and small).

@adamcoulombe
Copy link
Contributor Author

@jaubourg thanks for the help! The !! cast to bool was what it needed. Added new commit - not quite the same as what you posted, maybe a little more readable IMO and slightly shorter. Thoughts?

@dmethvin
Copy link
Member

Is this ready?

@jaubourg
Copy link
Member

Unit test needs some reformatting but it's good for a merge and we can make those (very minor) adjustements after.

@adamcoulombe
Copy link
Contributor Author

@dmethvin Done.

@dmethvin dmethvin closed this in 54fc5fd Jan 27, 2013
mescoda pushed a commit to mescoda/jquery that referenced this pull request Nov 4, 2014
@lock lock bot locked as resolved and limited conversation to collaborators Jan 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

5 participants