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

  • SEARCH
  • Home
  • 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 9078321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:35:27+00:00 2026-06-16T19:35:27+00:00

The code below works as I would expect it to in that I can

  • 0

The code below works as I would expect it to in that I can access the data in the environments template via content.dev.name.

I’m struggling with populating this data via an AJAX request though.

I have one rest API that returns the structure as in the controller below. It needs to be called once when the app loads.

Here is my working code:

Controller

App.EnvironmentsController = Ember.Controller.extend();
App.EnvironmentsController.reopenClass({
  find: function(){
    return {dev: {key: 'dev', name: 'Development'}, prod: {key: 'prod', name: 'Production'}};
    return this.content;
  },
});
App.EnvironmentsView = Ember.View.extend({
  templateName: 'environments'
});

Route

...
router.get('applicationController').connectOutlet('environments', App.EnvironmentsController.find());
...

Template

  <script type="text/x-handlebars" data-template-name="environments">
    <ul id="env-menu">
      {{#with content}}
        <li>{{dev.name}}</li>
        <li>{{prod.name}}</li>
      {{/with}}
    </ul>
  </script>

And here is the controller with AJAX that I cannot get the data through to the template with the same format.

Note: I know response.data.environments itself is returning the same structure as my manually coded object above.

App.EnvironmentsController = Ember.Controller.extend();
App.EnvironmentsController.reopenClass({
  environments: {},
  find: function(){
    $.ajax({
      url: 'http://localhost:3000/environments',
      dataType: 'json',
      context: this,
      success: function(response){
        this.environments = response.data.environments;
      }
    });
    return this.environments;
  },
});
App.EnvironmentsView = Ember.View.extend({
  templateName: 'environments'
});

Note, I had something similar working when the controller was an array controller, and the data was being returned as an array. I need to know how to get this working with objects specifically.

Update: More info from questions

Here is a fiddle: http://jsfiddle.net/coder1/csvZX/

I am open to a different pattern. I’m just trying to wrap my head around fetching an object via and and sending it through the controller.

This is a pretty awesome tutorial ( http://trek.github.com/ ) I started with, and I can make this work with an array controller, just not when I’m working with a single object.

  • 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-16T19:35:29+00:00Added an answer on June 16, 2026 at 7:35 pm

    I tweaked your jsfiddle and it now displays your data correctly.

    The key is that the data is handled by a model, not the controller. I modified your original jsfiddle and broke out the data loading portion into a class that is responsible for creating instances of itself in the find() function. This is the same way Trek’s example works.

    New model code:

    App.Environments = Ember.Object.extend({
    
    });
    App.Environments.reopenClass({
        find: function() {
            var env = App.Environments.create({});
            console.log("environments find()");
            // simulate an ajax call, hard coding response below
            setTimeout(function() {
                // Fake response
                console.log("fake ajax response");
                console.log(env);
                env.setProperties({
                    dev: {
                        key: 'dev',
                        name: 'Development'
                    },
                    prod: {
                        key: 'prod',
                        name: 'Production'
                    }
                });
                console.log(env);
            }, 1000);
            return env;
        }
    });
    

    The fake ajax call to google didn’t work so I switched it out and used a setTimeout() call to emulate the delay of an ajax call.

    Notice that the find() method creates a new Environments object, then has the fake-ajax call populate it with data. find() immediately returns the newly created, empty Environments object, which is accessed by the controller and the template. The setProperties() method is used to update the properties of this in a way that Ember’s bindings system can detect the changes and make sure they propagate into the template.

    The only other thing is to update connectOutlets to use the new Environments model and :

    router.get('applicationController').connectOutlet('environments', App.Environments.find());
    

    This line of code calls find(), which returns the newly created Environments object and passes it to an instance of the EnvironmentsController controller, and fills the {{outlet}} in the application template with the EnvironmentsView view.

    Make sure to check out the working jsfiddle example.

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

Sidebar

Related Questions

I would expect that my code below would, in the event I selected 8/6/2012
The code below works fine. But it seem clunky. How would you combine the
The code below works great. I have a MySQL database that contains book titles
This code below works in all web browsers except IE: <input type="text" name="passwordLogin" value="Password"
The code below works well right now; however, it's serially executed. I'd like to
The code below works well but grabs only the UIView which is VISIBLE on
the code below works in Windows Phone 7 private void ShowTime() { txtTime.Text =
the code below works fine but it takes an absolute age to run. How
The code below works nicely. Each title in the MySQL table submission is printed
The code below works unless p.School.SchoolName turns out to be null, in which case

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.