Skip to content

Instantly share code, notes, and snippets.

@NullVoxPopuli
Last active February 1, 2021 18:28
Show Gist options
  • Save NullVoxPopuli/5434af0e43761f60799facbd7d815ff6 to your computer and use it in GitHub Desktop.
Save NullVoxPopuli/5434af0e43761f60799facbd7d815ff6 to your computer and use it in GitHub Desktop.
Enumerable Properties Demo
import Controller from '@ember/controller';
import EmberObject, { computed } from '@ember/object';
const MyObject = EmberObject.extend({
prop: computed(function() {}),
});
class Foo {
// getters are not enumerable!, but properties are
get prop() {
return;
}
}
export default class ApplicationController extends Controller {
appName = 'Ember Twiddle';
get info () {
let foo = MyObject.create();
return enumerableInfoFor(foo);
}
get nativeInfo() {
let foo = new Foo();
return enumerableInfoFor(foo);
}
}
function enumerableInfoFor(obj) {
let isEnumerable = false;
for (let key in obj) {
// the long inefficient way of doing getOwnPropertyDescriptor().enumerable
if (key === 'prop') isEnumerable = true;
}
return {
'myObject': obj,
'Object.keys': Object.keys(obj).join(', '),
'can be iterated over?': isEnumerable,
[`'prop' in obj`]: 'prop' in obj,
["Object.getOwnPropertyDescriptor"]: Object.getOwnPropertyDescriptor(obj.__proto__, 'prop'),
};
}
import { helper } from '@ember/component/helper';
export default helper(function stringify([data]/*, hash*/) {
return JSON.stringify(data, null, 2);
});
<h1>Welcome to {{this.appName}}</h1>
<br>
<br>
EmberObject:
<pre>
{{stringify this.info}}
</pre>
Native Class:
<pre>
{{stringify this.nativeInfo}}
</pre>
<br>
<br>
import Application from '../app';
import config from '../config/environment';
import { setApplication } from '@ember/test-helpers';
import { assign } from '@ember/polyfills';
import { start } from 'ember-qunit';
let attributes = {
rootElement: '#test-root',
autoboot: false
};
attributes = assign(attributes, config.APP);
let application = Application.create(attributes);
setApplication(application);
start();
{
"version": "0.17.1",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1"
},
"addons": {
"@glimmer/component": "1.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment