Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Flatten DS.ManyArray in Ember

Flatten a DS.ManyArray in Ember.js

Ember.ArrayProxy.prototype.flatten = Array.prototype.flatten = function() {
  var r = [];
  this.forEach(function(el) {
    r.push.apply(r, Ember.isArray(el)  ? el.flatten() : [el]);
  });
  return r;
}

Use case

If you wanted to get all the books of all the authors in your library.

App.Author = DS.Model.extend
  books: DS.hasMany(App.book)

App.Book = DS.Model.extend()

App.Author.find().mapProperty('books').flatten() will return all books in an instance of a plain Array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.