1

How do I extend Ember.ArrayProxy? I have tried the following:

Ember.ArrayProxy.reopenClass({
  flatten: function(){
    var r = [];

    this.forEach(function(el) {
      r.push.apply(r, Ember.isArray(el)  ? el.flatten() : [el]);
    });

    return r;        
  }
});

But ended up writing the following solution:

// Source: https://gist.github.com/mehulkar/3232255
Array.prototype.flatten = Ember.ArrayProxy.prototype.flatten =function() {
  var r = [];

  this.forEach(function(el) {
    r.push.apply(r, Ember.isArray(el)  ? el.flatten() : [el]);
  });

  return r;
};

Is there something I'm missing in the former example? I'm trying to stick with Ember's methodology, so I would prefer not to use the later.

1 Answer 1

1

If you're using flatten on instances of ArrayProxy you'd want to use reopen, and not reopenClass. reopenClass adds the method to the class itself, aka you could call Em.ArrayProxy.flatten()

http://emberjs.jsbin.com/OqUFObeg/1/edit

Sign up to request clarification or add additional context in comments.

5 Comments

I tried using this on ArrayProxy but the problem is that when I use .mapBy, I get an Array and it doesn't contain the flatten method. I've tried reopening the Ember.NativeArray class, but that doesn't really solve my problem.
It sounds like you just want to add flatten to the array prototype
Yeah, thats what I'm debating. In an Ember application, am I supposed to extend the Array prototype or work with the built-in Ember classes? I'm having trouble trying to see where it should go.
If you just plan on using it on ember types only it makes sense to just add it to ember types, but if you want it to work on plain ol' array's you'd want to extend the Array prototype
I would prefer to only have it on Ember types, but unfortunately the mapBy function in Ember returns a plain Array. From the documentation it seems like it should be returning an ArrayProxy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.