1

I'm observing an Ember Data RecordArray like this:

myArray: function() {
  return MyRecord.find();
}.property(),

isDirtyChanged: function() {
  // Do something
}.observes('array.@each.isDirty');

I want to perform some operations on the particular record that fires the event. For example, if record 2 is modified:

MyRecord.find(2).set('my_property', 'some_value');

isDirtyChanged will get called and do some stuff with the record. How can I get a reference to the record that calls the observer, not just the array as a whole?

Andy G
  • 19,232
  • 5
  • 47
  • 69
Ryan Dao
  • 321
  • 1
  • 4
  • 13
  • It's not quite what you're asking, but [this question](http://stackoverflow.com/questions/17496446/how-can-an-observer-find-out-the-before-and-after-values-of-a-observed-property) has some info about using `addArrayObserver` which might help. – Jonathan Tran Aug 31 '13 at 20:05

1 Answers1

0

As far as I know there's no way to tell. The docs don't mention how to do this either. See here and here.

It does however mention this:

Note, @each.property observer is called per each add or replace of an element and it's not called with a specific enumeration item.

Normally if you add a obj and keyname parameters to the observer function and call obj.get(keyname) you'll get the object you're watching. But since the keyname here is array.@each.isDirty that doesn't work.

isDirtyChanged: function(obj, keyname) {
  obj is the targen here the object wacthing the array
  keyname is 'array.@each.isDirty'
  // Do something
}.observes('array.@each.isDirty');

I'd love to be proven wrong though!

Here's a fiddle try some stuff out.

albertjan
  • 7,739
  • 6
  • 44
  • 74
  • `obj` in this case is the target, i.e. the observing object. I've read through the code and it seems impossible to get the particular object calling the observer. – Ryan Dao Sep 01 '13 at 05:16