{"id":353,"date":"2009-02-06T02:07:07","date_gmt":"2009-02-06T08:07:07","guid":{"rendered":"http:\/\/bililite.nfshost.com\/blog\/?p=353"},"modified":"2009-02-13T11:24:13","modified_gmt":"2009-02-13T17:24:13","slug":"later-binding-jquery-plugin-methods","status":"publish","type":"post","link":"https:\/\/bililite.com\/blog\/2009\/02\/06\/later-binding-jquery-plugin-methods\/","title":{"rendered":"<code>$.later<\/code>: binding jQuery plugin methods"},"content":{"rendered":"\t<p>I don't like writing <code class=\"language-javascript\">function(){$(this).doStuff()}<\/code> for callbacks and\r\n\twanted some more elegant way of binding objects to functions. I could always use something like Prototype's <a href=\r\n\t\"http:\/\/www.prototypejs.org\/api\/function\/bind\"><code>bind<\/code><\/a>, but I don't want to modify built-in types\r\n\t(because John Resig says not to) and I generally just want to bind plugins to jQuery objects, so it ought to be a\r\n\tplugin. Inspired by <a href=\"http:\/\/test.flesler.com\/jquery.sugar\/\">Ariel Flesler's jquery.sugar<\/a> and <a href=\r\n\t\"http:\/\/ejohn.org\/blog\/ultra-chaining-with-jquery\/\">John Resig's ultrachaining<\/a>.<\/p>\r\n\t<p>So we have <code>$.later<\/code> and <code>$.fn.later<\/code>. <code>$.later()<\/code> returns a chainable function that is bound at\r\n\tcall time to <code class=\"language-javascript\">this<\/code>.\r\n\t<code class=\"language-javascript\">$.later().css('color', 'red').append('&lt;span&gt;more&lt;\/span&gt;')<\/code> is the same as\r\n\t<code class=\"language-javascript\">function() { $(this).css('color', 'red').append('&lt;span&gt;more&lt;\/span&gt;') }<\/code>.\r\n\t<code>$(selector, context).later()<\/code> is similar, but binds to <code>$(selector, context)<\/code> at call time: \r\n\t<code class=\"language-javascript\">$('p').later().css('color', 'red').append('&lt;span&gt;more&lt;\/span&gt;')<\/code> is the same as\r\n\t<code class=\"language-javascript\">function() { $('p').css('color', 'red').append('&lt;span&gt;more&lt;\/span&gt;') }<\/code><\/p>\r\n\t<p><a href=\"\/inc\/jquery.later.js\">Download the code<\/a>.<\/p>\r\n\t<!-- more -->\r\n\t<h4>Examples<\/h4>\r\n\t<pre><code class=\"language-javascript demo\">\r\n\t$('#example1').click($.later().toggleClass('bold'));\r\n\t<\/code><\/pre>\r\n\t<pre><code class=\"language-html demo\">\r\n\t&lt;div id=&quot;example1&quot;&gt;\r\n\t\tExample 1: \r\n\t\t&lt;span&gt;Click Me&lt;\/span&gt;\r\n\t&lt;\/div&gt;\r\n\t<\/code><\/pre>\r\n\t<pre><code class=\"language-javascript demo\">\r\n\t$('#example2').click($('.example:last').later().animate({fontSize: '+=2'}, $('.example:first').later().fadeOut().fadeIn()));\r\n\t<\/code><\/pre>\r\n\t<pre><code class=\"language-html demo\">\r\n\t&lt;div id=&quot;example2&quot;&gt;\r\n\t\tExample 2: \r\n\t\t&lt;span class=\"example\"&gt;Click Me&lt;\/span&gt; | \r\n\t\t&lt;span class=\"example\"&gt;Watch Me&lt;\/span&gt;\r\n\t&lt;\/div&gt;\r\n\t<\/code><\/pre>\r\n\t<script type=\"text\/javascript\" src=\"\/inc\/jquery.later.js\">\r\n\t<\/script>\r\n<script>$('head').append('<'+'style>.bold {font-weight: bold; font-size: 130%}<'+'\/style>')<\/script>\r\n","protected":false},"excerpt":{"rendered":"I don't like writing function(){$(this).doStuff()} for callbacks and wanted some more elegant way of binding objects to functions. I could always use something like Prototype's bind, but I don't want to modify built-in types (because John Resig says not to) and I generally just want to bind plugins to jQuery objects, so it ought to [&hellip;]","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/353"}],"collection":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/comments?post=353"}],"version-history":[{"count":5,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/353\/revisions"}],"predecessor-version":[{"id":399,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/353\/revisions\/399"}],"wp:attachment":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/media?parent=353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/categories?post=353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/tags?post=353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}