页面转换发生后,jQuery Mobile 获取导致页面转换的单击项目的 ID

发布于 2024-11-09 14:08:47 字数 1386 浏览 0 评论 0原文

我有一个带有两个“页面”的 jquery 移动应用程序。第一页是 Google 地图,第二页是建筑物列表视图,其中每个列表项都有一个与其关联的 ID。当用户单击建筑物时,地图将平移到该建筑物并显示标记。但是,jquery mobile 的怪癖之一是我必须调用 google.maps.event.trigger(map, 'resize')map.panTo(somePosition) 页面转换发生之后,否则地图会显得很奇怪。我正在利用 jQuery Mobile pageshow 事件来执行此操作。我的问题是,在 pageshow 事件处理程序参数中是否有任何内容或任何将某些内容附加到 pageshow 事件处理程序参数中的方法,可以让我识别首先单击导致页面转换的列表项,因此我可以使用它来确定将地图平移到哪里?

目前,我必须以一种圆形的方式来做这件事,我点击所选列表项的单击事件,将所选列表项 id 存储在全局变量中,然后稍后使用该全局变量在 pageshow 事件处理程序中时。

例如:

var selectedBuilding;

$('a.buildingSelect').click(function(e) {
    e.preventDefault();
    selectedBuilding = $(this).attr("data-buildingNum");
});


$('div').live('pageshow',function(event, ui){
    if (ui.prevPage.attr('id') === "buildingSelect") {
        google.maps.event.trigger(map, 'resize');
        map.panTo(selectedBuilding);
    }
});

除了有点混乱之外,问题之一是用户根本没有选择建筑物编号,例如当他们按建筑物列表页面上的“后退”按钮时。在这种情况下,pageshow 事件处理程序仍会执行,但如果用户之前未选择建筑物,则所选建筑物将不存在,或者会导致地图平移到 最后选择的建筑物(如果他们之前选择过建筑物),这实际上不是预期的行为。我确信我可以为两个事件处理程序提供一些额外的全局变量来围绕此进行编码,但这会有点混乱。理想情况下,我正在寻找一种方法,只需绑定到 pageshow 事件,并从绑定函数中就能够派生出单击了哪个建筑物 id。

总的来说,我对 jQuery 和 javascript 还比较陌生,所以请记住这一点。尽管我对 Chrome 中的 javascript 调试工具进行了一些尝试,但我仍然不确定如何有效地查找 ui 参数中提供的信息。 pageshow 事件处理程序,我认为这将是解决此类问题的良好开端。

I have a jquery mobile application with two "pages". On the first page is a Google map, and on the second page is a listview of buildings, where each list item has an id associated with it. When the user clicks a building, the map is meant to pan to that building and show a marker. However, one of the quirks of jquery mobile is that I have to call google.maps.event.trigger(map, 'resize') and map.panTo(somePosition) after the page transition takes place, otherwise the map shows up funky. I am tapping into the jQuery Mobile pageshow event to do this. My question is, is there anything either in the pageshow event handler arguments or any way of attaching something to the pageshow event handler arguments that will allow me to identify the id of the list item that was clicked which caused the page transition in the first place, so I can use this to determine where to pan the map to?

At the moment, I'm having to do it in a round about kind of way, where I tap into the click event of the selected list item, store the selected list item id in a global variable and then use that global variable later on when in the pageshow event handler.

So something like:

var selectedBuilding;

$('a.buildingSelect').click(function(e) {
    e.preventDefault();
    selectedBuilding = $(this).attr("data-buildingNum");
});


$('div').live('pageshow',function(event, ui){
    if (ui.prevPage.attr('id') === "buildingSelect") {
        google.maps.event.trigger(map, 'resize');
        map.panTo(selectedBuilding);
    }
});

