this. Get( "object. Value") not working properly in ember.js

72 Views Asked by At

Sometimes this.get("object.value") not working properly in ember.js. But this.get("object").value is working. Any reason?

this.get("object").value = "values"   // WORKS ✅
this.get("object.value") = undefined // DOESN'T WORK 

1

There are 1 best solutions below

0
NullVoxPopuli On

Can you clarify "what isn't working"? what do you expect to happen? what does your data look like?

is object.value accidentally a key with a dot in it? For example:

{
  'object1.value': 1,
  // vs
  object2: {
    value: 2
  }
}

get with object1.value won't work because there is no key named object1 But object2.value will work because there is a key named object2

so, if you want to access properties with . in the key, you'll need a custom method that does that (likely with square bracket access).

for example:

import { get } from '@ember/object';

function customGet(obj, keyOrPropertyPath) {
  if (keyOrPropertyPath in obj) return obj[keyOrPropertyPath];

  return get(obj, keyOrPropertyPath);
}