Passing Data Between Pages in jQuery Mobile

Coming from Flex one of the things I’ve struggled a bit with is passing data between views in my jQuery Mobile applications. The template approach with Mustache worked really well, but it also had a lot of overhead. In fiddling around today I found a more hacky way that seems to work pretty well (though I think the Mustache route is still better). It relies on the fact that the pagebeforeshow method provides a prevPage property as part of its data object. That prevPage property is just a representation of everything in DOM of the previous page. That means it’s relatively easy to use selectors and that object to pass data to the new page.

The setup of my pages is as follows. My first page has a list of breweries, and each brewery page has a list of the beers that brewery uses. When you click on any of the beers, it goes to a form that can be used to rate the beers and provide some extra information about them. My goal was to prepopulate some of those forms with the beer data I already had. For instance I know the beer name, brewery, and style of each beer, so I should be able to prepopulate those, but they change for every beer so it has to be dynamic.

Within each page I defined a hidden div with two span elements with ids that represent the brewery name and brewery location (since they’re the same for every beer). Then within the beer list I have a hidden div that contains the beer name and the beer style. Here’s an example:

<div data-role="page" id="aleasylum" data-theme="e" data-add-back-btn="true">
     <div data-role="header">
          <h1>Ale Asylum</h1>
     </div>
     <div style="display:none">
          <span id="brewernameinfo">Ale Asylum</span>
          <span id="brewerlocationinfo">Madison, WI</span>
     </div>    
     <div data-role="content">    
          <ul data-role="listview">
               <li data-role="list-divider">Year Round Beers</li>
               <li>
                    <a href="#beerdetails">
                         <img src="images/hopalicious-thumb.gif" />
                         <h3>Hopalicious</h3>
                         <p>5.8% abv. Eleven separate additions of cascade hops give this American pale ale its lush citrus aroma and bold hop flavor without crazy bitterness. Hopalicious is available year round in six packs and on tap throughout the Madison and Milwaukee regions.</p>
                         <div style="display:none">
                              <span id="beernameinfo">Hopalicious</span>
                              <span id="beerstyleinfo">IPA</span>
                         </div>
                    </a>
               </li>
               <li>
                    <a href="#beerdetails">
                         <img src="images/madtown-nutbrown-thumb.gif" />
                         <h3>Madtown Nutbrown</h3>
                         <p>5.5% abv. Our nutbrown ale is velvety smooth with a rich caramel aroma. We blend seven different malts for just the right touch of sweetness and a creamy finish youÔøΩll really dig. Madtown Nutbrown is available year round in six packs and on tap throughout the Madison and Milwaukee regions.</p>
                         <div id="info" style="display:none">
                              <span id="beernameinfo">Madtown Nutbrown</span>
                              <span id="beerstyle">IPA</span>
                         </div>                        
                    </a>
               </li>

So in order to grab that, I just added an event listener to the pagebeforeshow method that uses selectors to grab the data. One of the critical parts is that jQuery selectors have an optional context property that can be set so that jQuery only selects from that context. In this case, since my pages are all using the same id names for the values, I need to set the context so that the selectors are only pulling from the previous page and not the entire document.

 $('#beerdetails').on('pagebeforeshow',function(e,data){
     var beername = $('.ui-btn-hover-e #beernameinfo',data.prevPage).text();
     var brewername = $('#brewernameinfo',data.prevPage).text();
     var brewerlocation = $('#brewerlocationinfo',data.prevPage).text();
     var beerstyle = $('.ui-btn-hover-e #beerstyleinfo',data.prevPage).text();
     $('#beername').val(beername);
     $('#brewername').val(brewername);
     $('#brewerlocation').val(brewerlocation);
     $('#beerstyle').val(beerstyle);
 });

The code above executes before anything gets displayed on the new page, grabs the values from the first page with the hidden div tags, and then sets some of the form fields to those new values.

It strikes me as a bit odd that there isn’t an easier way to do this, so it could be that I’m just not googling the correct thing and jQuery Mobile has something built-in to help with this issue. It does seem like this can be done by using URL variables, but what I like about this approach is that it’s a bit more semantic and there’s no required parsing of the URL string.

