2013-12-10 70 views
6

我想知道是否可以向計算屬性中添加參數。到目前爲止,我嘗試過的所有東西都會導致錯誤,並且沒有發現任何問題我想使用未包含在我的模型中的值來構建URL。Ember使用參數計算的屬性

我正在尋找的東西,應該是這樣的:

// App.js 
App.Image = DS.Model.extend({ 
    image_path_sh: DS.attr(), // image.jpg 
    image_size_nm: DS.attr(), // 234234 
    image_alt_sh: DS.attr(), // My image 
    image_abs_url: function(width, height) { 
     return "http://localhost/images/" + this.get('image_path_sh') + "/" + width "x" + height 
    }.property('image_path_sh') 
}); 

// index.html 
// I know this doesn't work, but I'd like to have something that easy to use 
{{#each image}} 
    <img src="{{image_abs_url 250 250}}" alt="{{image_alt_sh}}" /> 
{{/each}} 

我的服務器將返回被調整大小的圖像。我不想把它放在我的數據庫中,因爲它們不是固定值。

+3

我希望這樣的事情,我不認爲你可以做到這一點,雖然,也許'Ember.Handlebars.helper'將是最接近它。 – iConnor

回答

12

計算屬性不應該依賴於參數,它會打破緩存範例,這正是幫助者和方法的用途。

Ember.Handlebars.helper('img', function(prop, height, width, options) { 
    return new Handlebars.SafeString('<div style="height:' + height +'px;width:'+ width +'px;background-color:' + prop + '">' + prop + '</div>'); 
}); 

http://emberjs.jsbin.com/IgUFaTAk/1/edit