2014-06-28 26 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

好的,我明白了。所以我想我必须更多地了解烬哲学。我认为这将是一个简单的方法,而不是为每个图像编写方法。 –