0

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

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

但我想post从数据属性中获取。可能吗?

4

1 回答 1

1

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

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

链接看起来像:

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

如果您在 s 进入页面之前对它们无能为力<a>,那么您可以像这样拼凑一些东西:

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

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

于 2013-11-27T21:54:33.347 回答