-
Notifications
You must be signed in to change notification settings - Fork 4
/
knockout.jquery.mobile.js
107 lines (94 loc) · 2.93 KB
/
knockout.jquery.mobile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* This script is designed to auto correct some conflicts between
* kockout.js and jquery.mobile.js
* it needs to be added to the document after jquery, but before jquery.mobile
* written by Master Morality.
*/
$(document)
.bind('mobileinit',function(){
/*
* postpone mobile init
* we need to make sure that ko.applyBindings(...)
* is called prior to $.mobile.initializePage(), so that any
* inline templates are parsed without added classes etc.
*/
$.mobile.autoInitializePage = false;
})
.bind('ready',function(){
/*
* initialized page.
* we assume that ko.applyBindings(...) has been called ala:
* $(function(){... ko.applyBindings(...); });
* somewhere in the body, or at least has been call in
* a 'ready' event handler prior to now.
*/
$.mobile.initializePage();
/*
* overwrite template update binding helper,
* this must be done after $.mobile.initializePage()
* because we want any initial data binding
* to work correctly. once the initial structure
* has been created, we are only worried about
* changes.
*/
var _update = ko.bindingHandlers.template.update,
_root = false;
ko.bindingHandlers.template.update = function(e){
/*
* We don't want to enhance elements
* that were added as children of a root
* template, because they will be enhanced
* anyway once the parent element is
* enhanced.
*/
var isRoot = _root ? false : (_root = true),
result = _update.apply(this, arguments);
if(isRoot)
{
var elem = $(e);
/*
* jQuery Mobile doesn't process pages
* until they are navigated to for the first time.
* This can cause a problem with elements
* being enhanced before their pages are.
* However, we only need to worry about
* enhanced pages anyway, because once
* a page is initially enhanced, it automatically
* enhances it's descendants.
*/
var page = elem.closest('.ui-page')[0];
if( page )
{
/*
* when a template is rendered,
* the element is replaced, this will
* remove any enhancement that jQuery mobile has
* applied.
*
* In order to re-apply the enhancement,
* we need to find the closest parent widget.
*/
var widget = elem.parent(':jqmData(role)');
/*
* we also need to divine what this widget
* is, so we can figure out if it can be
* refreshed or not.
*/
var widgetRole = widget.jqmData('role').replace('-',''),
widgetInstance = widget.jqmData(widgetRole),
widgetIsRefreshable = widgetInstance && $.isFunction(widgetInstance['refresh']);
if( widgetIsRefreshable ){
widgetInstance['refresh']();
} else {
/*
* if a widget isn't refreshable
* we need to do it the old fashion way.
*/
widget.trigger('create');
}
}
_root = false;
}
return result;
};
});