2014-06-28 30 views
0

我無法理解餘燼模板。使用帶有attr-bind的實體對象的參數調用函數

所以我有這樣的對象,它是分配給indexRoute型號:

App.Session = Ember.Object.extend({ 
    function_without_argument: function() { 
    return 'http://www.veryicon.com/icon/png/Flag/Rounded%20World%20Flags/Spain%20Flag.png'; 
    }.property(), 

    with_argmument: function (value){ 
    return 'http://www.veryicon.com/icon/png/Flag/Rounded%20World%20Flags/' + this.get(value) + '%20Flag.png'; 
    }.property() 
}); 

調用<img width="20" {{bind-attr src=function_without_argument}}>作品,但不帶參數<img width="20" {{bind-attr src="with_argmument answer.leader"}}>

爲什麼?

我做了一個小codepen例如:http://codepen.io/gurix/pen/zmdDh

回答

0

計算屬性不應該接受一個參數,而不是使用對象的屬性。

例,

http://codepen.io/anon/pen/ghnsD

JS

App = Ember.Application.create(); 

App.Router.map(function() { 
    // put your routes here 
}); 

App.Session = Ember.Object.extend({ 
    targetFlag:null, 
    function_without_argument: function() { 
    return 'http://www.veryicon.com/icon/png/Flag/Rounded%20World%20Flags/Spain%20Flag.png'; 
    }.property(), 

    answer_image: function(){ 
    return 'http://www.veryicon.com/icon/png/Flag/Rounded%20World%20Flags/' + this.get("answer.leader") + '%20Flag.png'; 
    }.property() 
}); 


App.IndexRoute = Ember.Route.extend({ 
    model: function() { 
    var session = App.Session.create(); 
    session.setProperties({ 
     token: 'abc', 
     answer: { 
     leader: 'Switzerland', 
     looser: 'Argentina' 
     }}); 
    return session;} 
}); 

HBS

<script type="text/x-handlebars"> 
    <h2>Welcome to Ember.js</h2> 

    {{outlet}} 
</script> 

<script type="text/x-handlebars" data-template-name="index"> 

    without argument: <img {{bind-attr src=function_without_argument}}> 

    {{answer.leader}} becomes the image ... 

    <img {{bind-attr src=answer_image}} > 



</script> 
+0

感謝您的評論,但這個解決我的問題沒有絲毫。所以你說無法將參數傳遞給對象的屬性?所以我必須爲每個答案創建一個像'leader_image'這樣的對象方法?沒有更好的方法嗎? –

+0

@MarkusGraf你不應該傳遞參數。這沒有意義。 melc是對的。圖像src應該是'Session'對象的屬性(在init或計算過程中設置),而不是某個傳遞參數的模板邏輯的結果。 –

+0

好的,我明白了。所以我想我必須更多地瞭解燼哲學。我認爲這將是一個簡單的方法,而不是爲每個圖像編寫方法。 –