主干网+jquery:如何从与主干网的链接中检索数据's路由器

backbone + jquery: How to retrieve data from the link with backbone's router?

本文关键字:主干网 数据 检索 路由器 链接 +jquery      更新时间:2023-11-11

如何从带有主干路由器的链接中检索数据?例如

html、

<a href='#!article/edit/about-us/' data-type="post">Edit</a>

主干,

routes: {
        '!article/edit/:url/':    'renderDynamicPage'
    },
    renderDynamicPage: function (url) {
        console.log(url);
        var $this = this;
        $($this).click(function(){
           console.log($this.data("type"));
        });
    },

结果,

about-us

但是我想从数据属性中得到CCD_ 1。有可能吗?

我认为你做不到。当路由器知道位置变化时,对<a>的任何引用都早已不复存在。路由器需要的任何东西都应该在路由中:

routes: {
    '!article/edit/:url/(:type)': 'renderDynamicPage'
}

链接看起来像:

<a href='#!article/edit/about-us/post'>Edit</a>

如果你在<a>出现之前对它们无能为力,那么你可以把它们拼凑成这样:

$('a[data-type]').each(function() {
    var $this = $(this);
    $this.attr('href', $this.attr('href') + $this.data('type'));
});

以修补href。或者,您可以将click处理程序添加到您关心的所有<a>中,并在更新window.location之前手动将data-type合并到URL片段中。