Slides and Code from 360Flex Denver

360Flex Denver360|Flex Denver was an absolutely fantastic event. I was at the first 360 and have watched the event grow and mature over the years. It’s always been the best Flex conference out there, but there was something about those first few 360′s that had so much energy and got the community excited. This event felt like that. I’d rank it as one of the best 360′s I’ve ever been to. The attendees were enthusiastic and great to talk to, the keynotes were awesome and inspiring, and John and Nicole were at the top of their game as far as hosting went. It might have been all of the mobile stuff, it might have been the announcement on day 2 of Project Spoon, but this 360Flex was just all around great.

So of course, I made a bit of a misstep with my talk. At what was one of the greatest Flex conferences of the past 5 years, I go and give an HTML5/jQuery talk. But hopefully it was still fun. And to be honest, as I dove into HTML5 and jQuery for this talk, I learned that we have it very, very good as Flex developers. There are a lot of great things about HTML5 and especially jQuery, but if you’re building complex RIAs and applications, Flex has solved a ton of problems that the HTML5/JS world hasn’t yet. I definitely think they will, and the community around JS and jQuery is great, but as a Flex developer, you’ve got the best of both worlds and you’re going to be able to jump over to jQuery/HTML5 projects and bring some very valuable knowledge about building complex apps.

Thanks to everyone who attended the talk. My slides can be found here (and embedded below) and you can grab the demos from my GitHub repository here.

Adobe Refresh – Australia, Seoul, Hong Kong and Singapore

I’m really excited to be joining up with the crew for Adobe Refresh, which will cover some of the latest and greatest features for the Flash Platform as well as show off Adobe’s HTML story over a couple of weeks in March. We’re hitting a total of 6 cities in Australia, Hong Kong, Seoul, and Singapore. I’ll be tagging along with Richard Galvan, Paul Burnett, and Michael Stoddart.

The sessions are going to cover Flash Pro, HTML5, Flex/Flash Builder, Digital Publishing, and a Q&A for answering any questions you might have about the Adobe stack. Plus we’ll be showing off some things that most people won’t have seen live yet, so if you come, you’ll get a first crack at seeing some very cool stuff coming up from Adobe.

Here’s the full list of cities:

I’m looking forward to being able to see all of the community members in those countries as well as getting to show off the latest and greatest with the Flash Platform. I’ll be talking about multiscreen development with Flex and Flash Builder and there is some VERY good stuff for developers on the horizon when it comes to building apps across devices. This will be a great event to see it all come together.

360Flex Denver

360Flex Denver360Flex is officially coming to Denver this year! I’m stoked because Denver is the second or third best city in the country. It’s going to be April 10th – 13th. Right now there are some good deals on tickets (if you buy before January 7th) so get them while they’re hot. It’s going to be a fun year for Flex with all of the devices coming to market and with people having had a chance to play with Flex “Hero” and Flash Builder “Burrito” (plus Flash Catalyst “Panini”) so this could be the best 360Flex in a while.

While you’re registering, you might as well think about speaking. They’ve opened the call for papers as well. See you there!

RIA Unleashed and Cigars

If you’re on the East Coast and haven’t registered for RIAUnleashed yet, you should (and it’s super cheap). Last year it was a lot of fun, maybe even the best conference aside from MAX that I went to last year. The speakers were great, the vibe was perfect, and there were a lot of new faces as well as old ones that made the evenings really fun. This year it falls just a couple of weeks after MAX. To me, that’s perfect timing. If you aren’t going to be at MAX, we’ll be showing some of the things we talked about there. If you are going, it’s a chance to come talk about what you saw, and keep the momentum rolling.

And if you’re into cigars, Terry Ryan and I are going to be doing a little cigar get-together probably the night before the conference. It’s going to be a chance to hang out, chill, enjoy a cigar, and probably make fun of Terry. The spot we go to will have limited seating, so if you’re interested in it, drop me an email and I’ll send out invites individually. It’s first come, first serve AND you’ve got to be registered for RIAUnleashed. But I’ll be covering scotch and cigars, so you can consider that a discount off the ticket price.

