Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uploading Files get confused when using "file" and "file_list" on the same page. #250

Closed
heddesheimer opened this issue Mar 13, 2015 · 10 comments
Labels

Comments

@heddesheimer
Copy link

I found an issue when using "file" and "file_list" on the same page. It's regardless if I put them in different metaboxes, or in the same metabox. Here is how to reproduce the problem:

In my functions.php, I declare the fields like this:

https://gist.github.com/heddesheimer/2ab0cff78c2e326a0f0f

And here is how to replicate the problem:

Create a new page, add one or two images to the file list, then add an image to the single file.

Then go back to the file list and add another image from the media library. The selected image will not display in the file_list as expected, but it shows up in the single file along the other image that was already selected.

On the other hand, sometimes when I select some images for the file_list, then go back to the single file and change that, the selected image for the single file shows up in the section for file_list. It looks like the code does confuse the sections to show the thumbnails, so that they don't appear the correct file type.

It seems to depend on which one I use first, it will mess up when I go back to one that I have already uploaded and want to change or add images.

I recorded the issue to show how you can reproduce it:

https://www.youtube.com/watch?v=2H-yh1AjFbw

I will check if I can find a fix or workaround in the next days, because I need it for a client project and the client found this issue when playing around. If I find a workaround, I will post it here. Just in case that I can't find a solution to fix it, it would be great if you can check if it's possible to fix in the next release.

Thanks
Marian

@heddesheimer
Copy link
Author

OK, here is what I found.

In the file cmb2.js the var $formfield is set while declaring this function:

cmb._handleMedia = function( formfield, isList ) {

within this function, another function is declared:

cmb.mediaHandlers.single = function( selection ) {

For some reason, the $formfield variable is not identical when the .single function is called, this leads to the wrong field, when replacing the thumbnail. A quick fix would be to add another assignment for $formfield within this .single function (and probably also for cmb.mediaHandlers.list), to set $formfield to the correct value:

$formfield = $id( cmb.formfield );

I don't have too much experience with javaScript and jQuery to find out why the $formfield variable does not get the correct value. Maybe it's something related with variable scope when declaring functions within functions. From my first tests, it seems to fix the problem when I assign $formfield to the initial value again.

It would be nice if you could verify that I have nailed down the problem correctly and if you could check if you can implement it in the next update when I haven't missed anything here.

Thanks
Marian

@jtsternberg
Copy link
Member

I made some recent changes to the JS. Can you please test with the Trunk branch?

@heddesheimer
Copy link
Author

I checked it and it's still having the same bug. Did you read what I wrote about the $formfield variable? I don't see that you changed anything regarding to this in your cmb2.js.

@heddesheimer
Copy link
Author

Any chance that you get this fixed? I just updated to 2.0.8 and it seems it still has the same problem. It's really disappointing, as I provided a possible solution already and nobody seems to care. Is it really that much trouble to implement a simple change like that, or am I the only one having this problem? It happens not only with image lists, but also if I have more than one image in a metabox.

@augustuswm
Copy link

I am running in to this same issue. I does look to be caused by having multiple file inputs on a single page. Each time a file input is clicked for the first time, the cmb.mediaHandlers.list, cmb.mediaHandlers.single, etc. functions are being redefined. These handlers have a closure to the variable $formfield, and therefore the "last" file input to be clicked by the user will have its handler be used for all other file inputs. You do not have this behavior with cmb.formfield because each redefinition of the mediaHandlers have a closure to the cmb variable. The other variables local to cmb._handleMedia have the same issue.

I'm working on fixing this on our local installation.

Same issue as: #296

augustuswm added a commit to augustuswm/CMB2 that referenced this issue Jul 9, 2015
cmb.mediaHandlers.* are redefined on the first click of each “file” or
“file_list” on the page. Each time .list and .single are redefined the
local variables for the current “file” and “file_list” are closed over.
This breaks the handlers for any previously clicked “files” and
“file_lists”. Variables have been moved into the handlers themselves
rather than rewriting the handlers.

In reference to :CMB2#250,
CMB2#296
@jtsternberg
Copy link
Member

This should be good on trunk. Feel free to test.

@heddesheimer
Copy link
Author

Hi Justin,

trunk version works fine. When do you think it's rolled out to the official
plugin? Do you have a donation button somewhere. I'd be happy to donate
some money when this bug fix is rolled out finally.

Thanks
Marian

On Thu, Aug 6, 2015 at 4:02 AM, Justin Sternberg notifications@github.com
wrote:

This should be good on trunk. Feel free to test.


Reply to this email directly or view it on GitHub
#250 (comment).

@jtsternberg
Copy link
Member

@heddesheimer I shoot for monthly releases more or less, and we had a release this/last week.

@heddesheimer
Copy link
Author

OK, thank you. Then I'll wait. I'm glad you got it fixed eventually.

On Thu, Aug 6, 2015 at 6:47 PM, Justin Sternberg notifications@github.com
wrote:

@heddesheimer https://github.com/heddesheimer I shoot for monthly
releases more or less, and we had a release this/last week.


Reply to this email directly or view it on GitHub
#250 (comment).

@tw2113 tw2113 added the bug label Aug 10, 2016
@tw2113
Copy link
Contributor

tw2113 commented Aug 10, 2016

Closing as confirmed fixed.

@tw2113 tw2113 closed this as completed Aug 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants