{"id":338,"date":"2009-03-29T02:43:58","date_gmt":"2009-03-29T06:43:58","guid":{"rendered":"http:\/\/eligrey.com\/?p=338"},"modified":"2009-03-29T02:43:58","modified_gmt":"2009-03-29T06:43:58","slug":"pmxdr-reusable-instances","status":"publish","type":"post","link":"https:\/\/eligrey.com\/blog\/pmxdr-reusable-instances\/","title":{"rendered":"Reusable pmxdr instances"},"content":{"rendered":"<p>I just released version 0.0.4 of the <a href=\"http:\/\/code.eligrey.com\/pmxdr\/client\/\">pmxdr client code<\/a> to support reusable instances where the same iframe can be used for mutiple pmxdr requests to a domain. It&#8217;s still the same interface, but you need to call <code>pmxdr.request()<\/code> instead of <code>pmxdr()<\/code> to do a normal request. Instances are created with <code>new pmxdr(<em>host<\/em>)<\/code> where <em>host<\/em> is any URI from the website you want to request (pmxdr figures out where the API is located automatically). Then just call the <code>request<\/code> method on the instance once the interface iframe has loaded, which you can find out when instance calls it&#8217;s <code>onload<\/code> method once it&#8217;s loaded if you set it. The request method now also accepts an array of requests. To start loading the instance, you call it&#8217;s <code>init<\/code> method. To remove the interface frame, you call it&#8217;s <code>unload<\/code> method. Another thing added is the ability to completely remove pmxdr using its <code>destruct<\/code> method, which removes all event listeners and deletes the pmxdr variable. This does not delete any still-existing interface frames so don&#8217;t forget to <code>unload<\/code> them when you are done to avoid memory leaks.<\/p>\n<p>Using reusable instances saves much more overhead than repeatedly re-requesting a website&#8217;s pmxdr host api. When I updated the <a href=\"http:\/\/code.eligrey.com\/pmxdr\/demo.html\">demo<\/a> to use a single instance for requesting eligrey.com, it started finishing a multitude of times faster. This is an example of using a reusable instance that uses one interface to make three requests:<\/p>\n<pre lang=\"javascript\">var exampleDotCom = new pmxdr(\"http:\/\/example.com\");\nexampleDotCom.onload = function() {\n  this.request([\n    {\n      uri     : \"\/foo.html\",\n      callback: responseHandlers.foo\n    },{\n      uri     : \"\/bar.html\",\n      callback: responseHandlers.bar\n    },{\n      uri     : \"\/baz.html\",\n      callback: responseHandlers.baz\n    }\n  ]);\n};\nexampleDotCom.init()\n\/\/ after all responseHandlers[x] are called, call exampleDotCom.unload()<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I just released version 0.0.4 of the pmxdr client code to support reusable instances where the same iframe can be used for mutiple pmxdr requests to a domain. It&#8217;s still the same interface, but you need to call pmxdr.request() instead of pmxdr() to do a normal request. Instances are created with new pmxdr(host) where host [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[90,91,93,129],"class_list":["post-338","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-javascript","tag-javascript-libraries","tag-javascript-snippets","tag-pmxdr"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pfpUD-5s","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/posts\/338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/comments?post=338"}],"version-history":[{"count":0,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/posts\/338\/revisions"}],"wp:attachment":[{"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/media?parent=338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/categories?post=338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/tags?post=338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}