使用jQuery地址处理深度链接的正确方法
我正在使用AJAX应用程序处理深度链接。我的URL的格式是:使用jQuery地址处理深度链接的正确方法,jquery,deep-linking,jquery-address,Jquery,Deep Linking,Jquery Address,我正在使用AJAX应用程序处理深度链接。我的URL的格式是:example.com/#/section/item/param/param/param…我使用.change()侦听器设置它,以处理URL的不同部分。因此,/project/my project/view item/2/compose将滑动打开该项目和该项目下的compose框。问题是,每当URL更改为该URL时,到该点为止的每个操作都会被调用。(当URL仅为/project/myproject时发生的操作将被调用,依此类推。) 在不
example.com/#/section/item/param/param/param…
我使用.change()
侦听器设置它,以处理URL的不同部分。因此,/project/my project/view item/2/compose
将滑动打开该项目和该项目下的compose框。问题是,每当URL更改为该URL时,到该点为止的每个操作都会被调用。(当URL仅为/project/myproject
时发生的操作将被调用,依此类推。)
在不“上链”的情况下,处理我想要的操作的最佳方法是什么?您需要跟踪“当前”URL/程序状态,并将其与
change()中的内容进行比较
以确定下一步需要执行哪些操作。事件变量将传递给change
函数,其属性为:值、路径、路径名、参数名称、参数、查询字符串。要监视的属性是路径名
以下是我整理的一些片段,可以帮助您了解自己的深度以及到底发生了什么变化:
var $current_path = window.location.hash;
$.address.change( function(event) {
// get the difference in the two paths
$changed_path = event.path.replace(new RegExp('^'+$current_path,'i'), '');
// make sure we update the current path
$current_path = event.path;
// how deep is the new path?
$level = event.pathNames.length;
// break the changed part into segments, ignoring leading/trailing slashes
$changed_path_array = $changed_path.replace(/^\/|\/$/g, '').split('/');
// let's see what level actually changed from the current path
$changed_level = $level - $changed_path_array.length;
} );
然后,您可以通过将新深度与分段数组结合使用来组织函数的其余部分,以精确地确定需要更新的内容。根据$current_路径,您可能正在执行新的页面加载,或者只是页面上某个地方的微小更改。使用更内置的高级客户端路由可能会对您有所帮助,例如。它的使用非常简单,可以让您做一些更简单、更优雅的事情,例如:
// Define a route
Path.map("#/project/my-project/view-item/:id/compose").to(function(){
var id = this.params["id"]; //get your ID
$("#panel_" + id).show(); // Do something with the id.
});
// Start listening for changes
Path.listen():
你能发布一些代码吗?