简体   繁体   English

jQuery 在自定义 ember 的把手助手中不起作用

[英]jQuery doesn't work in a custom ember's handlebars helper

I've just learned from others to write a helper to replace <title> in different templates, then I try to write a testing example like this:我刚刚跟别人学了写一个helper来替换不同模板中的<title> ,然后我尝试写一个这样的测试示例:

Ember.Handlebars.helper('headTitle', function (title) {
  Ember.$('head').find('title').text(title);
});

But this not work at all.但这根本行不通。 I've also try to use console.log(Ember.$('head').find('title')) to see if it's really select the <title> tag, and it does not.我也尝试使用console.log(Ember.$('head').find('title'))来查看它是否真的选择了<title>标签,但它没有。 The real strange things happens next: after loading all the codes, I type Ember.$('head').find('title') in the browser's console, now it finds the tag!接下来真正奇怪的事情发生了:加载所有代码后,我在浏览器的控制台中输入Ember.$('head').find('title') ,现在它找到了标签!

Why jQuery not works in Handlebars helper?为什么 jQuery 在 Handlebars 助手中不起作用?

I put the testing code in jsfiddle, please help me to figure this out, thank you.我把测试代码放在jsfiddle中,请帮我解决这个问题,谢谢。

http://jsfiddle.net/nightire/8arRv/1/ http://jsfiddle.net/nightire/8arRv/1/

UPDATE:更新:

Sorry guys, I finally made it works (change the <title> content as I expected), before I was use the same value as the initial one, so I can't see the difference between them.对不起,伙计们,在我使用与初始值相同的值之前,我终于让它工作了(按我的预期更改<title>内容),所以我看不到它们之间的区别。

You can check the working code at: http://jsfiddle.net/nightire/8arRv/2/您可以在以下位置检查工作代码: http : //jsfiddle.net/nightire/8arRv/2/

But during this experience, I also found if I change the selector for another pre-existing element, it does not work like above one.但是在这次体验中,我还发现如果我为另一个预先存在的元素更改选择器,它不会像上面那样工作。 To see this strange behavior, take look at this version: http://jsfiddle.net/nightire/8arRv/3/要查看这种奇怪的行为,请查看此版本: http : //jsfiddle.net/nightire/8arRv/3/

The only thing I've changed is the selector in helper function, instead of looking for <title> now I want to find an element with id #title .我唯一改变的是辅助函数中的选择器,而不是寻找<title>现在我想找到一个带有 id #title的元素。

I still can not understand why, hope someone can give an explanation.我还是不明白为什么,希望有人能给出解释。

To put it plainly, putting DOM manipulation code in a Handlebars helper like that is a terrible idea.说白了,像这样将 DOM 操作代码放在 Handlebars 助手中是一个糟糕的主意。 Handlebars helpers are not meant to call random functions, they're meant to insert data, in place, into the DOM. Handlebars 助手不是为了调用随机函数,而是为了将数据插入到 DOM 中。 You don't even technically know how or when that method is being called since it's an implementation detail (which is probably why your code isn't working how you expect it to).从技术上讲,您甚至不知道该方法是如何或何时被调用的,因为它是一个实现细节(这可能是您的代码无法按您期望的方式工作的原因)。

If you want to change the document title, I suggest something like this .如果您想更改文档标题,我建议您这样 There's a pull request for document.title integration into Ember, but it hasn't been merged yet.一个将document.title 集成到 Ember的拉取请求,但它尚未合并。

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

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