Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Date: Thu, 20 Dec 2012 16:28:50 -0500
From: Daryl Koopersmith <koop@wordpress.org>
Subject: Re: [wp-hackers] Javascript bind event for 3.5's new
uploader?
To: wp-hackers@lists.automattic.com
Message-ID: <DB193711A3AC46F1AA76AAE5940586BA@gmail.com>
Content-Type: text/plain; charset="utf-8"
> I'm trying to add an action when the uploader is displayed, but I
> can't seem to call the .on or .bind from external code.
>
> Unfortunately, it seems this can't be added from external code.
> wp.media.editor.on, wp.media.editor.bind, and others will not work.
Hi Luke,
wp.media.editor is used to manage instances of editor-specific media managers. If you're looking to trigger an event when opening the default media modal, you'll want to grab a reference to the media manager by calling wp.media.editor.add('content'). We're calling "add" here instead of "get" to make sure the modal exists, because "get" may return undefined (and don't worry, "add" only creates the instance once). You can then call the .on method on that object and your code will run just fine.
For example:
var media = wp.media.editor.add('content');
media.on( 'open', function() {
console.log( 'Opening the modal?' );
});
Hope this helps.
Cheers,
Koop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment