2

I am trying to get this code working in ember but it keeps giving me "Uncaught TypeError: Object # has no method 'get'" on line this.get('element_id')[item.id] = true; why can't it access the element_id hash?

function() { return Ember.ArrayProxy.extend({ box: null, content: [], element_id: {}, init: function() { this._super(); var items = this.get( 'box' ).getData(); if ( items.get( 'length' ) ) { this.set( '[]', items ); }; // Initialize the hash items.forEach(function(item) { this.get('element_id')[item.id] = true; }); } }); } 

);

1 Answer 1

3

When using the forEach method, you can pass a target object that will be set as this on the context, as explained in the documentation:

Note that in addition to a callback, you can also pass an optional target object that will be set as "this" on the context. This is a good way to give your iterator function access to the current object.

So your code should be now:

items.forEach(function(item) { this.get('element_id')[item.id] = true; }, this); 
Sign up to request clarification or add additional context in comments.

4 Comments

quick related question, when you use getJSON, also this doesn't work, right now I am declaring outside of getson a var backup = this; Is there a similar way?
AFAIK, no, you effectively have to use a closure for that. A common naming convention in js is var that = this;.
do you know is this very bad for application memory consumption is this duplicating big chunks of data or is it using proper pointers?
again, I'm not 100% sure, but normally, it just keeps the same object and make it accessible through the "nested" function. So I think you can do it safely.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.