FITC San Francisco – Come one, Come All

Next week is FITC San Francisco. FITC is one of my favorite Flash conferences of all time and having it in San Francisco means that a bunch of Adobeans are going to be out in force. It also means you get the big guns. Like Kevin Lynch giving the keynote.

It is an incredibly interesting period for Adobe and Flash developers. The word I keep hearing is that we’re in a period of “hyperchange” with the drive for multi-screen and an increasingly app-centric world. But all of this is good for Flash developers. While HTML5 is fantastic for a lot of reasons, it’s also not fully baked yet. It will be, I’m not worried about that, but the hype surrounding it hasn’t caught up with the reality of how hard it is to make the jump to mobile web content. Or create complex applications/content.

So this is a pretty pivotal FITC. And I think a lot of us at Adobe are looking forward to talking with you guys and providing some context to the stories you hear. Adobe is going to be a busy place over the next year and there’s a lot that should excite Flash developers. FITC is a chance to get a taste of that.

So go register!

My 360Flex Schedule

360Flex is right around the corner and based on the topics (and what’s coming on the horizon) I think this is going to be one of the more interesting 360s in a while. I’m speaking on creating Collaborative Applications with Flex and PHP and since I’m the only LiveCycle Collaboration Service talk, I’ll probably do a lot of generic LCCS stuff as part of the talk. But the most exciting part is getting to see what other people do. Here’s a breakdown of where I’m hoping to be for each time slot.

  • Monday 10:00 – 11:20: 300 – Michael Labriola – Apocalypse Soon? the remaking of ‘Flex Components’ – I always like hearing Michael speak and since he knows almost as much as anyone about Flex components, I think this is going to be a great talk.
  • Monday 1:00 – 2:20 – My session.
  • Monday 2:30 – 3:50: 200 – Kevin Hoyt – Flex + Phidgets = Beer – Tough time slot with both Garth Braithwaite and Jesse Warden. I know Kevin has been cooking up some unique demos for this, and with those three catch words, it can’t be a bad session.
  • Monday 4:00 – 5:20: 300 – Yakov Fain – Boring presentation on Flex libraries and modules – I think that Matt Guest’s typography session will be great but I want to spend some time geeking out on modules in Yakov’s session.
  • Tuesday 10:00 – 11:20: 300 – Aaron Pederson, James Polanco – Flex 4 Component LifeCycle Best Practices – I think both Deepa’s and Paul’s sessions are going to be great, but I think the Flex 4 component lifecycle is something worth checking out and James and Aaron always present very well.
  • Tuesday 1:00 – 2:20 – I’m really torn on this one between Jeffrey Houser’s session on Halo versus Spark and Gary Rogers’ automated build and deployment process. I know a lot less about automated building and deployment so I think I may step into that one and see how it goes.
  • Tuesday 2:30 – 3:50 – Elad Elrom – Building Applications using Test Driven Development (TDD) I am really shitty when it comes to test driven development because a lot of what I build are demos, so I want to check this out. But the GPS/Flex session by Randy Troppmann and Andrew Westberg looks really slick.
  • Wednesday 10:00 – 11:20 – by FAR the toughest slot at the conference. Renaun, Jeff Tapper, Dan Florio, and Simeon Bateman all going head to head. I’ll either flip a coin or run back and forth.
  • Wednesday 1:00 – 2:20 – 200 – Nate Beck – Flexible Games… game development with stuff you already know – I’ve seen Nate do a couple of bits on game development so I think this will be a cool and useful session.
  • Wednesday 2:30 – 3:50 – 250 – Andrew Powell – How I Learned to Stop Worrying and Love UX – Andy got really rave reviews at RIAUnleashed and I missed his session. I’m hoping this is more of the same and it’s a good way to end the conference.

It’s going to be a jam packed 3 days. If you haven’t registered yet, you can register with this URL and get a chance to win the next version of Creative Suite. Plus you’ll get a chance to talk to the engineers and product managers, you can check out Flash Player 10.1 on mobile devices (I think we’ll all have demos to show) and generally kick back with fellow Flex developers.