Apart from being a little messy, one of the problems with this is if the user didn't select a building number at all, such as when they press the 'back' button on the building list page. In this case, the pageshow event handler still gets executed, though the selected building will be either non existent if the user hasn't selected a building previously, or will cause the map to pan to the last selected building if they have selected a building previously, which isn't actually the intended behaviour. I am sure I could make some additional global variables available to both event handlers to code around this, but it would be a little messy. Ideally what I'm looking for is a way to only have to bind to the pageshow event and from within the binded function be able to derive which building id was clicked.

I am still relatively new to jQuery and javascript in general, so please keep this in mind. Though I've had a bit of a play with the javascript debugging tools in Chrome, I'm still not really sure how I would go about effectively finding what information is made available in the ui argument to the pageshow event handler, which I imagine would be a good start towards solving problems like this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落日海湾 2024-11-16 14:08:47

您可以将 pageshow 事件处理程序粘贴到 buildingSelect 的点击处理程序中,并使用允许您通过事件绑定器传递数据的语法:.live( eventType, eventData,处理程序) 来自 http://api.jquery.com/live/

但在执行此操作之前,您必须先杀死其他活动的 pageshow 处理程序。如果是我,我只会清理你存储这些全局变量的方式,然后就到此为止了。也许将它们放在一个位于命名空间之外的漂亮对象中,这样更安全。

编辑:将我不正确的口语“销毁”更改为正确的die。因此,您可能希望像 $('div').die() 中那样终止事件处理程序或文档中解释的任何其他变体:http://api.jquery.com/die/

You could stick the pageshow event handler inside buildingSelect's click handler, and use the syntax that allows you to pass data through event binders: .live( eventType, eventData, handler ) from http://api.jquery.com/live/.

But then you'd have to die the other live pageshow handlers before doing this. If it was me, I'd just clean up the way you're storing those global variables and call it a day. Maybe put them in a nice object that sits off a namespace so it's safer.

EDIT: changed my incorrect colloquial "destroy" to the correct die. So you would want to die the event handlers as in $('div').die() or any of the other variations explained in the documentation: http://api.jquery.com/die/

你的背包 2024-11-16 14:08:47

根据 Milimetric 的回答,我基本上做了以下事情:

var selectedBuilding;

function rejigMap(event ui) {
    google.maps.event.trigger(map, 'resize');
    map.panTo(selectedBuilding);
    $('div').die('pageshow', rejigMap);
}

$('a.buildingSelect').click(function(e) {
    e.preventDefault();
    selectedBuilding = $(this).attr("data-buildingNum");
    $('div').live('pageshow', rejigMap);
});

仅在选择建筑物时创建 live pageshow 监听器,然后立即销毁(dieing)它我已经执行了我需要的代码,它消除了 rejigMap 徘徊并侦听不相关的 pageshow 事件的问题,例如当用户按下“后退”时会触发" 按钮并且尚未选择根本没有建设。我相信可以按照 Milimetric 的建议进行更多重构,使用 .live(eventType, eventData, handler) 实际将所选建筑物传递到 rejigMap,这将允许全局变量被完全消除。

Following on from Milimetric's answer, here is what I essentially did:

var selectedBuilding;

function rejigMap(event ui) {
    google.maps.event.trigger(map, 'resize');
    map.panTo(selectedBuilding);
    $('div').die('pageshow', rejigMap);
}

$('a.buildingSelect').click(function(e) {
    e.preventDefault();
    selectedBuilding = $(this).attr("data-buildingNum");
    $('div').live('pageshow', rejigMap);
});

By only creating the live pageshow listener when a building is selected, and then destroying (dieing) it immediately after I've executed the code I need to, it removes the problem of rejigMap hanging around and listening to unrelated pageshow events, such as would fire when the user presses the "back" button and hasn't selected a building at all. I believe this could be refactored a little more by following Milimetric's suggestion to use .live(eventType, eventData, handler) to actually pass through the selected building to rejigMap, which would allow the global variable to be eliminated completely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文