2013-07-10 221 views
2

我有一個小的餘燼設置加載產品並顯示它們。因此,每個產品都有店面知道我想從產品頁面商店聯繫,但我不知道在哪裏我要告訴燼產品具有存儲emberjs中的嵌套對象

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'} 
    } 
] 
+0

你能解釋一下什麼叫「告訴燼產品都有專賣店」,並分享你的API將返回的例子是什麼意思? –

+0

我的目標是在呈現「App.Product」對象的模板中,它將知道產品中存在具有id的商店對象,因此它可以構建鏈接。 –

回答

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") 
0

好像你有什麼應該工作

我做了一個簡單的JSBIN模擬您的API並具有工作store link

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> 
+0

奇怪的是,我在我的例子中得到的是'Assertion failed:Can not call with'id'on a undefined object.'自己創建的App.Product可能是問題嗎? –