<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Andy Li’s Blog - jQuery</title><link href="https://blog.onthewings.net/" rel="alternate"></link><link href="http://feeds.feedburner.com/tag/jquery/feed/atom.xml" rel="self"></link><id>https://blog.onthewings.net/</id><updated>2015-07-02T00:00:00+08:00</updated><entry><title>New jQuery Extern arrived at Haxe Standard Library</title><link href="https://blog.onthewings.net/2015/07/02/new-jquery-extern-arrived-at-haxe-std-lib/" rel="alternate"></link><published>2015-07-02T00:00:00+08:00</published><updated>2015-07-02T00:00:00+08:00</updated><author><name>Andy Li</name></author><id>tag:blog.onthewings.net,2015-07-02:/2015/07/02/new-jquery-extern-arrived-at-haxe-std-lib/</id><summary type="html">&lt;p&gt;&lt;span class="center"&gt;
&lt;img alt="jQuery logo" src="https://blog.onthewings.net/files/2015/jQuery-Logo.png"/&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt; has been &lt;del&gt;one of&lt;/del&gt; &lt;a href="http://trends.builtwith.com/javascript/"&gt;the most popular JavaScript library&lt;/a&gt; in the world. To help existing &lt;span class="caps"&gt;JS&lt;/span&gt; developers switch to Haxe for a modern language with accurate static typing, a complete and up-to-date jQuery extern is necessary.&lt;/p&gt;
&lt;p&gt;As I’ve mentioned in the &lt;a href="https://blog.onthewings.net/2015/06/23/im-now-working-for-the-haxe-foundation/"&gt;previous post&lt;/a&gt;, I’m working on updating …&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;span class="center"&gt;
&lt;img alt="jQuery logo" src="https://blog.onthewings.net/files/2015/jQuery-Logo.png"/&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt; has been &lt;del&gt;one of&lt;/del&gt; &lt;a href="http://trends.builtwith.com/javascript/"&gt;the most popular JavaScript library&lt;/a&gt; in the world. To help existing &lt;span class="caps"&gt;JS&lt;/span&gt; developers switch to Haxe for a modern language with accurate static typing, a complete and up-to-date jQuery extern is necessary.&lt;/p&gt;
&lt;p&gt;As I’ve mentioned in the &lt;a href="https://blog.onthewings.net/2015/06/23/im-now-working-for-the-haxe-foundation/"&gt;previous post&lt;/a&gt;, I’m working on updating the jQuery extern in the Haxe standard library. Today, I’ve just committed the initial work to the Haxe repo. The new extern is generated by the same script that powers &lt;a href="https://github.com/andyli/jQueryExternForHaxe"&gt;jQueryExtern&lt;/a&gt;. The resulting extern matches perfectly with the latest jQuery &lt;span class="caps"&gt;API&lt;/span&gt; (1.11.3 / 2.1.4). And since it is generated, we can update the extern very easily when there is a new jQuery release (I used less than an hour for each of the previous updates to jQueryExtern).&lt;/p&gt;
&lt;p&gt;The differences between the old and the new externs are detailed at &lt;a href="https://github.com/HaxeFoundation/haxe/issues/4377"&gt;Github issue #4377&lt;/a&gt;, where you may report any migration issue or provide any feedback. Go ahead and get a &lt;a href="http://builds.haxe.org"&gt;development build of Haxe&lt;/a&gt; and play with the new extern!&lt;/p&gt;</content><category term="Haxe"></category><category term="jQuery"></category></entry><entry><title>jQueryExtern 2.0.0-alpha.1</title><link href="https://blog.onthewings.net/2013/05/14/jqueryextern-2-0-0-alpha-1/" rel="alternate"></link><published>2013-05-14T11:54:00+08:00</published><updated>2013-05-14T11:54:00+08:00</updated><author><name>Andy Li</name></author><id>tag:blog.onthewings.net,2013-05-14:/2013/05/14/jqueryextern-2-0-0-alpha-1/</id><summary type="html">&lt;p&gt;It took longer than expected, but &lt;a href="https://github.com/andyli/jQueryExternForHaxe"&gt;jQueryExtern for Haxe&lt;/a&gt;
2.0.0-alpha.1 was released to haxelib!&lt;/p&gt;
&lt;p&gt;The cool parts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Supports jQuery version 1.9.1/2.0.0! Note that 2.0.0 has the same
    &lt;span class="caps"&gt;API&lt;/span&gt; of 1.9.1, but removed support of &lt;span class="caps"&gt;IE&lt;/span&gt; 6,7,8 …&lt;/li&gt;&lt;/ul&gt;</summary><content type="html">&lt;p&gt;It took longer than expected, but &lt;a href="https://github.com/andyli/jQueryExternForHaxe"&gt;jQueryExtern for Haxe&lt;/a&gt;
