service_wev8.js
3.53 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
define("mService", ["mUtil", "mobilebone", "i18n"], function (mUtil, Mobilebone, i18n) {
var cache = {};
var namespace = "mService/";
var services = {
setService: function (id, service) {
this[id] = service
},
getService: function (id) {
return this[id];
}
};
function Service(id) {
var service = services.getService(id);
if (service) return service;
var index = id.indexOf("/");
var cssId = index === -1 ? id : id.substring(0, index);
this.id = id;
this.name = namespace + id;
this.css = "css!" + namespace + cssId + "_css";
this.html = namespace + id + "_html";
this.setService = function (service) { // service.js对象
this.service = service;
};
this.setTpl = function (tpl) { // service.html 模板字符串
this.tpl = tpl;
};
services.setService(id, this);
}
Service.prototype = {
load: function (callback) {
var done = $.Deferred();
var t = this;
callback = callback || function () { };
if (t.tpl) {
callback(t.tpl, t.service);
return done.resolve(t.tpl, t.service);
}
require([this.name, this.css], function (service) {
require([t.html], function (tpl) {
callback(tpl, service);
done.resolve(tpl, service);
t.setTpl(tpl);
t.setService(service);
});
});
return done;
},
preload: function () {
require([this.name, this.css]);
}
};
return {
show: function (id, cfg) {
var pageid = "page_service_" + (id.replace("/", "_"));
var _identity = cfg && cfg["_identity"];
if (_identity) {
pageid += "_" + _identity;
}
var isRerender = true;
var $pageout = $(".in." + Mobilebone.classPage);
var clsNameStr = pageid;
if (_identity) {
clsNameStr = clsNameStr.replace("_" + _identity, '');
}
clsNameStr = clsNameStr.replace(/_/g, '-');
if ($pageout.length) {
pageid += '_' + $pageout[0].id;
}
var $page = cache[pageid];
if (!$page) {
$page = $('<div id="' + pageid + '" class="' + clsNameStr + ' page out" data-onpagefirstinto="none" data-callback="serviceChange"><div class="page-loading wev-loading">' + i18n.LOADING_DATA + '</div></div>');
$(document.body).append($page);
cache[pageid] = $page;
} else {
isRerender = !_identity;
}
var pagein = $page[0];
if(cfg.classAnimation && !cfg.preload){
var pageOutAnimation = $pageout.attr("data-form");
!pageOutAnimation && $pageout.attr("data-form", cfg.classAnimation);
$page.attr("data-form", cfg.classAnimation);
pagein.flagAniBind = false;
pagein.animationend = function(page, cls) {
if(cls == 'out' && page.classList.contains("reverse")){
!pageOutAnimation && $pageout.removeAttr("data-form").removeClass(cfg.classAnimation);
$page.removeAttr("data-form").removeClass(cfg.classAnimation);
}
};
}
!cfg.preload && Mobilebone.transition(pagein, $pageout[0], false, {});
if (!isRerender) {
return;
}
var service = new Service(id);
service.load().then(function (tpl, serv) {
tpl = mUtil.replaceI18n(tpl);
$page.html(tpl);
serv.mounted($page, cfg);
});
},
preload: function (id) { // 预加载service页面资源
new Service(id).preload();
},
load: function (id, callback) {
var service = new Service(id);
service.load(callback);
},
getService: function (id) {
return new Service(id);
}
};
});