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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:56:38+00:00 2026-06-08T06:56:38+00:00

In EmberJS I want to create a model that has a field that is

  • 0

In EmberJS I want to create a model that has a field that is a collection of other models. I figured this would be an easy thing, but I can’t get it to work.

My current approach, as demonstrated in the JSFiddle below, is to create the two models (in this case Foo and Bar) and a collection model for Bars (BarCollection). Then I give the Foo a BarCollection as an attribute, and then try to add Bars to the collection. It doesn’t work. No errors are thrown, and the collection exists, but no elements are ever added to it.

http://jsfiddle.net/tbxhQ/1/

  • 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-08T06:56:40+00:00Added an answer on June 8, 2026 at 6:56 am

    That’s because you haven’t initialized your concrete App.BarCollection instance with a content property. This is needed since Ember.ArrayProxy proxies to the content arrray. If you don’t specify one, the proxy doesn’t which array it shall use …

    Another issue is that it’s a convention to name instances lowerCase and classes UpperCase. So it’s App.foo. Also, you should always access properties via get and set. This guarantees that bindings are resolved correctly.

    The result looks like this, see http://jsfiddle.net/pangratz666/fpqTv/:

    Handlebars:

    <script type="text/x-handlebars">
        All the bars from {{App.foo.name}}:
        <ul>
            {{#each App.foo.bars }}
                <li>{{this.name}}</li>
            {{/each}}
        </ul>
    </script>
    

    ​
    JavaScript:

    App = Ember.Application.create();
    App.Bar = Ember.Object.extend();
    
    App.BarCollection = Ember.ArrayProxy.extend();
    
    App.foo = Ember.Object.create({
        name: 'Fred',
        bars: App.BarCollection.create({
            content: []
        })
    });
    
    App.getPath('foo.bars').pushObject(App.Bar.create({
        name: 'bob'
    }));​
    

    As a note: in your case there is no requirement to use an ArrayProxy for your bars property. A simple JavaScript array does the job as well. So this would be my final suggestion for your posted example, see http://jsfiddle.net/pangratz666/CHHXb/:

    JavaScript:

    App = Ember.Application.create();
    App.Bar = Ember.Object.extend();
    
    App.foo = Ember.Object.create({
        name: 'Fred',
        bars: [],
        addToBars: function(name) {
            this.get('bars').pushObject(App.Bar.create({
                name: name
            }));
        }
    });
    
    App.get('foo').addToBars('bob');​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to access the class used to create an object instance from EmberJS.
Emberjs core has a new router implementation which extends Ember.StateManager. This is the basic
In my EmberJS application, there are use cases where I want to observe something
This error is driving me crazy . I tried freenode #emberjs before asking here.
I want to iterate an Emebrjs collection - each time a user clicks next
In emberJS, I have a model with an object property called style. I can
How to get index item of array in an emberjs view. This returns an
i have a similar question to this question Emberjs - Disable and Enable TextField
I building a ember.js app that also ties into some other jQuery plugins. I
I want see all possible paths in some object. How can i do this?

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.