Skip to content

Instantly share code, notes, and snippets.

@pwfisher
Last active August 2, 2016 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pwfisher/283716ad9898c7fc8101 to your computer and use it in GitHub Desktop.
Save pwfisher/283716ad9898c7fc8101 to your computer and use it in GitHub Desktop.
Implementation of the missing "Ember.computed.dynamicAlias"
import Ember from 'ember';
// @see http://emberjs.jsbin.com/beboga/edit?js,output
export default function dynamicAlias(target, keyKey) {
var prefix = target ? `${target}.` : '';
var dynamicAliasKey = `${prefix}${keyKey}_alias`;
return Ember.computed(function() {
var key = `${prefix}${this.get(keyKey)}`;
Ember.defineProperty(this, dynamicAliasKey, Ember.computed.alias(key));
return this.get(key);
}).property(keyKey, dynamicAliasKey);
}
@lcpriest
Copy link

Is this an alias or a oneWay?

  Ember.computed.dynamicAlias = function(target, keyKey) {
    const prefix = target ? `${target}.` : '';
    const dynamicAliasKey = `${prefix}${keyKey}_alias`;
    return Ember.computed(keyKey, dynamicAliasKey, function() {
      const key = `${prefix}${this.get(keyKey)}`;
      Ember.defineProperty(this, dynamicAliasKey, Ember.computed.alias(key));
      return this.get(key);
    });
  };

I have a dynamically generated form and the alias is used in this manner:
form-field.js

value: Ember.computed.dynamicAlias('model', 'field.value')

Where field.get('value') would be something like 'name', 'brand', 'productType' (attributes on the model).

And then
form-field.hbs

{{input value=value}}

But if the value on the model is updated, it doesn't trigger the value computed attribute to be updated.

@lcpriest
Copy link

@pwfisher any thoughts?

@webark
Copy link

webark commented Feb 17, 2016

curious about this as well.

@webark
Copy link

webark commented Feb 17, 2016

@lcpriest @pwfisher this is what I came up with

Ember.computed.dynamicAlias = function(target, aliasedKey) {
  return Ember.computed(aliasedKey, `${target}.${aliasedKey}_alias`, {
    get() {
      Ember.defineProperty(this, `${target}.${aliasedKey}_alias`, Ember.computed.alias(`${target}.${this.get(aliasedKey)}`));
      return this.get(`${target}.${this.get(aliasedKey)}`);
    },
    set(key, value) {
      return this.set(`${target}.${this.get(aliasedKey)}`, value);
    },
  });
};

any thoughts..?

@webark
Copy link

webark commented Feb 17, 2016

one last refinement

Ember.computed.dynamicAlias = function(target, aliasedKey) {
  const aliasedProperty = `${target}.${aliasedKey}_alias`;
  return Ember.computed(aliasedKey, aliasedProperty, {
    get() {
      Ember.defineProperty(this, aliasedProperty, Ember.computed.alias(`${target}.${this.get(aliasedKey)}`));
      return this.get(aliasedProperty);
    },
    set(key, value) {
      return this.set(aliasedProperty, value);
    },
  });
};

@webark
Copy link

webark commented Feb 17, 2016

weird.. was using this as

property: dynamicAlias('object', 'key'),

and was using an observer to check to make sure it was working, and once i removed the observer.. it broke..

  letsSee: Ember.observer('object.key_alias', function() {}),

not sure why I needed that..? maybe that alias need to be using somewhere..? not sure yet. will do more investigation.

@webark
Copy link

webark commented Feb 18, 2016

import Ember from 'ember';

export function dynamicAlias(target, aliasedKey) {
  const aliasedProperty = `${aliasedKey}_unique_alias`;
  return Ember.computed(aliasedKey, aliasedProperty, {
    get() {
      Ember.defineProperty(this, aliasedProperty, Ember.computed.alias(`${target}.${this.get(aliasedKey)}`));
      return this.get(aliasedProperty);
    },
    set(key, value) {
      return this.set(aliasedProperty, value);
    },
  });
}

@webark
Copy link

webark commented Feb 18, 2016

didn't need the target in the alias property

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment