I am having problems updating the template property on an Ember view when it is a computed property.
Ember compiles the template correctly when the view first loads and returns as a property, but the template computed property does not update when the dependency is later changed.
Here is an example on JSFiddle: http://jsfiddle.net/VkJC3/
App=Em.Application.create();
App.MyView = Em.View.extend({
toggle: true
,template: function(){
if (this.get('toggle')) {
return Em.Handlebars.compile('toggle is true')
} else {
return Em.Handlebars.compile('toggle is false')
}
}.property('toggle')
});
theView= App.MyView.create();
theView.append('body');
Ember.run.later(function() {
console.log('later');
theView.set('toggle',false);
}, 2000);
Any other suggestions on how to accomplish this are appreciated. Maybe it is best to just put if helpers into one handlebars template.
EDIT:
Here is a more complete example showing the Ember.CollectionView that will contain the above Ember.View: http://jsfiddle.net/VkJC3/6/
After the Ember.run.later, the first item should change from a type 1 to type 2, and have the computed template property update.
App=Em.Application.create();
App.MyView = Em.CollectionView.extend({
content: [
Em.Object.create({type: 1, data:"Maybe item type 1 is a link"})
,Em.Object.create({type: 2, data:"And item type 2 is a header"})]
,itemViewClass: Em.View.extend({
template: function(){
if (this.get('content.type')==1) {
return Em.Handlebars.compile('<a href="#">{{view.content.data}}</a>')
} else if (this.get('content.type')==2) {
return Em.Handlebars.compile('<h1>{{view.content.data}}</h1>')
}
}.property('content.type')
})
});
theView= App.MyView.create();
theView.append('body');
Ember.run.later(function() {
console.log('later');
theView.get('content')[0].set('type',2);
}, 2000);