2

我有一个小的 ember 集加载产品并显示它们。所以每个产品都有商店知道我想从产品页面链接到商店,但我不知道我必须在哪里告诉 ember 产品有商店

window.App = Ember.Application.create()

App.Router.map ->
  @route 'products'
  @route 'product', path: '/product/:product_id'
  @route 'store', path: '/store/:store_id'

App.ProductsRoute = Ember.Route.extend

  setupController: (controller)->
    $.get('api', (response) ->
      products = response.response.products //this object holds the store
        .filter((p)->  p.gender == 'male')
        .map ((p)-> App.Product.create(p))

      controller.set('content', products)
    )
    controller.set 'name', 'Produkte'


App.Product = Ember.Object.extend
  style: (->
    "background-image:url('" + @get("image") + "')"
  ).property("url")

模板

script(type="text/x-handlebars", data-template-name="product")
    <h1>{{page_title}}</h1>
    <img {{bindAttr src="image"}}>
    {{#linkTo "store" store}}Store{{/linkTo}}

产品 json

[
  {
    id: 1
    name: 'product1',
    gender: 'male'
    store: {id: 1, name: 'store1'}
  }
]
4

2 回答 2

0

似乎你所拥有的应该工作

我制作了一个简单的 JSBIN 来模拟您的 api 并有一个有效的商店链接

App = Ember.Application.create();
App.Router.map(function() {
  this.route("store", {path: 'store/:store_id'});
});

App.IndexRoute = Ember.Route.extend({
  model: function(){
      return [{ id: 1, name: 'product1', gender: 'male', store: {id: 1, name: 'store1'}}];
  }
});

<script type="text/x-handlebars">{{outlet}}</script>
<script type="text/x-handlebars" id="index">
  <h2>All Products:</h2>
  <ul>
    {{#each}}
      <li>{{name}} - {{#linkTo "store" store}}Store{{/linkTo}}</li>
    {{/each}}
  </ul>
</script>
<script type="text/x-handlebars" id="store">
  <h1>Store: {{name}}</h1>
</script>
于 2013-07-11T07:28:35.717 回答
0

好的,这似乎是 WTF 的余烬时刻之一。该store变量似乎是某种保留字。当我将密钥设置为_store它按预期工作时:

window.App = Ember.Application.create()

App.Router.map ->
  @route 'products'
  @route 'product', path: '/product/:product_id'
  @route 'store', path: '/store/:store_id'

App.ProductsRoute = Ember.Route.extend

  setupController: (controller)->
    $.get('api', (response) ->
      products = response.response.products
        .filter((p)->  p.gender == 'male')
        .map ((p)->
          p._store = p.store
          App.Product.create(p))

      controller.set('content', products)
    )
    controller.set 'name', 'Produkte'

App.ProductRoute = Ember.Route.extend
  setupController: (controller, model)->
    console.log(model);

App.Product = Ember.Object.extend
  style: (->
    "background-image:url('" + @get("image") + "')"
  ).property("url")
于 2013-07-11T14:28:49.527 回答