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.
That’s because you haven’t initialized your concrete
App.BarCollectioninstance with acontentproperty. This is needed sinceEmber.ArrayProxyproxies to thecontentarrray. 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
lowerCaseand classesUpperCase. So it’sApp.foo. Also, you should always access properties viagetandset. This guarantees that bindings are resolved correctly.The result looks like this, see http://jsfiddle.net/pangratz666/fpqTv/:
Handlebars:
JavaScript:
As a note: in your case there is no requirement to use an
ArrayProxyfor yourbarsproperty. 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: