11
Building a mobile-friendly web app

Building a mobile-friendly web app using Ember.js and jQuery Mobile

  • Upload
    mistio

  • View
    709

  • Download
    0

Embed Size (px)

DESCRIPTION

Ember.js is an MVC framework for creating powerful web applications. It has some nifty features like auto-updating templates and flexible routing. We use Ember.js to build the frontend of mist.io and because we want our interface to be mobile-friendly, we combine it with jQuery Mobile. This howto assumes you know how to build a basic Ember.js application. If this is the first time you’ve heard of it, you should first check out ember’s excellent startup guide. We will quickly go over the main issues you run into when combining Ember.js and jQuery mobile.

Citation preview

Page 1: Building a mobile-friendly web app using Ember.js and jQuery Mobile

Building a mobile-friendly web app

Page 2: Building a mobile-friendly web app using Ember.js and jQuery Mobile

Why ember.js

● Auto-updating Handlebars templates

● Reusable custom controls means less code

● Powerful and flexible routing

● Scales up without becoming too complex

Page 3: Building a mobile-friendly web app using Ember.js and jQuery Mobile

Why jQuery Mobile

● Great browser and platform compatibility

● Familiar for everyone that has used jQuery

● Ajax-friendly

● Touch-optimized and platform-agnostic UI

widgets

Page 4: Building a mobile-friendly web app using Ember.js and jQuery Mobile

Workflow

● Setup Ember.js http://emberjs.com/guides/

● Define Ember models

● Configure routing

● Add jQM to your main template

Page 5: Building a mobile-friendly web app using Ember.js and jQuery Mobile

Common obstacles

● They both try to handle navigation

● Ember auto-updates can break the mobile-

friendly UI

● Dealing with multi-page ember apps

Page 6: Building a mobile-friendly web app using Ember.js and jQuery Mobile

Disabling jQM navigation

Running this before loading jquery-mobile.js:$(document).bind('mobileinit', function() { $.mobile.ajaxEnabled = false; $.mobile.pushStateEnabled = false; $.mobile.linkBindingEnabled = false; $.mobile.hashListeningEnabled = false;});

will keep jQM from interfering with Ember’s routing system

Page 7: Building a mobile-friendly web app using Ember.js and jQuery Mobile

Handling auto-updates

jQM runs once, but the DOM gets updated constantly by Ember. We must re-render any updated elements:

$(‘#my-element’).trigger(‘create’); // Renders #my-element AND all child elements

or if you want just to update a button:

$(‘#my-element’).button(); // Makes sure #my-element looks like a button

Page 8: Building a mobile-friendly web app using Ember.js and jQuery Mobile

Example: run it for every new widgetCreate a model and controller in JS:// Foo ModelApp.fooModel = Ember.Object.extend({ text: ''});App.fooController = Ember.ArrayController.extend({ content: [], addFoo: function() { var foo = App.fooModel.create({ 'text': 'Foo ' + this.content.length }); this.content.pushObject(foo); }, removeFoo: function() { // Remove last object this.content.removeObject(this.content[this.content.length - 1]); }}).create();

Page 9: Building a mobile-friendly web app using Ember.js and jQuery Mobile

Example: run it for every new widgetRender our content in HTML:<div id=”my-list” data-role=”listview”> {{#each App.fooController.content}} <li> <a data-role=”button”>{{this.text}}</a> </li> {{/each}}</div>

For each foo in fooController, we get a button with the text ‘Foo <number>’

Running:App.fooController.addFoo();App.fooController.addFoo();

will add new, unrendered buttons into the DOM

Page 10: Building a mobile-friendly web app using Ember.js and jQuery Mobile

Example: run it for every new widgetUse an observer to make sure everything shows up nicely:

// It would be more appropriate for this observer to be placed inside// a View, but for the sake of simplicity we wrote it inside fooControllercontentObserver: function() { Ember.run.next(function() { $(‘#my-list’).trigger(‘create’); });}.observes(‘content’)

This will keep the page mobile-friendly when the DOM gets updated.

Page 11: Building a mobile-friendly web app using Ember.js and jQuery Mobile

Thank you!Code fiddle: http://jsfiddle.net/5Scqp/11/

Check how Mist.io uses Ember.js and jQM on github:

https://github.com/mistio/mist.io

More resources:

● http://blog.mist.io/post/78757774060/building-a-mobile-friendly-web-app-using-

ember-js-and

● http://emberjs.com/guides/