简体   繁体   English

重载ember.js中的“链接到”帮助器

[英]Overloading “link-to” helper in ember.js

I'm trying to make a custom helper that does the same thing that link-to, except with a few more assumptions. 我正在尝试制作一个自定义助手,该助手的功能与链接到的对象相同,只是有一些其他假设。

I'm trying to make this: 我正在努力做到这一点:

{{#my-link-to widget.href}}{{widget.title}}{{/my-link-to}}

It takes the route parameters only (widget.href), and the route is assumed to be 'root'. 它仅采用路由参数(widget.href),并且假定该路由为“根”。 Here's how I'm implementing it: 这是我的实现方式:

Ember.Handlebars.registerHelper('my-link-to', function() {
  var args = [].slice.call(arguments);
  args.unshift('root'); // always use the 'root' route

  return Ember.Handlebars.helpers['link-to'].apply(this, args);
});

However, my resulting links turn out to be all pointing to /# and I get a This link-to is in an inactive loading state because at least one of its parameters presently has a null/undefined value, or the provided route name is invalid. 但是,我得到的链接最终全部指向/#并且我得到一个“ This link-to is in an inactive loading state because at least one of its parameters presently has a null/undefined value, or the provided route name is invalid. error. 错误。

Doing {{#my-link-to 'root' widget.href}}{{widget.title}}{{/my-link-to}} and removing args.unshift('root') works as expected. 进行{{#my-link-to 'root' widget.href}}{{widget.title}}{{/my-link-to}}并删除args.unshift('root')可以正常工作。

I have a fiddle demonstrating this here: http://jsfiddle.net/p2R9y/3/ 我在这里展示了一个小提琴: http : //jsfiddle.net/p2R9y/3/

I'm really new to Ember.js so let me know if I'm doing something blatantly wrong. 我对Ember.js真的很陌生,所以请让我知道我做错了什么。

Each argument passed to a handlebars view helper have some additional informantion. 传递给车把视图助手的每个参数都有一些附加信息。 So args.unshift('root'); 所以args.unshift('root'); isn't enough you need to say what "root" means: a binding a string literal etc. In your case is a string literal, because the expected usage is: 还不够,您需要说“ root”是什么意思:绑定字符串文字等。在您的情况下是字符串文字,因为预期的用法是:

{{#my-link-to-works 'root' widget.href}}

You need to use STRING in the options hash types key 您需要在选项哈希types键中使用STRING

Ember.Handlebars.registerHelper('my-link-to-fails', function() {
  var args = [].slice.call(arguments),
      options = args[args.length-1]; // last argument is always the options hash

  args.unshift('root');
  // the types says what is the type of each argument, in that case we need STRING
  options.types.unshift('STRING');

  return Ember.Handlebars.helpers['link-to'].apply(this, args);
});

I updated your fiddle with this code please give a look http://jsfiddle.net/marciojunior/sR96j/ 我用此代码更新了您的提琴,请看一下http://jsfiddle.net/marciojunior/sR96j/

I think figured your problem. 我想你的问题了。

1. You got your example mixed up 1.你把例子弄混了

You are adding 'root' to the my-link-to-works helper: 您正在将'root'添加到“ my-link-to-works帮助器中:

{{#my-link-to-works 'root' widget.href}}

and doing the unshift on the my-link-to-fails helper. 并在my-link-to-fails帮助器上进行取消固定。 So actually both examples are different. 所以实际上这两个例子是不同的。 One has two arguments (ex: 'root' and '/widget/foo') and the other only has one (ex: '/widget/foo'). 一个有两个参数(例如:“ root”和“ / widget / foo”),另一个只有一个(例如:“ / widget / foo”)。

2. Anyway, why does it work when the first argument is 'root'? 2.无论如何,当第一个参数为“ root”时,为什么它起作用?

The link-to helper first argument must be a routeName . 链接到帮助器的第一个参数必须是routeName This helper can also receive a second argument supplying an explicit dynamic segment value which appends it's value to the resolved route. 该帮助程序还可以接收第二个参数, 参数提供一个显式的动态分段值 ,并将其值附加到解析的路由。 This was how your arguments were interpreted. 这就是解释您的论点的方式。 If you check your example, the working links resolve to, for example, #//widget/foo . 如果您检查示例,则工作链接将解析为例如#//widget/foo You may notice the two forward slashes there, which are a result of your resolved route #/ + your segment value /widget/foo . 您可能会注意到此处有两个正斜杠,这是您解析的路线#/ +段值/widget/foo

3. Solution? 3.解决方案?

Use the link-to helper directly: {{link-to 'widget' 'widget.href'}} and remove the /widget/ part from your href strings or do something like this: http://jsfiddle.net/tqFJ3/4/ 直接使用链接帮助器: {{link-to 'widget' 'widget.href'}}然后从href字符串中删除/widget/部分,或执行以下操作: http : //jsfiddle.net/tqFJ3/ 4 /

The helper: (this is the same as using link-to directly) 助手:(这link-to直接使用link-to相同)

Ember.Handlebars.registerHelper('my-link-to-works', function() {
  // Do extra stuff here...
  return Ember.Handlebars.helpers['link-to'].apply(this, arguments);
});

Change your widgets href and leave only the dynamic segments: 更改小部件href并仅保留动态细分:

App.IndexRoute = Ember.Route.extend({
    model : function() {
        return {
            widgets: [
              { href: 'foo' },
              { href: 'bar' },
              { href: 'baz' }
            ]
        };
    }
});

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

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