简体   繁体   English

ember.js可点击div内的可点击链接

[英]ember.js clickable links inside clickable div

I've found a few answers for this specific to jQuery but nothing related to Ember.Js. 我已经找到了一些特定于jQuery的答案,但没有任何与Ember.Js相关的答案。 I'm pretty new to Ember so I may be missing something obvious so my bad if so. 我对Ember很陌生,所以我可能会遗漏一些明显的东西,所以我的不好,如果是这样的话。

I've created a clickable div using an action inside my controller like so: 我使用控制器内部的动作创建了一个可点击的div,如下所示:

goHere: function(article) {
    this.transitionTo('view-article',{article_id: article.article_id});
}

But now my anchors inside of that div don't work. 但是现在我在那个div里面的锚点不起作用。 Any idea how to get around this? 知道怎么解决这个问题吗?

EDIT: I can't show you all of the code because it's a huge project, but basically we have Topics, in each Topic is a series of Articles. 编辑:我无法向您展示所有代码,因为它是一个巨大的项目,但基本上我们有主题,每个主题是一系列文章。 The Articles are listed on the homepage like so: 文章在主页上列出如下:

<div class="article pull-left span10"{{action goHere this}} >
    ...
    <a href="url.com">XX</a>
    ...
</div>

And that anchor tag, when clicked, triggers the goHere action instead of redirecting to url.com 点击该锚标记会触发goHere操作,而不是重定向到url.com

Try 尝试

goHere: function(article) {
    this.transitionToRoute('view-article',article);
}

If that doesn't work a jsbin/jsfiddle would be helpful for more context. 如果这不起作用,jsbin / jsfiddle将有助于更多上下文。

See: http://emberjs.com/api/classes/Ember.Controller.html#method_transitionToRoute 请参阅: http//emberjs.com/api/classes/Ember.Controller.html#method_transitionToRoute

Using transitionTo in controller is deprecated but it should work with depreciation warning. 不推荐在控制器中使用transitionTo,但它应该与折旧警告一起使用。 Using transitionToRoute (as in @RyanHirsch answer) is the right way to do that. 使用transitionToRoute(如在@RyanHirsch中的答案)是正确的方法。

You can better use linkTo helper 您可以更好地使用linkTo帮助程序

   {{#linkTo 'view-article' model_to_be_passed}}Go to View Article {{/linkTo}}

In my case, I was using links entered by the user, which were turned into anchor tags by rails_autolink on the server, so I wasn't able to replace the links with ember link-to. 在我的情况下,我使用的是用户输入的链接,这些链接在服务器上被rails_autolink转换为锚标记,因此我无法用ember link-to替换链接。

I was able to work around this with a few steps. 我能够通过几个步骤来解决这个问题。 First, I added the class autolink to all the links I was generating with autolink. 首先,我将类autolink添加到我使用autolink生成的所有链接。 Then in the Ember action for the surrounding div, I did: 然后在周围div的Ember动作中,我做了:

if event.target.className == 'autolink'
    @send('handleAutolink')
else
    // (handle non-link click)

Then on the ApplicationRoute, I put in a handler for the handleAutolink action that basically cancels the ember action and opens the link with javascript: 然后在ApplicationRoute上,我为handleAutolink操作添加了一个处理程序,它基本上取消了ember操作并用javascript打开了链接:

handleAutolink: ->
  event.preventDefault()
  window.open(event.target.href, '_blank')

I realize this is probably a giant hack, but I'd be happy to hear another approach! 我意识到这可能是一个巨大的黑客,但我很高兴听到另一种方法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM