Archive for June, 2012

oliverm

Going Loopy for Closures

Monday, June 18th, 2012

We do a lot of jQuery DOM manipulation in our projects here at New Toronto Group. Often we are required to create applications with UIs that are generated on-the-fly, based on various runtime conditions.

One problem we see often is when we want to attach an event listener on a dynamically-created object, such as  a button, and maintain a reference to that button’s index while it was created.

Sounds simple enough. Most developers would begin by coding something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
app.initBroken = function () {
    var times = 5,
        i;

    for (i = 0; i < times; i += 1) {
        var button = $('<button>I\'m button '
            + i
            + '</button><br/>');

        $('body').append(button);

        button.on('click', function (i) {
            alert(i);
        });
    }
};

So what happens when you click one of the buttons? That’s right – it alerts 5 every time. The alert function uses the i variable as it is when the button is clicked. Because the for loop completed when the button was clicked, i is 5. Dang…

 

So how do we capture the value of i at the time the button was created? This is where closures come in. We can leverage the fact that JavaScript functions each have their own scope. While we’re looping and creating the button, we can create a function which holds this value of i, and then returns it when the button is clicked. Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
app.initFixed = function () {
    var times = 5,
        i;

    for (i = 0; i < times; i += 1) {

        var button = $('<button>I\'m button '
            + i
            + '</button>
'
);

        $('body').append(button);

        button.on('click',
            function (j) {
                return function () {
                    alert(j);
                };
            }(i)
        );
    }
};

On line 15, we’re constructing a new function to return. Notice that we’re sending the function the value of i on line 18 when it is being created. It is this value that the function retains and returns when the button is clicked. (You may need to stare down this code for a few minutes before it sinks in.)

You can go one step further and move the inner function outside the for loop. This technique is illustrated in Douglas Crockford’s JavaScript: The Good Parts which inspired the solution presented here.

 

 

 

edv

Adobe Connect 9 Now Available

Thursday, June 14th, 2012

Adobe® Connect™ 9 delivers critical new capabilities supporting webinars, with an all-in-one solution that enables event managers to deliver compelling, immersive events; maximize attendance; and measure results for optimized outcomes.

ELearning enhancements include SCORM content support, and greater mobile learning capabilities with Adobe Connect Mobile 2.0, which brings nearly all desktop hosting and collaboration capabilities to the mobile device for complete mobile-to-mobile collaboration.

Contact the NTG Software Sales team to learn more!

edv

Appcelerator webinar – 4 Steps to Creating a Mobile Development Strategy – June 13, 2012

Monday, June 4th, 2012

Join New Toronto Group and Mike King, Principal Mobile Strategist for Appcelerator and formerly Gartner’s Research Director for Mobile, as we outline the 4 key steps to creating a successful, cost effective, mobile development strategy.

During this webinar we will also answer some of the most common mobile development questions, such as:

  • How can you reuse your development code when deploying from one device to the next?
  • How do you build mobile apps for multiple devices using your web team?
  • How can you use your existing web software, apps, and information in your mobile apps?

Appcelerator makes Titanium, the leading mobile platform of choice for thousands of companies seizing the mobile opportunity.

Webinar Details:
Date: Wednesday, June 13
Time: 1:00pm – 2:00pm EST
Duration: 1 hour

For more information, contact Andrew Rybak:
Phone: 905.283.0617
Email: andrewr@newyyz.com

Register now >

Full Details >

edv

Connect Adobe Day – Adobe Ottawa Office – June 21, 2012

Monday, June 4th, 2012

Join Adobe and New Toronto Group at the Adobe Ottawa office on Thursday, June 21st for an in-depth look at how government is leveraging new and existing technologies to accelerate program delivery and reduce costs in a way that has no geographic bounds. Whether it’s in training curriculum for a cross-country training initiative, building consensus around a new policy in the North, or leveraging an innovative way to connect with citizens, you will learn how government depends on technology to bridge divides—efficiently, reliably, securely and cost effectively.

At this seminar you will:

  • Learn how the United States Department of Defense uses Adobe Connect to support their mission-critical objectives
  • See how the United Nations is able to broadcast meetings around the world in multiple languages simultaneously
  • Discover how the Government of Canada uses Adobe Connect in policy development
  • See how technology is helping agencies to train the next generation of leadership with real-time, cost-effective learning experiences delivered virtually anywhere, anytime

Agenda

  • 8:00 a.m. – 8:30 a.m. Breakfast and Registration
  • 8:30 a.m. – 8:35 a.m. Welcome and Opening Remarks
  • 8:35 a.m. – 9:30 a.m. Adobe Connect Overview
  • 9:30 a.m. – 10:30 a.m. Customer Success Story: DISA and the U.S. Dept. of Defense
  • 10:30 a.m. – 10:45 a.m. Break
  • 10:45 a.m. – 11:45 a.m. Training and eLearning with Adobe Connect
  • 11:45 a.m. – 12:00 p.m. Q&A and Closing Remarks

Location:
Adobe Systems Canada, Inc.
343 Preston Street
Ottawa, Ontario K1S 1N4

Register now >

Full Details >