To explain my question, I have concocted a contrived example, so bear with me. I have two related ember-data models that each have the same type of embedded association:
App.Article = DS.Model.extend({
title: DS.attr("string"),
summary: DS.belongsTo(App.Summary, {embedded: true}),
});
App.Book = DS.Model.extend({
title: DS.attr("string"),
summary: DS.belongsTo(App.Summary, {embedded: true}),
});
App.Summary = DS.Model.extend({
text: DS.attr("string"),
});
One great feature of ember-data is that find()
will return an object of the correct type which can be used to render the view even before the actual data values have been retrieved from the server. However, I worry this convenience doesn't extend completely to associations.
In cases like this, when multiple types of objects share the same type of associated data, I would like to reuse my view that displays Summary
objects for both Book
objects and Article
objects. In particular, I would like to do the following:
book: Ember.Route.extend({
route: '/book',
connectOutlets: function(router) {
book = App.Book.find(1);
router.get('applicationController').connectOutlet('titleOutlet', 'book', book);
router.get('applicationController').connectOutlet('summaryOutlet', 'summary', book.get('summary'));
},
}),
That is, I would like to have one view for the book-specific stuff, and one view for the summary, which is independent of whether a Book
or Article
is displayed.
Unfortunately, I can't do book.get('summary')
because this returns null
if the book has not been populated by store.load()
, which is called asynchronously by Book.find()
.
The alternative seems to be to pass around the book object itself, and always reference nested paths, starting from the root level of the object. In this particular case, the structure of Book
and Article
are identical, so there is no difference. But if I want to use an association and refer to it independently of its context, it seems there should be another way.
Thoughts?
I've put together a gist illustrating this full example.
Update
I'm resigned to Mike Aski's point that this is not possible. But in order to keep my logic abstract, I create a uniformly named binding pointing to the association, where ever it may be buried, each time I connectOutlet. It's when I call connectOutlet that I know what type of object I'm sending to the controller/view and thus the view can use the binding and doesn't need to know about the rest of the object.