Thursday, January 26, 2012

Twitter bootstrap popover doesn't work with click event

We were playing with the Twitter bootstrap javascript library lately,  and found out the popover doesn't play well with 'click' event in Chrome (FF works though).  I tried to find solution on the web, but didn't find what we want or the solution doesn't work. Hopefully this post might provide a solution that can help others.

The way we get around this is to wrap the popover call inside a callback which listen to click.
        // ... probably in your initialize function
        $('my-selector').bind('click', this.showPopover);

        // ... somewhere inside an object
        showPopover: function(e){
            var $popTrigger = $(e.target),
                $closeBtn = $('.popover .close-btn');

            $closeBtn.trigger("click");

            (function(that){
                $popTrigger.popover({
                    html:true,
                    placement:'below',
                    offset:8,
                    trigger:'manual',
                    title:function () {
                        return "Well, title!"
                    },
                    content:function () {
                        return that.getContent();
                    }
                });
            })(this);

            $popTrigger.popover("show");

            $closeBtn.live('click', function(e){
                $popTrigger.popover("hide");
                e.preventDefault();
            });

            e.preventDefault();
        }


You can enhance the code by checking if the elements already have desired events attached, so you don't need to trigger that twice.  And if you are designing a one page app, you may need to add function to  hide/remove the popover when the page context is changing.

Wednesday, January 4, 2012

Another good javascript interview question

I was debugging some Node.js code recently and found a bug that is so obvious but very easy to get that  to creep in if you are not careful enough. The following code is just a sample, not reflecting the real code that I'm working on.

The buggy version
Do you see the problem??  That could be one of the interview questions for my next candidate.  I can think of  three different ways to solve this scope issue, two of them are pretty standard, the other one is more elegant :)  Can you think of at least one?

(function() {
//...
for ( key in MyModules){
    if(MyModules.hasOwnProperty(key)){
        module = MyModules[key];
        // if you're not coming from Node.js, the code 
        // inside will get execute during runtime
        app.get(module.regEx ,function(req,res,next){
            module.router(req,res,next,module.collection); 
        });
    }
}
//...
}());

As a javascript developer, the basic minimal concepts we should know before going into an interview would be "prototype, scope and closure".  Because from those concepts, one can generate different patterns out from them.


Thursday, December 8, 2011

Useful HTML5 Resources

Collecting links for HTML5 resources, let me know if you found other good links as well. Enjoy!

W3Schools
http://www.w3schools.com/html5/default.asp

W3Fool  ( An anti-W3Schools campaign )
http://w3fools.com/

Dive Into HTML5
http://diveintohtml5.org/

HTML5 Glossary

HTML5 - edition for web developers

Sites using HTML5 markup
http://www.html5arena.com

Accessibility
http://www.html5accessibility.com/

The Expressive Web

HTML5 Canvas Tutorials
http://www.html5canvastutorials.com

Can I use

HTML5 Test / Browsers Compatibility
http://www.html5test.com

HTML5 Security
http://html5sec.org/

How to order you head section

Juude Blog about HTML5
http://juude.info/html5-layout.php

Best CSS3 Resources
http://www.linkedin.com/news?viewArticle=&articleID=784509223&gid=2071438&type=member&item=71538466&articleURL=http%3A%2F%2Fstylishwebdesigner%2Ecom%2F150-excellent-css3-tutorials-to-make-you-a-stylish-web-designer%2F&urlhash=U0xc&goback=%2Egde_2071438_member_71538466

Efficiently Rendering CSS

Best CSS3 Utilities
http://iroylabs.blogspot.com/2011/09/best-css3-utilities.html

10 Tools to simplify HTML5
http://www.catswhocode.com/blog/10-online-tools-to-simplify-html5-coding

Make old IE to understand HTML5 
http://code.google.com/p/html5shiv/

HTML5 Polyfills
https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills

Wednesday, November 9, 2011

TSG HTML5 Class Part 3

Today's our talk is focusing on CSS3, and here is the slide we have used in the class.



Readings
For those of you who wants a brush up on your css, you can take a look of this site -  http://www.htmldog.com/guides/cssbeginner/ , they have beginner, intermediate and advance level topics on html and css.

Samples
Here are some samples we've used in the class, feel free to change anything you want

Note
Some people asked how come in chrome and firefox the animation is reversed?
That's because of the vendor prefix, I hope you all remembered.  You can make different browser use different css3 by using the vendor prefix.
  • -moz-    (firefox)
  • -webkit- (chrome)
  • -o- (opera)
  • ... etc
We have introduced another tool yesterday, it is http://jsfiddle.net/ , it is similar to cssdesk, but it will let you do javascript also and it is a good tool for developer to write testing code and play around with stuff.

There is a good link Edward has mentioned, take your time to take a brief look even though it's a little bit lengthy - http://html5center.sourceforge.net/building-apps-in-html5

Next Tuesday is our field trip to Linkedin, no class next week.
How to Make Your JavaScript Fast ( Linkedin Campus )
http://www.meetup.com/SF-Web-Performance-Group/events/37966322/


Homework! Homework!
Since we have quite a long time in between until the next class, and thus I have changed my mind and assign you some homework.

Your homework is - without using the cssdesk, try to create a simple page from scratch using HTML and CSS.  You can do whatever you want with only one limitation, your css must be less than 150 lines of code.

Feel free to ask any questions in the email thread or through the blog. Thanks and enjoy :)

One handsome guy

Thanks Pojen for sending me those pictures, I think I look handsome in there :)





Tuesday, November 1, 2011

TSG HTML5 Class Part 2

Today's our talk is focusing on web forms. Hopefully it is not too hard for you all. Please feel free to send me email or leave comments in the blog for any questions you have.

Today's Presentation
http://www.standardista.com/forms/
...

The following is the Linkedin talk I mentioned in the class, you need to signup first if you plan to go.  But it is optional, only go if you are interested, it is a field trip for our class.

How to Make Your JavaScript Fast ( Linkedin Campus )
http://www.meetup.com/SF-Web-Performance-Group/events/37966322/
...
Q&A
Someone asked in the class
"How do I know which browser support what?"

Ok, now below are some cool tools for your reference

Also, someone asked this in the class
"Can we use the HTML5 form now? But not all the browser supports it?"

Well, yes, you can use it now.  For those browser that doesn't support the new form attribute will fall back to "text", and you will need extra javascript to do the validation and extra css for styling.  Meanwhile, there are already written tools to make HTML5 form cross browser and so you don't need to recreate the wheel again - http://code.google.com/p/webforms2/
...

Reading for this week
http://www.catswhocode.com/blog/enhance-your-web-forms-with-new-html5features

...

Homework
Create as many web forms as you would like and test different features.   ( Note: you don't have to use cssdesk, that's just a handy tool only. )

For your homework, here is a good example for you to take a look - http://www.useragentman.com/tests/html5Widgets/bradshawenterprises.com/index.html

And if you would like to explore more about how people do HTML5 in the industry, you can this tool to generate your own HTML5 template, just trial and error and play around with it - http://switchtohtml5.com/

See you all next week in the class.