jQuery mobile:移动增强之前的 json 处理
我开始使用 jQuery Mobile 开发移动应用程序。这个想法是构建 HTML 静态页面,并在显示它们之前调用服务器以获取输入标签和按钮的 i18n 文本。我使用特殊属性标记容易更改内部文本的 HTML 元素:“data-i18n”:
对于标签:
<label data-i18n="login.username" for="loginPaciente.username">login.username</label>
对于按钮:
<button data-i18n="login.submit" type="submit" data-theme="a">login.submit</button>
我使用 JSON 调用服务器:
$('#pageLogin').live('pagebeforeshow',function(event, ui){
var action = "/MyServerApp/namespace1/mobile_Action_Login_configPage.action";
$.getJSON(action, function(data) {
var resources = data.i18n_resources;
var id, text;
var $scope = $('#pageLogin');
for (i=0; i<resources.length; i++){
id = resources[i].id;
text = resources[i].text;
$scope.find('[data-i18n="' + id + '"]').html(text);
}
});
});
这与标签完美配合,因为 JQM 不不要修改这些 HTML 元素。问题出在按钮上,因为 JQM 隐藏了我定义的按钮,并创建了一个新的跨度来呈现按钮。当我读取 JSON 结果时,我可以找到并更改我定义的按钮,但不能找到并更改 JQM 创建的新范围,因此屏幕上显示的文本是旧文本:“login.submit”。
有没有办法在 JQM 更改 HTML 代码之前执行 JSON 调用?
PD:不动态构建整个 HTML 页面(包括 i18n 文本)的原因是,将来我想用 PhoneGap 或类似的 shell 封装 Web 应用程序,并且我想在里面分发 HTML 页面、CSS 和脚本应用程序,并最大限度地减少与服务器的数据流量。
预先感谢:
卡洛斯。
编辑:更改文本后调用 $scope.trigger('create')
无法解决问题。
I'm starting to develop a mobile app with jQuery Mobile. The idea is to build HTML static pages, and before showing them, call to the server to obtain the i18n text for the input labels and buttons. I mark the HTML elements that are susceptible to change the inner text with an special attribute: "data-i18n":
For a label:
<label data-i18n="login.username" for="loginPaciente.username">login.username</label>
For a button:
<button data-i18n="login.submit" type="submit" data-theme="a">login.submit</button>
I call to the server using JSON:
$('#pageLogin').live('pagebeforeshow',function(event, ui){
var action = "/MyServerApp/namespace1/mobile_Action_Login_configPage.action";
$.getJSON(action, function(data) {
var resources = data.i18n_resources;
var id, text;
var $scope = $('#pageLogin');
for (i=0; i<resources.length; i++){
id = resources[i].id;
text = resources[i].text;
$scope.find('[data-i18n="' + id + '"]').html(text);
}
});
});
This works perfectly with the labels, because JQM doesn't modify these HTML elements. The problem comes with the button, because JQM hides the button I've defined, and creates a new span to render the button. When I read the JSON result, I can find and change the button I've defined, but not the new span that JQM has created, so the text that appears on screen is the old one: "login.submit".
Is there any way to execute the JSON call before JQM changes the HTML code?
P.D.: The reason for not building the whole HTML page dynamically (including i18n texts) is that in the future, I want to encapsulate the web app with PhoneGap or a similar shell, and I want to distribute the HTML pages, CSS and scripts inside the application, and minimize the data traffic with the server.
Thanks in advance:
Carlos.
EDIT: invoking $scope.trigger('create')
after changing the text doesn't solve the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最后,我自己找到了解决问题的方法,捕获了事件“pagebeforecreate”。
我在需要国际化的每个页面上调用此函数,传递我需要调用的服务器操作和页面 id:
请注意,我强制使用对服务器的同步调用,以避免页面移动增强之前i18n 文本已准备就绪。
这是在 json 回调中调用的函数:
该函数查找所有 i18n 元素并使用翻译后的文本覆盖其内部 html:
该函数覆盖输入的占位符文本:
最后,这是生成 i18n 资源的 jsp。我用的是Struts2,所以jsp不是直接调用的。我调用一个操作,而 jsp 只是视图。 i18n 资源是使用 Struts2 功能获得的:
我不知道这是否是国际化 JQM 应用程序的最佳方式。任何建议将不胜感激。
Finally I've found the solution to my problems by myself, catching the event "pagebeforecreate".
I invoke this function on each page I need to internationalize, passing the server action I need to call and the page id:
Note that I force to use synchronous calls to the server, in order to avoid the page mobile enhancing before the i18n texts were ready.
This is the function that is invoked in the json callback:
This function finds all i18n elements and override their inner html with the translated texts:
This function overrides the placeholder texts for the inputs:
And finally, this is the jsp that produces the i18n resources. I use Struts2, so the jsp is not invoked directly. I invoke an action and the jsp is only the view. The i18n resources are obtained using Struts2 capabilities:
I don't know if this is the best way to internationalize a JQM application. Any suggestion will be apreciated.
您可以使用
ui-btn-text
类更改元素的文本:
或者如果您不确定该元素是否已初始化通过 jQuery Mobile,您可以首先检查 ui-btn-class 是否存在:
You can just change the text of the
<span>
element with theui-btn-text
class:Or if you aren't sure if the element will have been initialized by jQuery Mobile you can check for the existence of the
ui-btn-class
first: