简体   繁体   English

ember.js链接到帮助器的“ this”

[英]'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. 一个这样的是thislink-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 ,但是我的问题是:

  1. Does it interact with App.ProductRoute or router ? 它是否与App.ProductRouterouter交互? If yes how? 如果是,怎么办?
  2. Can we replace this with this.title ? 我们可以更换thisthis.title
  1. 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. 如果有必要,还可以确保正确填写父路由。

  2. 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 ,从而跳过ProductRoutemodel挂钩。 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.

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