{"id":642,"date":"2018-04-23T08:36:18","date_gmt":"2018-04-23T08:36:18","guid":{"rendered":"https:\/\/buddydev.com\/mediapress\/?p=642"},"modified":"2018-04-23T17:19:35","modified_gmt":"2018-04-23T17:19:35","slug":"mediapress-uploader-events-lifecycle","status":"publish","type":"post","link":"https:\/\/buddydev.com\/mediapress\/api-guides\/mediapress-uploader-events-lifecycle\/","title":{"rendered":"MediaPress Uploader Events Lifecycle"},"content":{"rendered":"<p>Welcome to MediaPress uploader events life cycle guide. The guide is aimed at developer who want to create their own uploading experience with MediaPress.<\/p>\n<p>MediaPress uses plupload.Uploader for uploading files. It is the same library used by WordPress(and bundled with it). We have a Container class mpp.Uploader. The various instances of mpp.Uploader allows us uploading files.<\/p>\n<p>Note:- All the events listed on this page are fired by the MediaPress uploader(mpp.Uploader) on the document object.<\/p>\n<p>List of stages where you can hook into.<\/p>\n<ul>\n<li>\u00a0<a href=\"#files-selected\">On File selection<\/a><\/li>\n<li><a href=\"#upload-start\">On Upload Start<\/a><\/li>\n<li><a href=\"#upload-progress\">On Upload Progress<\/a><\/li>\n<li><a href=\"#upload-success\">On Upload Success<\/a><\/li>\n<li><a href=\"#upload-complete\">On All Upload Complete<\/a><\/li>\n<li><a href=\"#upload-error\">On Error<\/a><\/li>\n<\/ul>\n<blockquote><p>For all the events listed on this page, Any bound event listener callback will receive the Event object as the first parameter(You will ignore it most of the time) and an instance of mpp.Uploader as the second parameter. Rest of the parameters are determined by the events you need to hook to. Also, the events are triggered on <strong>document\u00a0<\/strong>object. The event listeners should be bound to the document object to correctly intercept the event.<\/p><\/blockquote>\n<h3 id=\"files-selected\">On File Selection:-<\/h3>\n<p>As soon as the user selects file(s), the files are added to our upload queue. mpp.Uploader triggers two events after the file selection.<\/p>\n<ul>\n<li><strong>mpp:uploader:file.added<\/strong> :- It is fired when a file is added to the upload queue. It fires for each file of the selection(in case of multiple selection)<\/li>\n<li><strong>mpp:uploader:files.added<\/strong> :- this event is fired when all the files from the selection have been added to the upload queue successfully.<\/li>\n<\/ul>\n<h4 id=\"mpp-uploader-file-added\">mpp:uploader:file.added :-<\/h4>\n<p>A listener bound to this event will receive 3 arguements<\/p>\n<ul>\n<li>Event object.<\/li>\n<li>mpp.Uploader instance<\/li>\n<li>file Backbone.Model instance<\/li>\n<\/ul>\n<p><strong>Example<\/strong>:- Attaching a listener using javascript.<\/p>\n<pre class=\"lang:js decode:true\">\t$(document).on('mpp:uploader:file.added', function (event, mpp_uploader, file) {\r\n\t\tconsole.log( \"File: \"+ file.name + \" successfully added to the queue\");\r\n    });<\/pre>\n<p>If you want to know which uploader triggered it( e.g are we working with activity upload or gallery upload or shortcode upload etc), Please see the <a href=\"#uploader-type\">guide to determine it<\/a>.<\/p>\n<h4 id=\"mpp-uploader-files-added\">mpp:uploader:files.added :-<\/h4>\n<p>It is fired when all the selected files are added to the upload queue. A listener bound to this event will receive the following arguements<\/p>\n<ul>\n<li>Event object<\/li>\n<li>mpp.Uploader object<\/li>\n<li>plupload.Uploader instance<\/li>\n<\/ul>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"lang:js decode:true\">\t$(document).on('mpp:uploader:file.added', function (event, mpp_uploader, uploader) {\r\n\t\tconsole.log( \"All files in current selection added to the queue\");\r\n    });<\/pre>\n<h3 id=\"upload-start\">On Upload Start:-<\/h3>\n<p>Before the upload starts for each of the file, following event is triggered<\/p>\n<ul>\n<li id=\"mpp-uploader-before-upload\"><strong>mpp:uploader:before.upload<\/strong><\/li>\n<\/ul>\n<p>The bound listener will receive following arguements<\/p>\n<ul>\n<li>Event Object<\/li>\n<li>mpp.Uploader instance<\/li>\n<li>plupload.Uploader instance<\/li>\n<li>file Backbone.Model instance<\/li>\n<\/ul>\n<p>You can use this hook to stop uploading a file.<\/p>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"lang:default decode:true\">\t$(document).on('mpp:uploader:before.upload', function (event, mpp_uploader, uploader, file) {\r\n\t\tconsole.log( \"We are about to start uploading\" + file.name );\r\n    });<\/pre>\n<p>&nbsp;<\/p>\n<h3 id=\"upload-progress\">On Upload Progress:-<\/h3>\n<p>If you need to hook to upload progress and do something, you can use the event:-<\/p>\n<ul>\n<li id=\"mpp-uploader-upload-progress\"><strong>mpp:uploader:upload.progress<\/strong><\/li>\n<\/ul>\n<p>The bound listener will receive following arguments:-<\/p>\n<ul>\n<li>Event object<\/li>\n<li>mpp.Uploader instance<\/li>\n<li>attachment :- Backbone Model object containing details about the uploading file.<\/li>\n<\/ul>\n<p>The event gives you access to mpp.Uploader and the attachment object. On attchment, you can check for the &#8216;loaded&#8217;, &#8216;percent&#8217; properties to check for the upload progress.<\/p>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"lang:default decode:true\">\t$(document).on('mpp:uploader:upload.progress', function (event, mpp_uploader, attachment) {\r\n\t\t\/\/ do something.\r\n    });<\/pre>\n<p>&nbsp;<\/p>\n<h3 id=\"upload-success\">On Upload Success:-<\/h3>\n<p>Whena\u00a0 file uploads successfully, the following event is fired for it<\/p>\n<ul>\n<li id=\"mpp-uploader-upload-success\">\u00a0<strong>mpp:uploader:upload.success<\/strong><\/li>\n<\/ul>\n<p>This even fires for each file individually.<\/p>\n<p>The listener attached to this event will receive following argumenets<\/p>\n<ul>\n<li>Event object<\/li>\n<li>mpp.Uploader instance<\/li>\n<li>attachment :- Backbone Model object containing details about the uploading file.<\/li>\n<\/ul>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"lang:js decode:true\">\t$(document).on('mpp:uploader:upload.success', function (event, mpp_uploader, attachment) {\r\n\t\tconsole.log( attachment );\r\n    });<\/pre>\n<h4><\/h4>\n<h4 id=\"upload-complete\">On All Upload Complete:-<\/h4>\n<p>When all files from the current queue get uploaded, the uploader fires the event:-<\/p>\n<ul>\n<li id=\"mpp-uploader-upload-complete\"><strong>mpp:uploader:upload.complete<\/strong><\/li>\n<\/ul>\n<p>A listener attached to it receives 4 arguements<\/p>\n<ul>\n<li>Event object<\/li>\n<li>mpp.Uploader instance<\/li>\n<li>plupload.Uploader instance<\/li>\n<li>Backbone collection of files.<\/li>\n<\/ul>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"lang:js decode:true\">\t$(document).on('mpp:uploader:upload.complete', function (event, mpp_uploader, uploader, files) {\r\n\t\t\/\/ do something here.\r\n    });<\/pre>\n<h4 id=\"upload-error\">On Error:-<\/h4>\n<p>For all type of error(Possibly a file selection error or upload failure error), MediaPress triggers &#8216;<strong>mpp:uploader:error<\/strong>&#8216; .<\/p>\n<p>The listener attached to this event receives 4 arguements<\/p>\n<ul>\n<li>Event object<\/li>\n<li>mpp.Uploader instance<\/li>\n<li>message string<\/li>\n<li>data object<\/li>\n<li>file object<\/li>\n<\/ul>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"lang:js decode:true\">\t$(document).on('mpp:uploader:error', function (event, mpp_uploader, message, data, file) {\r\n\t\t\/\/ do something here.\r\n    });<\/pre>\n<p>&nbsp;<\/p>\n<h3 id=\"uploader-type\">Uploader Type Check:-<\/h3>\n<p>In all the listeners, you receive the instance of current MediaPress uploader as second argument.\u00a0 You don&#8217;t need to worry about uploader check if you want to do some action on all the uploads irrespective of whether it is being uploaded.<\/p>\n<p>There may be a time when you will want to determine whcih uploader was used(was it a gallery upload or was it happening on activity?) . Before we determine it, You will need to know the kind of mpp.Uploader instances which are used by MediaPress.<\/p>\n<p>In MediaPress, we create multiple instance of the mpp.Uploader for uploading to activity, single gallery, using shortcode, cover upload. Here are the list of the instance object.<\/p>\n<ul>\n<li>mpp.activity_uploader :- our activity uploader.<\/li>\n<li>mpp.guploader :- Gallery uploader<\/li>\n<li>mpp.shortcode_uploader :- Shortcode uploader<\/li>\n<li>mpp.cover_uploader :- cover uploader<\/li>\n<\/ul>\n<p>In order to determine the type of uploader, compare the 2nd arguement with one of the above. For example, if I want to check for upload complete from activity, I will do it like below:-<\/p>\n<pre class=\"lang:default decode:true \">\t$(document).on('mpp:uploader:upload.complete', function (event, mpp_uploader, uploader, files ) {\r\n\t\t\/\/ did it happen from activity uploader?\r\n\t\tif ( mpp_uploader === mpp.activity_uploader ) {\r\n\t\t\tconsole.log(\" All files uploaded from activity\");\r\n\t\t}\r\n    });<\/pre>\n<h3 id=\"further-reading\">Further reading.<\/h3>\n<ul>\n<li>plupload.Uploader<\/li>\n<li><a href=\"http:\/\/api.jquery.com\/trigger\/\">jQuery.trigger<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to MediaPress uploader events life cycle guide. The guide is aimed at developer who want to create their own uploading experience with MediaPress. MediaPress uses plupload.Uploader for uploading files. It is the same library used by WordPress(and bundled with it). We have a Container class mpp.Uploader. The various instances of mpp.Uploader allows us uploading &#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7044,4722],"tags":[],"class_list":["clearfix","post-642","post","type-post","status-publish","format-standard","hentry","category-api-guides","category-uploader"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>MediaPress Uploader Events Lifecycle &#8226; MediaPress | BuddyDev<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/buddydev.com\/mediapress\/api-guides\/mediapress-uploader-events-lifecycle\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MediaPress Uploader Events Lifecycle &#8226; MediaPress | BuddyDev\" \/>\n<meta property=\"og:description\" content=\"Welcome to MediaPress uploader events life cycle guide. The guide is aimed at developer who want to create their own uploading experience with MediaPress. MediaPress uses plupload.Uploader for uploading files. It is the same library used by WordPress(and bundled with it). We have a Container class mpp.Uploader. The various instances of mpp.Uploader allows us uploading ...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/buddydev.com\/mediapress\/api-guides\/mediapress-uploader-events-lifecycle\/\" \/>\n<meta property=\"og:site_name\" content=\"MediaPress | BuddyDev\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/bpdev\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-23T08:36:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-04-23T17:19:35+00:00\" \/>\n<meta name=\"author\" content=\"Brajesh Singh\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@sbrajesh\" \/>\n<meta name=\"twitter:site\" content=\"@buddydev\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Brajesh Singh\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/buddydev.com\/mediapress\/api-guides\/mediapress-uploader-events-lifecycle\/\",\"url\":\"https:\/\/buddydev.com\/mediapress\/api-guides\/mediapress-uploader-events-lifecycle\/\",\"name\":\"MediaPress Uploader Events Lifecycle &#8226; MediaPress | BuddyDev\",\"isPartOf\":{\"@id\":\"https:\/\/buddydev.com\/mediapress\/#website\"},\"datePublished\":\"2018-04-23T08:36:18+00:00\",\"dateModified\":\"2018-04-23T17:19:35+00:00\",\"author\":{\"@id\":\"https:\/\/buddydev.com\/mediapress\/#\/schema\/person\/4ebf5f3e519f8cbddc6583e182dbe4fb\"},\"breadcrumb\":{\"@id\":\"https:\/\/buddydev.com\/mediapress\/api-guides\/mediapress-uploader-events-lifecycle\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/buddydev.com\/mediapress\/api-guides\/mediapress-uploader-events-lifecycle\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/buddydev.com\/mediapress\/api-guides\/mediapress-uploader-events-lifecycle\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/buddydev.com\/mediapress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MediaPress Uploader Events Lifecycle\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/buddydev.com\/mediapress\/#website\",\"url\":\"https:\/\/buddydev.com\/mediapress\/\",\"name\":\"MediaPress | BuddyDev\",\"description\":\"The Best Media Gallery Plugin For BuddyPress\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/buddydev.com\/mediapress\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/buddydev.com\/mediapress\/#\/schema\/person\/4ebf5f3e519f8cbddc6583e182dbe4fb\",\"name\":\"Brajesh Singh\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/buddydev.com\/mediapress\/#\/schema\/person\/image\/\",\"url\":\"\/\/www.gravatar.com\/avatar\/2106622ee90d53d15ac1402806616aca?s=96&#038;r=g&#038;d=identicon\",\"contentUrl\":\"\/\/www.gravatar.com\/avatar\/2106622ee90d53d15ac1402806616aca?s=96&#038;r=g&#038;d=identicon\",\"caption\":\"Brajesh Singh\"},\"sameAs\":[\"http:\/\/buddydev.com\/members\/sbrajesh\/\",\"https:\/\/x.com\/sbrajesh\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MediaPress Uploader Events Lifecycle &#8226; MediaPress | BuddyDev","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/buddydev.com\/mediapress\/api-guides\/mediapress-uploader-events-lifecycle\/","og_locale":"en_US","og_type":"article","og_title":"MediaPress Uploader Events Lifecycle &#8226; MediaPress | BuddyDev","og_description":"Welcome to MediaPress uploader events life cycle guide. The guide is aimed at developer who want to create their own uploading experience with MediaPress. MediaPress uses plupload.Uploader for uploading files. It is the same library used by WordPress(and bundled with it). We have a Container class mpp.Uploader. The various instances of mpp.Uploader allows us uploading ...","og_url":"https:\/\/buddydev.com\/mediapress\/api-guides\/mediapress-uploader-events-lifecycle\/","og_site_name":"MediaPress | BuddyDev","article_publisher":"https:\/\/www.facebook.com\/bpdev","article_published_time":"2018-04-23T08:36:18+00:00","article_modified_time":"2018-04-23T17:19:35+00:00","author":"Brajesh Singh","twitter_card":"summary_large_image","twitter_creator":"@sbrajesh","twitter_site":"@buddydev","twitter_misc":{"Written by":"Brajesh Singh","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/buddydev.com\/mediapress\/api-guides\/mediapress-uploader-events-lifecycle\/","url":"https:\/\/buddydev.com\/mediapress\/api-guides\/mediapress-uploader-events-lifecycle\/","name":"MediaPress Uploader Events Lifecycle &#8226; MediaPress | BuddyDev","isPartOf":{"@id":"https:\/\/buddydev.com\/mediapress\/#website"},"datePublished":"2018-04-23T08:36:18+00:00","dateModified":"2018-04-23T17:19:35+00:00","author":{"@id":"https:\/\/buddydev.com\/mediapress\/#\/schema\/person\/4ebf5f3e519f8cbddc6583e182dbe4fb"},"breadcrumb":{"@id":"https:\/\/buddydev.com\/mediapress\/api-guides\/mediapress-uploader-events-lifecycle\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/buddydev.com\/mediapress\/api-guides\/mediapress-uploader-events-lifecycle\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/buddydev.com\/mediapress\/api-guides\/mediapress-uploader-events-lifecycle\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/buddydev.com\/mediapress\/"},{"@type":"ListItem","position":2,"name":"MediaPress Uploader Events Lifecycle"}]},{"@type":"WebSite","@id":"https:\/\/buddydev.com\/mediapress\/#website","url":"https:\/\/buddydev.com\/mediapress\/","name":"MediaPress | BuddyDev","description":"The Best Media Gallery Plugin For BuddyPress","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/buddydev.com\/mediapress\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/buddydev.com\/mediapress\/#\/schema\/person\/4ebf5f3e519f8cbddc6583e182dbe4fb","name":"Brajesh Singh","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buddydev.com\/mediapress\/#\/schema\/person\/image\/","url":"\/\/www.gravatar.com\/avatar\/2106622ee90d53d15ac1402806616aca?s=96&#038;r=g&#038;d=identicon","contentUrl":"\/\/www.gravatar.com\/avatar\/2106622ee90d53d15ac1402806616aca?s=96&#038;r=g&#038;d=identicon","caption":"Brajesh Singh"},"sameAs":["http:\/\/buddydev.com\/members\/sbrajesh\/","https:\/\/x.com\/sbrajesh"]}]}},"_links":{"self":[{"href":"https:\/\/buddydev.com\/mediapress\/wp-json\/wp\/v2\/posts\/642","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buddydev.com\/mediapress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buddydev.com\/mediapress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buddydev.com\/mediapress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/buddydev.com\/mediapress\/wp-json\/wp\/v2\/comments?post=642"}],"version-history":[{"count":6,"href":"https:\/\/buddydev.com\/mediapress\/wp-json\/wp\/v2\/posts\/642\/revisions"}],"predecessor-version":[{"id":655,"href":"https:\/\/buddydev.com\/mediapress\/wp-json\/wp\/v2\/posts\/642\/revisions\/655"}],"wp:attachment":[{"href":"https:\/\/buddydev.com\/mediapress\/wp-json\/wp\/v2\/media?parent=642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buddydev.com\/mediapress\/wp-json\/wp\/v2\/categories?post=642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buddydev.com\/mediapress\/wp-json\/wp\/v2\/tags?post=642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}