Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9014507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:31:36+00:00 2026-06-16T03:31:36+00:00

I am having problems updating the template property on an Ember view when it

  • 0

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);
    ​
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:31:37+00:00Added an answer on June 16, 2026 at 3:31 am

    That’s not how you should set the template. The template should not return a compiled template but rather act as a compiled template. In your code you’re trying to set the template itself as a computed property and you’re compiling two possible templates to it conditionally. IMHO you should compile a template with a binding to a computed property that evaluates a text based on your toggle property, like this:

    App = Em.Application.create();
    
    App.MyView = Em.View.extend({
        template: Em.Handlebars.compile('toggle is {{toggleState}}'),
        toggle: true,
        toggleState: function(){
            if (this.get('toggle')) {
                return 'set to \'true\'';
            } else {
                return 'set to \'false\'';
            }
         }.property('toggle')
    });
    
    theView = App.MyView.create();
    theView.append('body');
    
    Ember.run.later(function() {
        console.log('later');
        theView.set('toggle',false);
    }, 2000);
    

    See fiddle here

    This will only change what needs to be change, so you don’t have to re-compile a template.

    EDIT:

    I’ve made some modifications to the fiddle (You can see it here).

    What I am doing, instead of assigning the template property directly, I’m compiling the templates to Ember.TEMPLATES collection before creating the app (I’m assuming you’ll do something like this in prod anyway), and I’m changing your computed property to return the name of the template to be used depending on the condition (in your case content.type), and I’m binding the templateName property that computed property. Once the template changes, you have to rerender your view. The code can be improved, but I’ll paste it here to demonstrate the solution:

    (function() {
    
        Em.TEMPLATES["one"] = Em.Handlebars.compile('<a href="#">{{view.content.data}}</a>');
        Em.TEMPLATES["two"] = Em.Handlebars.compile('<h1>{{view.content.data}}</h1>');
    
    })();
    
    App = Em.Application.create();
    
    App.MyView = Em.CollectionView.extend({
        content: [
            Em.Object.create({type: 1, data:"Item type 1 is a link"}),
            Em.Object.create({type: 2, data:"Item type 2 is a header"})
        ],
        itemViewClass: Em.View.extend({
            templateNameBinding: 'currentTypeName',
            currentTypeName: function() {
                if (this.get('content.type') == 1) {
                    return "one";
                } else if (this.get('content.type') == 2) {
                    return "two";
                }
            }.property('content.type'),
            templateNameObserver: function() {
                this.rerender();
            }.observes('templateName')
        })
    });
    // ... rest of the code... 
    

    Like I said, this code can be improved… hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having problems updating a SPAN in my HTML with the result from
I'm having problems updating records to contain NULL values - in particular, a field
Im having problems with classpaths. I have used them before with import but I'm
I am updating my class Nesty so it's infinite but I'm having a little
I'm having problems updating a div's content in IE using .update; I also tried
Im trying to update details of a single customer and I'm having problems updating
I am having some problems updating to the right value. On the page which
I'm having problems passing information, updating progress and indicating done with a SwingWorker class
I'm having problems updating ( or refreshing) an Icon from button after closing a
I am having problems writing statements, especially when updating. I want to update a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.