简体   繁体   English

车把模板中的动作未触发

[英]Action in handlebars template not firing

I have the following simple code:我有以下简单的代码:

(JSFiddle) http://jsfiddle.net/aR2UX/3/ (JSFiddle) http://jsfiddle.net/aR2UX/3/

The action in the template will not fire no matter what I do.无论我做什么,模板中的操作都不会触发。 I'm suspicious it's due to the way the view is rendered, but no matter what I read, I can't wrap my head around it.我怀疑这是由于视图的呈现方式造成的,但无论我阅读什么,我都无法理解它。

I'm still trying to understand ember views, but from what I've read my code in the fiddle should work.我仍在尝试理解 ember 视图,但是从我在小提琴中读到的代码应该可以工作。 It doesn't seem to though, can anyone help me?不过好像不行,有谁能帮帮我吗?

(This is in a rails 3.2 app, using ember-rails, ember 0.9.4 and the handlebars template gem, btw). (这是在 rails 3.2 应用程序中,使用 ember-rails、ember 0.9.4 和把手模板 gem,顺便说一句)。

Any help is greatly appreciated as I took a bit of a risk on ember for this project.非常感谢任何帮助,因为我为这个项目承担了一些 ember 风险。

This doesn't answer the OP's original problem, but it fits with the title, and if you have this problem, then search engines point you to this page.这并没有回答 OP 的原始问题,但它符合标题,如果您有这个问题,那么搜索引擎会将您指向此页面。 Here is one possible reason why actions might not fire: the tag might not be within the Ember Application's rootElement.以下是操作可能不会触发的一个可能原因:标签可能不在 Ember 应用程序的 rootElement 内。 So if you have JS所以如果你有JS

FooApp = Ember.Application.create({
  rootElement:"#foo",
  test: function() {
    alert("testing");
  }
});

and handlebars和车把

<div id="foo"></div>
<a {{action "test" target="FooApp"}}>Fail</a>

Then nothing will happen.那么什么都不会发生。 Had me baffled for hours.让我困惑了几个小时。

Since the handlebars templates don't have names, they are inserted into the DOM at the point they are declared – this is why you see anything at all.由于车把模板没有名称,它们会在声明时插入到 DOM 中——这就是为什么你会看到任何东西。

This:这个:

{{#each content}}
{{view Skills.RecommendedSkillView skillBinding="this"}}
{{/each}}

Doesn't do anything for you since content is not defined at this point and the handlebars template is not associated with the view.不会为您做任何事情,因为此时未定义content ,并且把手模板与视图无关。

Likewise:同样地:

Skills.RecommendedSkillsListView = Ember.View.extend({
    templateName: 'app/templates/recommended_skills_list',

Doesn't help you since templateName is pointing to a non-existant handlebars template, and the view classes are never instantiated (otherwise you would get a warning of a missing template).对您没有帮助,因为templateName指向一个不存在的把手模板,并且视图类永远不会实例化(否则您会收到缺少模板的警告)。

Furthermore:此外:

<td><a href="#" {{action "a"}}>DO A THING</a></td>

Will not call the function a since again this template is just inserted once in the DOM where it is encountered and a is not defined in that context (neither is skill ).不会调用函数a因为该模板只是在遇到它的 DOM 中插入一次,并且a未在该上下文中定义(也不是skill )。

And finally, when you do get your code closer to work the way you intend, there will be a problem with that every handlebars template is, by default, wrapped in a div .最后,当你让你的代码更接近你想要的工作方式时,会出现一个问题,每个把手模板默认情况下都包含在div Hence your table will have div around every tr .因此,您的桌子将在每个tr周围都有div This is solved by changing the tagName : http://emberjs.com/#toc_36这是通过更改tagName解决的: http : //emberjs.com/#toc_36

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

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