2012-06-19 21 views
1

比方說,我有一個像這樣一個觀點:如何使html屬性的值依賴於Ember.js視圖呈現的項目?

App.AnchorView = Ember.View.extend({ 
    tagName: 'a', 
    attributeBindings: ['href'] 
}); 

和一個類定義:

App.Item = Ember.Object.extend({ 
    name: "Unknown Entry", 
    location: "" 
}); 

和類的一個實例:

App.Item.create({ 
    name: "Google", 
    location: "http://www.google.com" 
}); 

和我的模板是:

{{#view App.AnchorView}}{{name}}{{/view}} 

,我希望它呈現爲:

<a href="http://www.google.com">Google</a> 

我該怎麼辦呢?

回答

3

這裏有一個超快速的方法來做到這一點使用bindAttr href和上述ItemsController:http://jsfiddle.net/nLa8Z/1/對於更詳細的文檔退房http://emberjs.com/documentation/#toc_binding-element-attributes-with-bindattr

{{#each App.ItemsController}} 
    <a {{bindAttr href="location"}}>{{name}}</a> 
{{/each}} 

App.ItemsController = Ember.ArrayController.create({ 
    content: [ 
     App.Item.create({ name: "Google", location: "http://www.google.com"}) 
    ]}); 
+0

謝謝,但我特別想知道這是否有可能在改變由視圖本身生成的元素 – Adam