Last active
August 2, 2016 20:20
-
-
Save pwfisher/283716ad9898c7fc8101 to your computer and use it in GitHub Desktop.
Implementation of the missing "Ember.computed.dynamicAlias"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
@pwfisher any thoughts?
curious about this as well.
@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..?
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);
},
});
};
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.
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);
},
});
}
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
Is this an alias or a oneWay?
I have a dynamically generated form and the alias is used in this manner:
form-field.js
Where field.get('value') would be something like 'name', 'brand', 'productType' (attributes on the model).
And then
form-field.hbs
But if the value on the model is updated, it doesn't trigger the
value
computed attribute to be updated.