2014-08-31 41 views
2

我是EmberJS的新手。試圖讓一些簡單的應用程序更熟悉它。有一種我不甚瞭解的行爲。我正在構建這個燼。EmberJS/ember-cli - 如何不引用模板中的模型?

路由器

Router.map(function() { 
    this.resource('quotes', {path: '/'}, function() { 
    this.route('new'); 
    this.resource('quote', {path: '/quotes/:id'}, function() { 
     this.route('delete'); 
    }); 
    }); 
}); 

路由/ quote.js

import Ember from 'ember'; 

export default Ember.Route.extend({ 
    model: function(params) { 
    return this.store.find('quote', params.id); 
    } 
}); 

控制器/ quote.js

import Ember from 'ember'; 

export default Ember.Controller.extend({ 

    actions: { 

    deleteQuote: function() { 
     // is calling this.model here OK? 
     this.model.destroyRecord(); 
     this.transitionTo('quotes'); 
    } 
    } 

}); 

個quotes.hbs

{{#link-to 'quotes.new' tagName='h3' class='new-quote'}}New Quote{{/link-to}} 

<ul> 
{{#each quote in controller}} 
    {{#link-to 'quote' quote tagName='li' class='quotes'}} 
    {{quote.title}} 
    {{/link-to}} 
{{/each}} 
</ul> 

{{outlet}} 

quote.hbs

{{!-- probably shouldn't be referring to model directly here... --}} 
<p class='description'>{{model.body}}</p> 

<button {{action deleteQuote}}>Delete Quote</button> 

我只有用 'model.body' 在報價模板時,我明確地定義一個QuoteController。但是,如果我不生成controllers/quote.js文件,則可以在報價模板中使用body而不使用模型前綴。

我不確定這是否在Ember中是標準的,但不知何故,在我看來,我在模板中調用model.something。如果有人能夠向我解釋這種邏輯/行爲,以及什麼是「正確」的方式來做到這一點,將不勝感激。提前感謝!

[編輯]

感謝BenjaminRH的解決方案。對於其他人在看問題,想了解更多的信息,這裏的東西,可以幫助:http://coryforsyth.com/2014/02/17/ember-controller-versus-objectcontroller/

回答

1

嘗試使它成爲一個ObjectController而不是Controller的。以下是Ember guide的更多詳細信息。

+0

這樣做!謝謝。我想我明白了。 – supahken 2014-09-01 01:28:10

相關問題