[英]'this' in ember.js link-to helper
I am trying to get hands-on experience with popular ember.js
framework and quite amazed with lots of magical things. 我试图通过流行的ember.js
框架获得动手经验,并且对许多神奇的事物感到惊讶。
One such is this
in link-to
handlebar helper which I couldn't digest. 一个这样的是this
在link-to
车把帮助我消化不了。
Following is my code scenarios: 以下是我的代码方案:
// handlebar script
<script type='text/x-handlebars' data-template-name='products'>
<h1>Products</h1>
<ul class='list-unstyled col-md-8'>
{{#each}}
<h2>{{title}}</h2>
<p>{{#link-to 'product' this class='btn btn-success' }}Buy for ${{price}}{{/link-to}}</p>
{{/each}}
</ul>
</script>
// in app.js
App.PRODUCTS = [
{
title: 'Chair',
price: 99,
description: 'Chair is...',
},
...
];
App.Router.map(function() {
this.resource('products');
this.resource('product', { path: '/products/:title'});
});
App.ProductsRoute = Ember.Route.extend({
model: function() {
return App.PRODUCTS;
}
});
App.ProductRoute = Ember.Route.extend({
model: function(params) {
return App.PRODUCTS.findBy('title', params.title);
}
});
I came to know that this
refers to current product
in template but my questions are: 我知道this
是指模板中的current product
,但是我的问题是:
App.ProductRoute
or router
? 它是否与App.ProductRoute
或router
交互? If yes how? 如果是,怎么办? this
with this.title
? 我们可以更换this
与this.title
? Yes, the link-to
helper does. 是的, link-to
帮助器可以。 The link-to
helper builds a URL based on the information you give it. link-to
帮助器会根据您提供的信息link-to
构建URL。 Using the route name, it looks up the route defined in your router and uses the path that you specified. 使用路由名称,它将查找路由器中定义的路由并使用您指定的路径。 Then it uses the ID from the object to fill in the dynamic segment(s). 然后,它使用来自对象的ID来填充动态段。 It also makes sure to correctly fill in parent routes if necessary. 如果有必要,还可以确保正确填写父路由。
No, you need to use this
because it needs the id
to build the URL. 不,您需要使用this
因为它需要id
来构建URL。 You used to be able to pass in just this.id
, but I don't think you can do that any more. 您过去只能传递this.id
,但我认为您不能再这样做了。 (It's a bad idea anyway.) Also, Ember.js will use the object you pass it as the model for the route, thereby skipping the model
hook in your ProductRoute
. (无论如何,这都是一个坏主意。)此外,Ember.js会将传递给它的对象用作路由的model
,从而跳过ProductRoute
的model
挂钩。 And the object you pass it can either be a fully-loaded model, or a promise of a model. 您传递给它的对象可以是完全加载的模型,也可以是模型的承诺。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.