Last active
February 1, 2021 18:28
-
-
Save NullVoxPopuli/5434af0e43761f60799facbd7d815ff6 to your computer and use it in GitHub Desktop.
Enumerable Properties Demo
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 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'), | |
}; | |
} |
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 { helper } from '@ember/component/helper'; | |
export default helper(function stringify([data]/*, hash*/) { | |
return JSON.stringify(data, null, 2); | |
}); |
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 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(); |
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
{ | |
"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