2.0.0-alpha.1 was released to haxelib!&lt;/p&gt;
&lt;p&gt;The cool parts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Supports jQuery version 1.9.1/2.0.0! Note that 2.0.0 has the same
    &lt;span class="caps"&gt;API&lt;/span&gt; of 1.9.1, but removed support of &lt;span class="caps"&gt;IE&lt;/span&gt; 6,7,8.&lt;/li&gt;
&lt;li&gt;jQueryExtern is now generated
    from the official jQuery documentation in &lt;span class="caps"&gt;XML&lt;/span&gt; format. It means it
    will be updated faster, more accurate in the future!&lt;/li&gt;
&lt;li&gt;Fine grained configuration of the extern via… macros! e.g. Select
    specific jQuery support version, switching between the use of &lt;code&gt;$&lt;/code&gt;
    or &lt;code&gt;jQuery&lt;/code&gt; in generated code etc.&lt;/li&gt;
&lt;li&gt;Macro based Plugin extern system! Writing jQuery plugin extern is
    much easier.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can find out the details at the &lt;a href="https://github.com/andyli/jQueryExternForHaxe/wiki/Haxe-3"&gt;Github wiki page&lt;/a&gt;.&lt;/p&gt;</content><category term="Haxe"></category><category term="jQuery"></category></entry><entry><title>Haxe tips: better untyped expression</title><link href="https://blog.onthewings.net/2012/07/08/haxe-tips-better-untyped-expression/" rel="alternate"></link><published>2012-07-08T04:20:00+08:00</published><updated>2012-07-08T04:20:00+08:00</updated><author><name>Andy Li</name></author><id>tag:blog.onthewings.net,2012-07-08:/2012/07/08/haxe-tips-better-untyped-expression/</id><summary type="html">&lt;h3&gt;tl;dr&lt;/h3&gt;
