Ember.js: Add ability to know if proxied object is equal to an unproxied object.

Created on 23 Jan 2015  路  5Comments  路  Source: emberjs/ember.js

var object = getAProxiedObject();
Ember.isEqual(object, object.get('content')) // IMO should return true, actually returns false 

Something like that would make my life a whole lot easier.

Feature Needs Team Discussion

Most helpful comment

@rwjblue I'm using Ember 2.4 with Ember Data 2.4 and I'm having the same problem, comparing a model with a belongsTo relationship returns always false, because belongsTo returns a ObjectProxy

All 5 comments

:+1: I've come across this issue as well.

What's the best work around at this time? At the moment I'm checking if either of the objects are proxies and getting their content:

a = (a instanceof Ember.ObjectProxy) ? a.get("content") : a;
b = (b instanceof Ember.ObjectProxy) ? b.get("content") : b;

Ember.isEqual(a, b);

Yes, i'm using your workaround too, like this:

// app/utils/proxy-helpers.js
import Ember from 'ember';

export function isProxy(obj) {
  // note that I added the Ember.ArrayProxy, as arrProxy intanceof Ember.ObjectProxy => false
  return (obj instanceof Ember.ObjectProxy) || (obj instanceof Ember.ArrayProxy);
}

export function withoutProxies(obj) {
  while (isProxy(obj)) {
    obj = obj.get('content');
  }

  return obj;
}

export function isEqualProxied(a, b) {
  return Ember.isEqual(withoutProxies(a), withoutProxies(b));
}

But the real issue is that when is for instance have an (Ember) array of proxied objects and I have a reference to an object without proxies and I want to know at which index it is in the array. This won't work reliably.

arrayWithProxies.indexOf(myObjectWithoutProxy); // -1 if the object is in the array, but with a proxy.

This also relates to an Ember.Select issue. (although I can't find the issue right now)
I created the following function to workaround this for myself.

// app/utils/index-of-proxied.js;
import {
  withoutProxies,
  isEqualProxied
} from '../utils/proxy-helpers';

function _itemAt(arr, index) {
  if (typeof arr.objectAt === 'function') {
    return arr.objectAt(index);
  } else {
    return arr[i];
  }
}

export default function(arr, object, fromIndex) {
  if (fromIndex == null) {
    fromIndex = 0;
  } else if (fromIndex < 0) {
    fromIndex = Math.max(0, this.length + fromIndex);
  }

  arr = withoutProxies(arr);

  if (arr == null) { return -1; }
  for (var i = fromIndex; i < arr.length; i++) {
    var item = _itemAt(arr, i);
    if (isEqualProxied(item, object)) {
      return i;
    }
  }

  return -1;
}

Updated: some more findings...

For the most part, dealing with proxied things is deprecated in 1.13 and removed in 2.0.0.

I'm going to close, but if this is still a problem in 2.0.0+ I am happy to reopen.

@rwjblue I'm using Ember 2.4 with Ember Data 2.4 and I'm having the same problem, comparing a model with a belongsTo relationship returns always false, because belongsTo returns a ObjectProxy

I just encountered this as well, and I was surprised to learn that ObjectProxy doesn't implement a reasonable default for isEqual. Two proxy objects of the same class with the same content should be considered equal IMO.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fivetanley picture fivetanley  路  44Comments

chancancode picture chancancode  路  44Comments

QuantumKing picture QuantumKing  路  33Comments

GendelfLugansk picture GendelfLugansk  路  43Comments

wycats picture wycats  路  47Comments