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 9209933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:57:39+00:00 2026-06-18T00:57:39+00:00

Given these JSON data models on a RESTful server /users {users:[ {id:1,first_name:John,last_name:Doe}, {id:2,first_name:Donald,last_name:Duck} ]}

  • 0

Given these JSON data models on a RESTful server

/users

{"users":[
   {"id":"1","first_name":"John","last_name":"Doe"},
   {"id":"2","first_name":"Donald","last_name":"Duck"}
]}

/users/1

{"user": 
   {"id":"1","first_name":"John","last_name":"Doe","account":"1"}
}

/accounts

{"accounts":[
   {"id":"1","owned_by":"1"},{"id":"2","owned_by":"2"}
]}

/accounts/1

{"account":
   {"id":"1","owned_by":"1","transactions":[1,17]}
}

and these Ember data models

App.Store = DS.Store.extend({
  revision: 11,
  adapter: DS.RESTAdapter.create({
    url: 'http://api.mydomain.ca'
  })
});

App.User = DS.Model.extend({
    firstName: DS.attr('string'),
    lastName: DS.attr('string'),
    account: DS.belongsTo('App.Account')
});

App.Account = DS.Model.extend({
    ownedBy: DS.belongsTo('App.User'),
    transactions: DS.hasMany('App.Transaction')
});

what other ember code do I have to write to load the data into the models and then write a template that outputs a user’s name, account id, and the number of transactions in the account?

  • 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-18T00:57:41+00:00Added an answer on June 18, 2026 at 12:57 am

    I was able to solve this so I will post my code in case it helps someone else. The trick is to make sure the JSON data is formatted exactly how Ember wants it and to create the proper routes.

    From what I can tell, Ember expects parent objects to provide a list of child objects. This feels weird to me so if anyone knows a way to do it with child objects referencing their parents with a foreign key please let me know.

    I changed the account property on my /user/:user_id JSON object to account_id I also included the account_id on the user objects found at /users and I changed the owned_by property on the account to user_id.

    My javascript file

    var App = Ember.Application.create();
    
    // Router
    App.Router.map(function() {
        this.resource('users', function() {
            this.resource('user', {path:':user_id'});
        }); // '/#/users/:user_id'
        this.resource('accounts', function() {
            this.resource('account', {path:':account_id'});
        });
    });
    
    App.IndexRoute = Ember.Route.extend({
        redirect: function() {
            this.transitionTo('users');
        }
    });
    
    App.UsersRoute = Ember.Route.extend({
        model: function() {
            return App.User.find();
        }
    });
    
    App.AccountsRoute = Ember.Route.extend({
        model: function() {
            return App.Account.find();
        } 
    });
    
    // Controllers
    
    App.TransactionsController = Ember.ArrayController.extend();
    
    // Adapter
    App.Adapter = DS.RESTAdapter.extend({
        url: 'http://api.mydomain.ca'
    });
    
    // Models
    
    App.Store = DS.Store.extend({
      revision: 11,
      adapter: App.Adapter.create({})
    });
    
    App.User = DS.Model.extend({
        firstName: DS.attr('string'),
        lastName: DS.attr('string'),
        account: DS.belongsTo('App.Account')
    });
    
    App.Account = DS.Model.extend({
        user: DS.belongsTo('App.User'),
        transactions: DS.hasMany('App.Transaction'),
        balance: function() {
          return this.get('transactions').getEach('amount').reduce(function(accum, item) {
              return accum + item;
          }, 0);
      }.property('transactions.@each.amount')
    });
    
    App.Transaction = DS.Model.extend({
        account: DS.belongsTo('App.Account'),
        amount: DS.attr('number'),
        description: DS.attr('string'),
        timestamp: DS.attr('date')
    });
    

    And the handlebars templates

    <script type="text/x-handlebars" data-template-name="application">
        <div class="row">
            <div class="twelve columns">
                <h2>Accounts</h2>
                <p>{{outlet}}</p>
            </div>
        </div>
    </script>
    
    <script type="text/x-handlebars" data-template-name="users">
        <div class="row">
            <div class="three columns" id="users">
                {{#each user in controller }}
                    {{#linkTo "user" user class="panel twelve columns"}}{{user.firstName}} {{user.lastName}}{{/linkTo}}
                {{/each}}
            </div>
            <div class="nine columns" id="user">
                {{ outlet }}
            </div>
        </div>  
    </script>
    
    <script type="text/x-handlebars" data-template-name="user">
        <h2>{{firstName}} {{lastName}}</h2>
        {{#if account}}
        {{render "account" account}}
        {{else}}
        Error: Account not set up!
        {{/if}}
    </script>
    
    <script type="text/x-handlebars" data-template-name="accounts">
        <div class="row">
            <div class="three columns" id="accounts">
                {{#each account in controller }}
                    {{#linkTo "account" account class="panel twelve columns"}}{{account.id}} {{account.user.firstName}} {{account.user.lastName}}{{/linkTo}}
                {{/each}}
            </div>
            <div class="nine columns" id="account">
                {{ outlet }}
            </div>
        </div>  
    </script>
    
    <script type="text/x-handlebars" data-template-name="account">
        <p>Account Number: {{id}}, Balance: {{balance}}, {{transactions.length}} transactions</p>
        {{render "transactions" transactions}}
    </script>
    
    <script type="text/x-handlebars" data-template-name="transactions">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Amount</th>
                    <th>Timestamp</th>
                    <th>Description</th>
                </tr>
            </thead>
            <tbody>
            {{#each transaction in controller}}
                <tr>
                    <td>{{transaction.id}}</td>
                    <td>{{transaction.amount}}</td>
                    <td>{{transaction.timestamp}}</td>
                    <td>{{transaction.description}}</td>
                </tr>
            {{/each}}
            </tbody>
        </table>
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some json from picasaweb: http://picasaweb.google.com/data/feed/api/user/100489095734859091829?kind=album&access=visible&alt=json-in-script&thumbsize=144c Here's the output as tidied up by jsonview -
Given these two queries: Select t1.id, t2.companyName from table1 t1 INNER JOIN table2 t2
Given these tables: create table Orders ( Id INT IDENTITY NOT NULL, primary key
I am learning Java and have been given these options: How can you implement
This is easier to explain with an example. Given these two classes: public class
i am using asynctask to fetch images from given url.these images are displaying in
can any one explain inter relation between these below given directives ; Do not
Given file names like these: /the/path/foo.txt bar.txt I hope to get: foo bar Why
Given a BehaviorSubject, what is the practical difference between calling all of these different
Given the Java code below, what's the closest you could represent these two static

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.