| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Newer versions of jqm will be integrated using angular-jqm and not this project.
Background: See the post in the angular mailing list: https://groups.google.com/d/msg/angular/qG8YRwVaL_g/zgKAvaArmB0J
#JQuery Mobile Angular Adapter
##Description
Integration between jquery mobile and angular.js. Needed as jquery mobile enhances the pages with new elements and styles and so does angular. With this adapter, all widgets in jquery mobile can be used directly in angular, without further modifications. Furthermore, this adapter also provides special utilities useful for mobile applications.
If you are interested in how to build mobile web apps with this adapter, have a look at the german book Mobile Web-Apps mit JavaScript.
##Dependencies
##Examples
##Reporting Issues
##Usage Note: The directive ng-app for the html element is required, as in all angular applications.
Include this adapter after angular and jquery mobile (see below).
<html ng-app>
<head>
<title>MobileToys</title>
<link rel="stylesheet" href="lib/jquery.mobile.css"/>
<script src="lib/jquery.js"></script>
<script src="lib/jquery.mobile.js"></script>
<script src="lib/angular.js"></script>
<script src="lib/jquery-mobile-angular-adapter.js"></script>
</head>
Create a index.xhtml file like the one below:
<html ng-app>
<head>
<title>MobileToys</title>
<link rel="stylesheet" href="lib/jquery.mobile.css"/>
<script src="lib/requirejs.js" data-main="main.js"/>
</head>
<body>
... your jqm pages ...
</body>
</html>
And a main.js file with the following content:
require.config({
shim:{
'angular':{ deps:['jquery'], exports:'angular'}
}
});
function tryHoldReady() {
if (!tryHoldReady.executed && window.jQuery) {
window.jQuery.holdReady(true);
tryHoldReady.executed = true;
}
}
tryHoldReady();
require.onResourceLoad = tryHoldReady;
require([
"jquery",
"jquery-mobile-angular-adapter",
... // your controllers, angular modules, ...
], function (jquery) {
jquery.holdReady(false);
});
Notes:
The adapter always takes the major and minor version of jquery mobile, e.g. if you want to use jquery mobile 1.3.x, use the adapter version 1.3.x. The patch version is assigend incrementally.
Directory structure
compiled: The created versions of the adapter.
src: The main files of uitest.js
test/ui: The ui self tests for uitest.js
test/unit: The unit tests of uitest.js
Install the dependencies: npm install.
Build it: ./node_modules/.bin/grunt
Auto-Run tests when file change: ./node_modules/.bin/grunt dev
##Navigation and routes
The adapter integrates angular routes with jquery mobile:
We set $locationProvider.html5Mode(true), $locationProvider.hashPrefix('!') and add the tag <base href="{address of your html file}"> to your header.
Default routing: If no route is defined, the default jquery mobile url handling applies:
You can set the special property jqmOptions on routes, e.g.
$routeProvider.when('/somePage', {
templateUrl:'someTemplate.html',
jqmOptions: { transition: 'flip' }
});
Those properties are directly passed to $.mobile.changePage. For a documentation of the available options have a look at the jquery mobile documentation.
You can set the special property onActivate in routes. If this is set, it will be evaluated in the scope of the page to which the route navigates to, before the pagebeforeshow event.
This expression can also use the properties from $routeParams and $route.current.locals, which are calculates by the resolve entry of the route. E.g.
$routeProvider.when('/somePage/:name', {
templateUrl:'someTemplate.html',
onActivate: 'someFn(someParam, name)',
resolve: {someParam: function() { return 'hello'; }}
});
function SomePageController($scope) {
$scope.someFn = function(someParam, name) {
expect(someParam).toBe('hello');
}
}
relative links in pages are treated relative to the templateUrl of the route, not the route path. E.g. given the mapping
$routeProvider.when('/someFolder/page1.html', { templateUrl:'#page1' });
$routeProvider.when('/someFolder/page2.html', { templateUrl:'/someTemplateFolder/page2.html' });
Page1 is embedded page, and therefore, all links within that page are relative to the initial url of the app. Page2 is an external page and all relative links in that page are relative to the folder /someTemplateFolder.
Please also look at the extensions to the $location service for controlling history and changing route params for just one route call.
Default routing: basePath+$location.url()
Notes:
Using $.mobile.changePage directly is not recommended. It still works, but it will no more update the browser location.href.
If you don't want $locationProvider.html5Mode you can disable it. By this, you get hash urls like /index.html#!/somePath... back. E.g.
module.config(function($locationProvider) { $locationProvider.html5Mode(false) }));
Internally, we use jquery mobile to load the pages and do the transition between the pages. By this, we automatically support the prefetching and caching functionality for pages from jquery mobile (see their docs for details). E.g. use <a href="prefetchThisPage.html" data-prefetch> ... </a> in a parent page to prefetch a child page.
If you want to start an app directly on a subpage, use the following url:
Restrictions:
##Scopes Every page of jquery mobile gets a separate scope. The $digest of the global scope only evaluates the currently active page, so there is no performance interaction between pages.
For communicating between the pages use the ngm-shared-controller directive (see below).
##Directives, Filters and Services
Syntax: <div ngm-shared-controller="name1:Controller1,name2:Controller2, ...">
Mobile pages are small, so often a usecase is split up into multiple pages. To share common behaviour and state between those pages, this directive allows shared controllers.
The directive will create an own scope for every given controllers and store it in the variables as name1, name2, .... If the controller is used on more than one page, the instance of the controller is shared.
Note that the shared controller have the full scope functionality, e.g. for dependecy injection or using $watch.
The following event directives are supported (see http://jquerymobile.com/demos/1.2.0/docs/api/events.html):
Usage: E.g. <a href="#" ngm-swipeleft="myFn()">
The directive @ngm-if allows to add/remove an element to/from the dom, depending on an expression. This is especially useful at places where we cannot insert an ng-switch into the dom. E.g. jquery mobile does not allow elements between an ul and an li element.
Usage: E.g. <div ngm-if="myFlag">asdfasdf</div>
Methods and Properties:
$location.routeOverride(someOverride): By this, you can override route properties only for the next routing. This is useful e.g. for passing special parameters to the onActivate expression. The following properties of routes can be overridden:
E.g.
$location.routeOverride({
locals: {someKey: 'someValue'},
jqmOptions: {transition: 'pop'}
});
$location.path('/someRoutePath');
function SomePageController($scope) {
$scope.someActivateFn = function(someKey) {
expect(someKey).toBe(someValue);
}
}
$location.backMode(): This will try to go back in history to the url specified by $location. E.g. if the navigation path to the current page is page1->page2->page3 and we then call $location.path('page1'); $location.backMode() this will go two steps back in history. Note that this is in analogy to the already existing angular method $location.replace.
The service $waitDialog allows the access to the jquery mobile wait dialog. It provides the following functions:
Default messages are:
Lists can be paged in the sense that more entries can be additionally loaded. By "loading" we mean the display of a sublist of a list that is already fully loaded in JavaScript. This is useful, as the main performance problems result from DOM operations, which can be reduced with this paging mechanism.
To implement this paging mechanism, the angular filter paged was created. For displaying a page within a list, simply use:
list | paged:'pagerId':12
This returns the subarray of the given array with the currently loaded pages.
Parameters:
The first parameter is required and must be unique for every usage of the paged filter. It is the property name in the scope which stores the state of pagination for this filter usage, and also contains the function loadMore and hasMore (see below).
If the second parameter is a number, it is interpreted as the pageSize. If this parameter is omitted, the default page size is used. This is by default 10, and can be configured using
module(["ng"]).value('defaultListPageSize', 123);
For filtering and sorting the paged array, you can use filter chaining with the angular filters filter and orderBy, e.g.
list | filter:{name:'someName'} | orderBy:'name' | paged:'pagerId'
To show a button that loads the next page of the list, use the following syntax:
<a href="#" ngm-if="pagerId.hasMore" ngm-vclick="pagerId.loadMore()">Load More</a>
The following example shows an example for a paged list for the data in the variable myList:
<ul data-role="listview">
<li ng-repeat="item in list | paged:'pager1'">{{item}}</li>
<li ngm-if="pager1.hasMore">
<a href="#" ngm-vclick="pager1.loadMore()">Load more</a>
</li>
</ul>
Note: pagerId.cache stores the last result that was returns for a list | paged:'pagerId' expression. This can be used to check whether the paged list is empty, .. without refiltering the list.
The attribute data-collapsed has bidirectional data binding, e.g.
<div data-role="collapsible" data-collapsed="someProperty">...</div>
using ng-repeat with a checkbox or radio button without a wrapper element can be done like the following:
<label ng-repeat="l in [1,2]">
{{l}}
<input type="checkbox">
</label>
the jqm adapter does not change the url when a popup is opened/closed. This is due to the fact that the jqm adatper assumes that urls represent routings for pages, and not parts of pages.
The new attribute data-opened has bidirectional data binding for opening/closing the popup, e.g.
<div data-role="popup" data-opened="someProperty">...</div>
the jqm adapter does not change the url when a panel is opened/closed. This is due to the fact that the jqm adatper assumes that urls represent routings for pages, and not parts of pages.
The new attribute data-opened has bidirectional data binding for opening/closing the panel, e.g.
<div data-role="panel" data-opened="someProperty">...</div>
##Integrating custom jquery mobile plugins with angular
All integration work is done using the jqmNgWidget provider.
This will automatically create angular directives for all jqm widgets that are contained in $.mobile and use the jqm widget factory (e.g. $.mobile.dialog). If you want to specify a custom handler for a jqm directive, use the following pattern:
ng.config(["jqmNgWidgetProvider", function(jqmNgWidgetProvider) {
jqmNgWidgetProvider.widget("somePlugin", ["jqmNgWidget", function(jqmNgWidget) {
return {
link: function(widgetName, scope, iElement, iAttrs, ngModelCtrl) {
jqmNgWidet.createWidget(widgetName, iElement, iAttrs);
// Do additional binding work...
}
}
}]);
}]);
See src/main/webapp/widgetAdapters.js for more examples.
##Integration strategy
Jquery mobile has two kinds of markup:
Integration strategy:
We have a precompile phase: This is called before the angular compiles does it's work, i.e. before $compile is called, and before directive.template and directive.templateUrl is evaluated. Here, we trigger the jqm create and pagecreate event. Before this, we instrument all stateful jqm widgets (see above), so they do not really create the jqm widget, but only add the attribute jqm-create=<widgetName> and jqm-link=<widgetname> to the corresponding element. By this, all stateless markup can be used by angular for stamping (e.g. in ng-repeat), without calling a jqm method again, so we are fast. Furthermore, we have special handlers in the precompile phase for those jqm widgets that wrap themselves into new elements (checkboxradio, slider, button, selectmenu, search input), as the angular compiler does not like this. Finally, we also mark all jqm pages with the jqm-page attribute. This is needed as jqm pages are represented as data-role=page in the dom and angular does not allow to create directives that only match pages but not other jqm widgets.
We have the directive ngmPage: This creates an own scope for every page. By this, we are able to disconnect the scope of the pages that are not visible right now. This is important for optimizing performance. This creates the jqm pages by calling element.page() in the pre link phase, however without the pagecreate event. By this, we only create the page instance, but do not modify the dom (as this is not allowed in the pre link phase). Furthermore, the page jqm widget instance is already available for the other widgets, which are created in the post link phase.
We have the directive ngmCreate: This will create the jqm widgets in the post link phase. For widgets that wrap themselves into new elements this needs to be called for the wrapper, that was already created in the precompile phase. This is important as the jqm widgets do more DOM transformations during creations that the angular compiler does not like (e.g. the jqm widget <input type="checkbox>" enhances the sibling <label> element and wraps that element). By calling the widget during the post link phase of the wrapper element those DOM modifications are ok with angular.
We have the directive ngmLink: Here we listen for changes in the model and refresh the jqm widgets when needed and vice versa. For elements that wrap themselves into new elements this will be called on the original element (e.g. the <input> for <input type="checkbox"> elements), in contrast to the ngmCreate directive.
All together: This minimizes the number DOM traversals and DOM changes
Ohter possibilities not chosen:
Integration of jqm routing and angular routing:
| Back | FazBrowse Home | New Git URL |