&lt;p&gt;Remember to put the brackets&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;untyped&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#images"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;imageLoader&lt;/span&gt;&lt;span class="p"&gt;)({});&lt;/span&gt;
&lt;span class="c1"&gt;//  ^                                         ^&lt;/span&gt;
&lt;span class="c1"&gt;//  |________these____________________________|&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Abstract&lt;/h3&gt;
&lt;p&gt;Static type system of Haxe helps you a lot by performing type-checking
in the compilation phase. You will be notified errors before the
application is run.&lt;/p&gt;
&lt;p&gt;Since some Haxe targets (e.g.. &lt;span class="caps"&gt;JS&lt;/span&gt; and &lt;span class="caps"&gt;PHP&lt;/span&gt;) are …&lt;/p&gt;</summary><content type="html">&lt;h3&gt;tl;dr&lt;/h3&gt;
&lt;p&gt;Remember to put the brackets&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;untyped&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#images"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;imageLoader&lt;/span&gt;&lt;span class="p"&gt;)({});&lt;/span&gt;
&lt;span class="c1"&gt;//  ^                                         ^&lt;/span&gt;
&lt;span class="c1"&gt;//  |________these____________________________|&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Abstract&lt;/h3&gt;
&lt;p&gt;Static type system of Haxe helps you a lot by performing type-checking
in the compilation phase. You will be notified errors before the
application is run.&lt;/p&gt;
&lt;p&gt;Since some Haxe targets (e.g.. &lt;span class="caps"&gt;JS&lt;/span&gt; and &lt;span class="caps"&gt;PHP&lt;/span&gt;) are dynamic in nature, many of
the &lt;code&gt;native&lt;/code&gt; libraries/frameworks are also very dynamic. You have to use
the &lt;code&gt;untyped&lt;/code&gt; keyword in Haxe very often to workaround the compiler type-checking.&lt;/p&gt;
&lt;p&gt;As a best-practice, to avoid unnecessary suppression of type-checking,
brackets should always be placed in order to constrain affecting slope
when using the &lt;code&gt;untyped&lt;/code&gt; keyword in an expression.&lt;/p&gt;
&lt;h3&gt;Example: using jQuery plug-in&lt;/h3&gt;
&lt;p&gt;For illustration, I will explain a frequently faced problem that is how
to use jQuery plug-in in Haxe (following will use &lt;a href="https://github.com/andyli/jQueryExternForHaxe"&gt;jQueryExtern&lt;/a&gt;). In
case there is a plug-in that will add a method, &lt;code&gt;.imageLoader(config)&lt;/code&gt;,
to jQuery. But since there is no such method declared in the JQuery
extern class, if you simply compile &lt;code&gt;new JQuery("#images").imageLoader({});&lt;/code&gt;, 
it will certainly fail with error “&lt;strong&gt;jQuery.JQuery has no field imageLoader&lt;/strong&gt;“.&lt;/p&gt;
&lt;p&gt;You have two options:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create a plugin class and then “using” it.&lt;/li&gt;
&lt;li&gt;Use a quick and dirty &lt;code&gt;untyped&lt;/code&gt; keyword.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For 1, you can refer to some &lt;a href="https://github.com/andyli/jQueryExternForHaxe/tree/master/jQuery/plugins"&gt;experimental plug-in externs from
jQueryExtern&lt;/a&gt;. Read my &lt;a href="https://blog.onthewings.net/2010/08/03/using-jquery-in-haxe/"&gt;blog post&lt;/a&gt; on how to use it with ”using”.
This method takes some more amount of coding and does not work well with
optional arguments.&lt;/p&gt;
&lt;p&gt;For 2, adding a &lt;code&gt;untyped&lt;/code&gt; keyword will shut the complier’s mouth up:
&lt;code&gt;untyped new JQuery("#images").imageLoader({});&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The above of course works, but you will lose many type-checking in the
expression where are unnecessarily suppressed. e.g.. if you forgot to
define &lt;code&gt;config&lt;/code&gt; in&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;untyped&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#images"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;imageLoader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;…it will compile and fails at runtime with “&lt;strong&gt;Uncaught ReferenceError:
config is not defined&lt;/strong&gt;“.&lt;/p&gt;
&lt;p&gt;Instead, constrain the &lt;code&gt;untyped&lt;/code&gt; scope by placing a pair of brackets:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;untyped&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#images"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;imageLoader&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;…it now (correctly) fails to be compiled with error “&lt;strong&gt;Unknown
identifier : config&lt;/strong&gt;“.&lt;/p&gt;
&lt;p&gt;It is useful because sometimes you may code like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;untyped&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#images"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;imageLoader&lt;/span&gt;&lt;span class="p"&gt;)({&lt;/span&gt;
    &lt;span class="n"&gt;images&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;images&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;async&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;progressbar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;progressbar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"value"&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;loaded&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numOfFrames&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;allcomplete&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
            &lt;span class="kd"&gt;var&lt;/span&gt; playback &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#playback"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"playback-"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;Std&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;appendTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hide&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;displayMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;enumEq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PlaybackMode&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                &lt;span class="n"&gt;imageLoop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#toggle-play-btn"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;removeAttr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"disabled"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#toggle-slow-btn"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;removeAttr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"disabled"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#main"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;removeClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"loading"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you do not place brackets around &lt;code&gt;untyped&lt;/code&gt; (at line 1 as shown
