ModelArray should support containing models of multiple types #90

Closed
pixelcort opened this Issue Feb 13, 2012 · 4 comments

Projects

None yet

4 participants

@pixelcort

I have a DS.Adapter#findQuery call that returns records of multiple types from the server's response, which I want to load into its modelArray. This is important because the server returns them in a specific order which I want to preserve when rendering them via {{#each}} in one of my templates.

However, the modelArray that is passed to DS.Adapter#findQuery is of type DS.AdapterPopulatedModelArray, which only supports one type of model per modelArray. Furthermore, the DS.Store#find which I use to invoke the query in the first place takes as its first argument a type.

A workaround is to use one DS.Model subclass and fake the types in some other way.

@garth

We have a similar issue with a global search feature. We pass a query into multiple components and each returns a set of matches for different object types.

Our solution is to have a model SearchResult that has attributes for name, desc, type and typeId. This way we can return and render a result set without having to have the actual model data. Then when the user makes a selection you can use the type and typeId to fetch the model.

Alternatively I wonder if there might be a way to create polymorphic model definitions?

@wycats
Ember.js member

The reason that we store the type at present is because the ModelArray proxies to a list of clientIds, which are materialized by the server on demand via:

var contentObject = content.objectAt(index);

if (contentObject !== undefined) {
  model = store.findByClientId(get(this, 'type'), contentObject);
  modelCache.replace(index, 1, [model]);
}

This mirrors the API used by the adapter, which loads a series of records by type:

load: function(array) {
  var store = get(this, 'store'), type = get(this, 'type');

  var clientIds = store.loadMany(type, array).clientIds;

  this.beginPropertyChanges();
  set(this, 'content', Ember.A(clientIds));
  set(this, 'isLoaded', true);
  this.endPropertyChanges();
}

Note that the call to loadMany, which gets the records into the store, is indexed by type.

I'd be interested to hear more about how a polymorphic solution would work, or how an AdapterPopulatedModelArray could support multiple types without making the general case more complicated in the adapter.

@pixelcort

ModelArray and its subclasses could have either content be type/clientId pairs or have a types property where the index on content and types would represent the pair. Then, when talking to the store, store.findByClientId would be called with the correct type and clientId.

@igorT
Ember.js member

There is now a limited support for polymorphism so closing.

@igorT igorT closed this Apr 15, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment