Kapil Bhatia

Education,Technology,Ideas,India

  • Home
  • Bio
  • Research

Getting Started with Yahoo YUI 3 – Part 1

Posted by Kapil in September 4th 2008  

YUI 3 is now available as preview release.In this post,I am just sharing some personal notes I took while understanding the very basics of new version of this framework : 

  •      The YUI Global Object 
  1. Download YUI 3 and include yui-min to start working with YUI global  

           <script type="text/javascript" src=../../build/yui/yui-min.js" ></script>

        or, go to YUI Dependency Configurator, and leverage Yahoo’s infrastructure and bandwidth.Also,helps you to select which files you should include for the modules you need.
 

 I am starting with example given in YUI website :

<style>
    #demo {
        height: 100px;
        width: 100px;
        border: 1px solid black;
        background-color: #8DD5E7;
        text-align: center;
    }
</style>

<div id="demo"></div>

<script>

YUI({combine: true, timeout: 10000}).use("node",function(Y) {
var node = Y.get(’#demo’);
Y.log(’Found node.. Setting style’);
node.setStyle(’backgroundColor’, ‘#D00050′);
node.set(’innerHTML’, ‘<strong>Changed!</strong>’);
});
</script>


Explanation :

        Looking at 

            YUI({combine: true, timeout: 10000}).use("node",function(Y) {

  • YUI namespace is used instead of Yahoo namespace as in version 2.
  • combine: true is a boolean parameter which basically utilizes the YUI combo service to join the dependency js files in order to reduce the number of http connections.
  • timeout is number of milliseconds before a timeout occurs when dynamically loading nodes. if not set, there is no timeout
  • Use method allows you to choose the modules that you want loaded into your YUI instance.One cool thing is "You can pick and choose what you need, you don’t have to load everything that was included on the page".
  • We are including node module here.If we want to include all modules available, we can use star(*)(more details in green block )
  • Then,we are passing function as a last argument.This function will execute after YUI instance loads all the modules specified.
  • There is one argument passed in this function which is the YUI instance we’ll be working with.

 
      Now,looking inside the function 

var node = Y.get(’#demo’);
Y.log(’Found node.. Setting style’);
node.setStyle(’backgroundColor’, ‘#D00050′);
node.set(’innerHTML’, ‘<strong>Changed!</strong>’);

  • One way to get a Node instance is using YUI instance’s(Y in our case) get method.
  • If the ‘debug’ global setting is true,then a log message(’Found node…’) will be dispatched through  ‘yui:log’ event.In order to see these messages in browser console,you have to  set ‘useConsole’ config to true.
  • Line 3 in this block,we are just changing the background color of the node using setStyle passing backgroundcolor attribute with its value.
  • And then we are changing the innerHTML using some text(changed)

Click here to see this example in your browser.

Now,I want to change the text inside this box based on some event(say onClick) from "changed" to "somebody clicked me".For that ,add the following code ..


onClick = function(e) {
    var node = e.currentTarget;
    var html = ‘Somebody clicked me !!’;
    node.set(’innerHTML’, html);

node.setStyle(’backgroundColor’, ‘#FFFFFF’);
    };

    node.on(’click’, onClick);
 

  • Let’s start with the last line.The node.on / node.attach method links a DOM event handler( on is just an allias of attach method).The first parameter is the type of DOM event to listen for(click in our case) and second parameter is the handler to call when the event fires.In our case,the handler is onClick.
  • Now,lets look at the function definition.currentTarget is the property of  Event.Facade class.It basically references the node for the element that the event is attached to.
  • After that,set method sets the value of the HTMLelement bound to this node.innerHTML is the attribute and its value is defined in html variable passed.

The output of this code will be something like this when you click on that box(I have changed the block width)

 var helloWorld = function(e) {
        Y.log("helloWorld function firing.", "info", "example");
        alert("Hello World!");
    }

    Y.on("click", helloWorld, "#demo");


  • Here,we are showing alert box in another way..
  • We are using on method of YUI global namespace object.The method takes event type(click),function to execute(helloworld) and element to attach to(#demo).

This covers the basics of YUI global object with hint of working with node and event in the above example. In part 2,I will be covering utilties – animation,drag-drop and cookies etc.

 

  • YUI 3 Home Page – http://developer.yahoo.com/yui/3/
  • YUI 3 API reference – http://developer.yahoo.com/yui/3/api/

 


 

 

P.S. I have been trying to solve daily "real" issues which educators face while integrating technology in their curriculum. Click one of the icons on the right sidebar to join the growing community of educators reading this blog by RSS or by email...
under: Programming, Web 2.0
Digg it Add to del.icio.us Stumble it add to technorati

Related Post

  • 10 questions every software developer should ask himself after every project (September 17th, 2008)
  • An open letter to Google Chrome from a Web Developer (September 2nd, 2008)
  • Four minutes introduction to OAuth (June 29th, 2008)
  • 4 Ways Developers Can Make Money with Amazon S3 (July 5th, 2007)
  • 3 Minutes Introduction to Google Gears (July 1st, 2007)

No Comment Received

Leave A Reply

Please Note: Comments maybe under moderation after you submit your comments so there is no need to resubmit your comment again

« An open letter to Google Chrome from a Web Developer
10 questions every software developer should ask himself after every project »

Subscribe

feedsfeeds

Tag Cloud

  • 100 audacity audio blogging business classroom code snippets collaboration conversion edtech edugames elearning elementary free games google india ISB language learning lectures margin online pedagogy podcasting research resource resources scholars schools sharing skills software special education startup strategy tips tool tools twitter usability value video video games website

Ask Kapil

  • If you have any question related to education technology, ask here. Ask Kapil

About this blog

  • Brief, regular and personal analysis on role of technology in education.

Search

Recent Entries

  • Basic Steps for HR in Small Companies
  • 10 Steps to make money and control the world with Swine Flu
  • Being an Employee of the company that is built to flip
  • Fixed Salary versus Profit Sharing in Time(Fiddle) Based Software Projects
  • Compensation Plan for small rails consulting
  • How to become Twitter Teacher(TT) in 23 steps or less ?
  • Taking it global
  • Parents as Partners Show #21 -
  • Teachers Teaching Teachers – 26 February – Voicethread.com
  • women of web 2.0 – notes of 25 February
  • its elementary – 24 feb 2009 – some personal notes
  • How to teach Information/Research skills in primary schools ?
  • 15 Key learning from ed21online.com

Recent Comments

  • Sean Romero in How to become Twitter Teacher(TT) i…
  • Useful Links (w… in How to become Twitter Teacher(TT) i…
  • Suggested Readi… in How to become Twitter Teacher(TT) i…
  • Twitter in the … in How to become Twitter Teacher(TT) i…
  • Meet My Learnin… in How to become Twitter Teacher(TT) i…
  • Claudia Ceraso in How to become Twitter Teacher(TT) i…
  • links for 2009-… in How to become Twitter Teacher(TT) i…
  • Ralf in How to become Twitter Teacher(TT) i…
  • Rodd Lucier in How to become Twitter Teacher(TT) i…
  • Anonymous in How to become Twitter Teacher(TT) i…

Most Comments

  • Predicting AIEEE Rank and Cutoffs  (15)
  • How to become Twitter Teacher(TT) in 23 steps or less ? (11)
  • 25 Graduate programmes in education technology / E learning / ICT in Education (5)
  • An open letter to Google Chrome from a Web Developer (4)
  • If You’re Not Trying Web Commercials, You’ll Hate Yourself Later (3)
  • 5 Open Source Alternatives to Create Your Own MySpace (3)
  • How Indian Idol Fools India ? (3)
  • 20 Startup Lessons I learnt at Proto.in(Day 1) (3)
  • Considering Mobile as a service delivery platform (2)
  • 5 Extreme Programming(XP) Ideas to Improve Your Personal Relationships (2)
  • 10 questions every software developer should ask himself after every project (2)
  • Teachers Teaching Teachers - 18 Feb - Key learnings (2)
Box-Tube Box Modulize WordPress Theme By Dezzain Studio
©2006-2010 Kapil Bhatia
Powered by WordPress 2.8.4    Valid XHTML    Valid CSS