above), you will unnecessarily lose type-checking for over 90% of codes
(begin with line 2 of above), which is pretty bad.&lt;/p&gt;</content><category term="Haxe"></category><category term="jQuery"></category><category term="trick"></category></entry><entry><title>haXe jQueryExtern update: align with 1.6.1, ‘jQuery’ package…</title><link href="https://blog.onthewings.net/2011/06/12/haxe-jqueryextern-update-align-with-1-6-1-jquery-package/" rel="alternate"></link><published>2011-06-12T00:00:00+08:00</published><updated>2011-06-12T00:00:00+08:00</updated><author><name>Andy Li</name></author><id>tag:blog.onthewings.net,2011-06-12:/2011/06/12/haxe-jqueryextern-update-align-with-1-6-1-jquery-package/</id><summary type="html">&lt;p&gt;I’ve recently updated &lt;a href="http://lib.haxe.org/p/jQueryExtern"&gt;jQueryExtern&lt;/a&gt; to align with &lt;a href="http://api.jquery.com/category/version/1.6/"&gt;jQuery 1.6.1&lt;/a&gt;.
To get it, use the usual &lt;code&gt;haxelib upgrade&lt;/code&gt; (or &lt;code&gt;haxelib install
jQueryExtern&lt;/code&gt; if you have not installed it previously).&lt;/p&gt;
&lt;p&gt;I introduced some changes that would like to explain here:&lt;/p&gt;
&lt;h3&gt;package &lt;code&gt;jQuery&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Finally jQueryExtern has its own package, &lt;code&gt;jQuery …&lt;/code&gt;&lt;/p&gt;</summary><content type="html">&lt;p&gt;I’ve recently updated &lt;a href="http://lib.haxe.org/p/jQueryExtern"&gt;jQueryExtern&lt;/a&gt; to align with &lt;a href="http://api.jquery.com/category/version/1.6/"&gt;jQuery 1.6.1&lt;/a&gt;.
To get it, use the usual &lt;code&gt;haxelib upgrade&lt;/code&gt; (or &lt;code&gt;haxelib install
jQueryExtern&lt;/code&gt; if you have not installed it previously).&lt;/p&gt;
&lt;p&gt;I introduced some changes that would like to explain here:&lt;/p&gt;
&lt;h3&gt;package &lt;code&gt;jQuery&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Finally jQueryExtern has its own package, &lt;code&gt;jQuery&lt;/code&gt;. The previous
&lt;code&gt;jQueryPlugins&lt;/code&gt; package is also moved to &lt;code&gt;jQuery.plugins&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;It is done because jQuery has more and more types, e.g.. &lt;a href="http://api.jquery.com/Types/#jqXHR"&gt;jqXHR&lt;/a&gt;,
&lt;a href="http://api.jquery.com/category/deferred-object/"&gt;Deferred&lt;/a&gt; and &lt;a href="http://api.jquery.com/Types/#Promise"&gt;Promise&lt;/a&gt; are introduced in jQuery 1.5. In the
previous versions of jQueryExtern, most of them are prefixed by &lt;code&gt;JQuery&lt;/code&gt;
(&lt;code&gt;JQueryPromise&lt;/code&gt;, &lt;code&gt;JQueryDeferred&lt;/code&gt;, &lt;code&gt;JQueryEvent&lt;/code&gt;) which is lengthy
and redundant, with &lt;code&gt;jQuery&lt;/code&gt; package, it is now safe to remove the
prefix. In case of name collision, simply use the fully qualified names,
e.g. &lt;code&gt;jQuery.Event&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To conclude, you should now &lt;code&gt;include jQuery.JQuery;&lt;/code&gt; instead of &lt;code&gt;include
JQuery;&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;JQueryStatic&lt;/code&gt; and &lt;code&gt;_static&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;To solve the problem of haXe disallowing the use of same name for static
and non-static members, previously I grouped the static methods into
a separate class &lt;code&gt;JQueryS&lt;/code&gt;. It is now renamed as &lt;code&gt;JQueryStatic&lt;/code&gt;, which
is more meaningful.&lt;/p&gt;
&lt;p&gt;I’ve also added a &lt;code&gt;_static&lt;/code&gt; static property to &lt;code&gt;JQuery&lt;/code&gt;. It is for the
people who don’t read documentation, when they type &lt;code&gt;JQuery.&lt;/code&gt;, there is
still a code completion &lt;code&gt;_static&lt;/code&gt; for them to retrieve the
&lt;code&gt;JQueryStatic&lt;/code&gt; class.&lt;/p&gt;
&lt;p&gt;Whether to use &lt;code&gt;JQuery._static&lt;/code&gt; or &lt;code&gt;JQueryStatic&lt;/code&gt; is up to you.&lt;/p&gt;
&lt;h3&gt;Backward compatibility and &lt;code&gt;JQUERY_NO_DEPRECATED&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Don’t worry on having to change all your production codes to match the
changes mentioned above. I’ve included a top-level “JQuery.hx” for
maintaining backward compatibility. A number of typedef are placed there
to solve the old naming.&lt;/p&gt;
&lt;p&gt;However, the old naming are deprecated and the top-level
“JQuery.hx” will be removed a few versions later. You should try to
update your code and test with &lt;code&gt;-D JQUERY_NO_DEPRECATED&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The typedef trick have not been made for the plug-ins, so if you’re
using any of the plug-ins, you have to make the changes when updating to
jQueryExtern 1.6.1.&lt;/p&gt;</content><category term="Haxe"></category><category term="jQuery"></category></entry><entry><title>Using jQuery in haXe</title><link href="https://blog.onthewings.net/2010/08/03/using-jquery-in-haxe/" rel="alternate"></link><published>2010-08-03T23:41:00+08:00</published><updated>2010-08-03T23:41:00+08:00</updated><author><name>Andy Li</name></author><id>tag:blog.onthewings.net,2010-08-03:/2010/08/03/using-jquery-in-haxe/</id><summary type="html">&lt;p&gt;It is a kind of pain to code in &lt;span class="caps"&gt;JS&lt;/span&gt; without a proper library like
&lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;. Using plain haXe in &lt;span class="caps"&gt;JS&lt;/span&gt; target is just as pain since haXe
does not abstract most of the browser quirks, what it gives is
only stricter typing and a little bit better core &lt;span class="caps"&gt;API …&lt;/span&gt;&lt;/p&gt;</summary><content type="html">&lt;p&gt;It is a kind of pain to code in &lt;span class="caps"&gt;JS&lt;/span&gt; without a proper library like
&lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;. Using plain haXe in &lt;span class="caps"&gt;JS&lt;/span&gt; target is just as pain since haXe
does not abstract most of the browser quirks, what it gives is
only stricter typing and a little bit better core &lt;span class="caps"&gt;API&lt;/span&gt; (nothing &lt;span class="caps"&gt;DOM&lt;/span&gt;
related…). When there is no solid haXe/&lt;span class="caps"&gt;JS&lt;/span&gt; targeted haxe library come
out yet, we have to find some ways to use external &lt;span class="caps"&gt;JS&lt;/span&gt; libraries.&lt;/p&gt;
&lt;p&gt;There is a &lt;a href="http://lib.haxe.org/p/jquery"&gt;jQuery wrapper&lt;/a&gt; appeared on haxelib last year, but I don’t
really like its &lt;span class="caps"&gt;API&lt;/span&gt;, which is too different from the original one, and
I’ve never used to having method names first letter capitalized… &lt;span class="caps"&gt;OK&lt;/span&gt;,
it’s just personal taste :P Anyway I’ve written a &lt;a href="http://lib.haxe.org/p/jQueryExtern"&gt;jQuery extern&lt;/a&gt; to
replace it, which provides &lt;span class="caps"&gt;API&lt;/span&gt; closer to jQuery and smaller compiled &lt;span class="caps"&gt;JS&lt;/span&gt;
file (smaller because the haXe compiler does not generate codes for
extern class).&lt;/p&gt;
&lt;p&gt;Here I will illustrate a bit on how to use it, requiring you to have
some experience on both haXe and jQuery.&lt;/p&gt;
&lt;h3&gt;Installing the jQuery extern&lt;/h3&gt;
&lt;p&gt;Since its put on haxelib, you can install it using the command:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;haxelib install jQueryExtern
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;and then add &lt;code&gt;-lib jQueryExtern&lt;/code&gt; in the hxml.&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;OR&lt;/span&gt;, you can download the cutting edge version from Github:
&lt;a href="http://github.com/andyli/jQueryExternForHaxe"&gt;http://github.com/andyli/jQueryExternForHaxe&lt;/a&gt;, extract and place
“JQuery.hx” (optionally with the “jQueryPlugins” folder) into your
project source directory.&lt;/p&gt;
&lt;h3&gt;Using the jQuery classes&lt;/h3&gt;
&lt;p&gt;It should be better to illustrate with example. For a typical &lt;span class="caps"&gt;JS&lt;/span&gt;
starting point with jQuery, you write:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="c1"&gt;//do your magic&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you don’t know, it’s a short-hand that bind your magic codes to the
document ready event, same as you write:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;ready&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="c1"&gt;//do your magic&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;HaXe does not allow using &lt;code&gt;$&lt;/code&gt; as a class name or a function name, but
&lt;code&gt;$&lt;/code&gt; is just a short-hand to &lt;code&gt;jQuery&lt;/code&gt;. However, haXe requires all class
names start with capital letter, so it is &lt;code&gt;JQuery&lt;/code&gt; not &lt;code&gt;jQuery&lt;/code&gt;. You
start your haXe/&lt;span class="caps"&gt;JS&lt;/span&gt; codes using the jQuery extern as following:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;JQuery&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="n"&gt;Main&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;&lt;span class="n"&gt;Void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;&lt;span class="n"&gt;Void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;//your magic codes&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It seems to be a few lines more than &lt;span class="caps"&gt;JS&lt;/span&gt;, but you should already know,
the extra lines are the same for all other haXe targets, its how haXe
program starts. The main point is &lt;code&gt;new JQuery(...)&lt;/code&gt; which is the same as
&lt;code&gt;$(...)&lt;/code&gt; in &lt;span class="caps"&gt;JS&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;So, when you code in &lt;span class="caps"&gt;JS&lt;/span&gt; like:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#myMightyDiv"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;hide&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;now you do the same in haXe:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#myMightyDiv"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;hide&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Simple.&lt;/p&gt;
&lt;h3&gt;Static functions&lt;/h3&gt;
&lt;p&gt;One thing to aware, that is the static methods of jQuery are placed to
&lt;code&gt;JQueryS&lt;/code&gt; in the extern. The reason is haXe does not allow using same
name for both static and instance methods. For example in the original
jQuery, there is &lt;code&gt;$.data()&lt;/code&gt; and &lt;code&gt;$(...).data()&lt;/code&gt;, they are now in the
extern as &lt;code&gt;JQueryS.data()&lt;/code&gt; and &lt;code&gt;new JQuery(...).data()&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;Extra methods in the jQuery extern&lt;/h3&gt;
&lt;p&gt;And there are some extra methods appears in the extern comparing to the
original jQuery. For example, there is &lt;code&gt;cssSet()&lt;/code&gt;, and it is just the
same as &lt;code&gt;css()&lt;/code&gt; but limiting you to really setting a &lt;span class="caps"&gt;CSS&lt;/span&gt; property and it
is properly typed as returning a JQuery object, so you can happily
chaining the methods with code completion and type checking. All the
extra methods are like that and they are all set as &lt;code&gt;inline&lt;/code&gt;, so that
the haXe compiler will generate correct codes, that is &lt;code&gt;css()&lt;/code&gt; not
&lt;code&gt;cssSet()&lt;/code&gt; in the compiled &lt;span class="caps"&gt;JS&lt;/span&gt;, because &lt;code&gt;cssSet()&lt;/code&gt; is not really existing
in jQuery. And because they are not really existing, don’t try to call
the extra methods with Reflect.&lt;/p&gt;
&lt;h3&gt;Using plug-ins&lt;/h3&gt;
&lt;p&gt;I’ve included some jQuery plug-in externs. Currently there are &lt;a href="http://flowplayer.org/tools/"&gt;jQuery
Tools&lt;/a&gt;, &lt;a href="http://jquery.malsup.com/media/"&gt;jQuery media plug-in&lt;/a&gt; and &lt;a href="http://jqueryui.com/"&gt;jQuery &lt;span class="caps"&gt;UI&lt;/span&gt;&lt;/a&gt; (work-in-progress).&lt;/p&gt;
&lt;p&gt;The plug-in architecture is a bit hard to deal with… To understand how
it works, first of all you have to know there is a mighty “&lt;a href="http://scwn.net/2009/05/23/injecting-methods-into-haxe-classes-with-using/"&gt;using&lt;/a&gt;”
keyword in haXe, which inject methods into object instance. And the
jQuery plug-in in my haXe extern is utilizing both “using” and “inline”.&lt;/p&gt;
&lt;p&gt;Anyway, here is how you use the plug-ins. For example, when using
&lt;a href="http://flowplayer.org/tools/"&gt;jQuery Tools&lt;/a&gt; in &lt;span class="caps"&gt;JS&lt;/span&gt;, you just include its &lt;span class="caps"&gt;JS&lt;/span&gt; file in &lt;span class="caps"&gt;HTML&lt;/span&gt; and you can
call the plug-in’s method on the jQuery object, that is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;".my_overlay_trigger"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;overlay&lt;/span&gt;&lt;span class="p"&gt;({...});&lt;/span&gt; &lt;span class="c1"&gt;//overlay is from jQuery Tools&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In haXe, with my extern, you have to write:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;JQuery&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;//there are several classes in Overlay.hx, we "using" only the "Overlay" class.&lt;/span&gt;
&lt;span class="kn"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;jQueryPlugins&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;jQueryTools&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;Overlay&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;Overlay&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/* ... */&lt;/span&gt;

&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;".my_overlay_trigger"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;overlay&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="cm"&gt;/* ... */&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="cm"&gt;/* ... */&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Because this implementation doesn’t deal with optional parameter very
well, in most of the cases I made the optional parameters mandatory. If
the method has a optional config parameter, but you just want to use the
defaults, simply pass in &lt;code&gt;{}&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;And there are many cases some functions does not fit that well, I’ve
wrap them into classes/methods with &lt;code&gt;inline&lt;/code&gt;. You may have to look at
the source file to fully understand how to use.&lt;/p&gt;
&lt;p&gt;There will be more plug-ins to come when I need them. Feel free to
integrate other plug-ins and let me put them into the extern.&lt;/p&gt;</content><category term="Haxe"></category><category term="JavaScript"></category><category term="jQuery"></category></entry></feed>