#

Navigation Search

- Accordion
- AJAX Navigation
- Autocomplete
- Buttons
- Checkboxes
- Collapsibles
- Controlgroup
- Dialogs
- Fixed toolbars
- Flip switch toggle
- Footer toolbar
- Form elements
- Grids
- Header toolbar
- Icon
- Links
- Listviews
- Loader overlay
- Navbar
- Navbar, persistent
- Pages
- Panels New
- Popup
- Radio buttons
- Select
- Slider, single
- Slider, dual range New
- Table, column toggle New
- Table, reflow New
- Text inputs & textarea
- Transitions
- Collapsibles
- Dynamic collapsible
- Controlgroups
- Dynamic controlgroup
- Listviews
- Grid Listview
- Autodividers Linkbar
- Autodividers Selector
- Navigation
- Backbone and Require.js example
- Redirection with server-side support
- Panels
- Panel styling
- Open panel on swipe
- Popups
- Dynamic popup
- Popup with arrow
- Responsive Tables
- Reflow: Custom styles
- Reflow: Heading groups
- Column toggle: Customization options
- Column toggle: Heading groups
- Column toggle demo: Phone comparison
- Sliders
- Tooltip extension
- Swipe
- Swipe to navigate
- Swipe to delete list item
- Forms
- Why won't my CSS styles apply correctly to a widget?
- Updating the value of enhanced form elements does not work.
- My HTML 5 inputs look different across devices and browsers.
- My range or search inputs are being change to number/text.
- Sometimes my custom select shows as a popup other times its a dialog.
- Disabling a button does not work.
- Controls in my fixed header or footer are not responding or behave erratically.
- Pages
- Why aren't my scripts and styles loading?
- Why isn't DOM ready working for my scripts?
- How do I load a page?
- Why is only the first page of my multi page document loaded?
- I'm trying to pass query params to a page but it's not working?
- I'm trying to pass information via the hash but it's not working?
- I'm trying to use the application cache but it's not working?
- Content injected into a page is not enhanced.
- Triggering create on injected HTML does not work.
- How can I stop JQM from auto-enhancing an element?
- How can I stop JQM from enhancing a block of elements?
- How do I control page titles?
- How do I load a page?
- Events
- How do I use touch and mouse events?
- Apps
- How do I need to configure PhoneGap/Cordova?

# jQuery Mobile, Backbone.js and Require.js

jQuery Mobile provides an HTML5-based user interface for all popular mobile device platforms, but it does not influence how you organize and structure your app's JavaScript. Many jQuery Mobile users turn to a variety of other popular third-party libraries, including MV* frameworks and dependency management tools, to help structure their code.

## Backbone and Require.js

Backbone.js and Require.js are two of the most popular third-party libraries that are used with jQuery Mobile to provide a rich JavaScript tech stack for developers.

Backbone.js is a great client-side MV* JavaScript framework that provides structure to JavaScript applications by providing View, Model, Collection, Router, and Event class objects.

Require.js serves a few different purposes than Backbone.js. Require.js is an AMD (Asynchronous Module Definition) script loader that asynchronously loads your JavaScript to improve page load performance, while also helping with script dependency managagement and allowing you to organize your JavaScript into self contained modules (files).

Although there is a high amount of developer interest with using jQuery Mobile, Backbone.js, and Require.js together, there is a high barrier of entry. Many users are confused about how to use the Backbone.js Router class object with the jQuery Mobile routing system.

View example page

## jQuery Mobile configuration

The technique used in this example page is by no means the only technique available, but it is one of the most elegant. The Backbone.js router is used exclusively to handle all hashchange events, and the jQuery Mobile `$.mobile.changePage()` method is used to programmatically change the page.

Below are two internal jQuery Mobile properties that are turned off to allow this to happen:

- `$.mobile.linkBindingEnabled`
  - jQuery Mobile will automatically bind the clicks on anchor tags in your document. Setting this option to false will prevent all anchor click handling including the addition of active button state and alternate link blurring. This should only be used when attempting to delegate the click management to another library or custom code.
- `$.mobile.hashListeningEnabled`
  - jQuery Mobile will automatically listen and handle changes to the location.hash. Disabling this will prevent jQuery Mobile from handling hash changes, which allows you to handle them yourself or use simple deep-links within a document that scroll to a particular id.

## Example page code

To illustrate how the above internal jQuery Mobile properties are turned off, let's examine our example page code.

Inside of the head section of our index.html page, we first include the Require.js JavaScript library and set the data-main attribute of our script tag to the JavaScript file that we want Require.js to include on the page first (this file will contain all of our Require.js configurations). In this example, we are telling Require.js to look inside of the js folder and then load mobile.js.

`<head> <script src="js/libs/require.js" data-main="js/mobile"></script> </head>`

If we look inside of mobile.js, we will find that the `$.mobile.linkBindingEnabled` and `$.mobile.hashListeningEnabled` jQuery Mobile attributes are set to false.

          // Sets the require.js configuration for your application.
          require.config( {

            // 3rd party script alias names (Easier to type "jquery" than "libs/jquery-1.8.3.min")
            paths: {

                // Core Libraries
                "jquery": "libs/jquery",
                "jquerymobile": "libs/jquerymobile",
                "underscore": "libs/lodash",
                "backbone": "libs/backbone"

            },

            // Sets the configuration for your third party scripts that are not AMD compatible
            shim: {

                "backbone": {
                      "deps": [ "underscore", "jquery" ],
                      "exports": "Backbone"  //attaches "Backbone" to the window object
                }

            } // end Shim Configuration

          } );

          // Includes File Dependencies
          require([ "jquery","backbone","routers/mobileRouter","jquerymobile" ], function( $, Backbone, Mobile ) {

            // Prevents all anchor click handling
            $.mobile.linkBindingEnabled = false;

            // Disabling this will prevent jQuery Mobile from handling hash changes
            $.mobile.hashListeningEnabled = false;

            // Instantiates a new Backbone.js Mobile Router
            this.router = new Mobile();

          } );

Next, inside of the Backbone.js Router class object, we can respond to haschange events and manually call the jQuery Mobile `changePage()` method. Below is a small snippet of mobileRouter.js.

            // Backbone.js Routes
            routes: {

                // When there is no hash bang on the url, the home method is called
                "": "home",

                // When #category? is on the url, the category method is called
                "category?:type": "category"

            },

            // Home method
            home: function() {

                // Programatically changes to the categories page
                $.mobile.changePage( "#categories" , { reverse: false, changeHash: false } );

            }

The example page illustrates how to render a jQuery Mobile ListView that is populated with dynamic JSON data asynchronously. Feel free to take a deeper look into the source code to see how Require.js and Backbone.js are used.