Skip to content

Instantly share code, notes, and snippets.

@mehulkar
Created August 2, 2012 01:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehulkar/3232255 to your computer and use it in GitHub Desktop.
Save mehulkar/3232255 to your computer and use it in GitHub Desktop.
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