Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Adds documentation to CoreObject for native classes #17174

Merged
merged 1 commit into from
Jan 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions packages/@ember/-internals/runtime/lib/system/core_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,63 @@ function initialize(obj, properties) {
}

/**
`CoreObject` is the base class for all Ember constructs. It establishes a
class system based on Ember's Mixin system, and provides the basis for the
Ember Object Model. `CoreObject` should generally not be used directly,
instead you should use `EmberObject`.

## Usage

You can define a class by extending from `CoreObject` using the `extend`
method:

```js
const Person = CoreObject.extend({
name: 'Tomster',
});
```

For detailed usage, see the [Object Model](https://guides.emberjs.com/release/object-model/)
section of the guides.

## Usage with Native Classes

Native JavaScript `class` syntax can be used to extend from any `CoreObject`
based class:

```js
class Person extends CoreObject {
init() {
super.init(...arguments);
this.name = 'Tomster';
}
}
```

Some notes about `class` usage:

* `new` syntax is not currently supported with classes that extend from
`EmberObject` or `CoreObject`. You must continue to use the `create` method
when making new instances of classes, even if they are defined using native
class syntax. If you want to use `new` syntax, consider creating classes
which do _not_ extend from `EmberObject` or `CoreObject`. Ember features,
such as computed properties and decorators, will still work with base-less
classes.
* Instead of using `this._super()`, you must use standard `super` syntax in
native classes. See the [MDN docs on classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#Super_class_calls_with_super)
for more details.
* Native classes support using [constructors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#Constructor)
to set up newly-created instances. Ember uses these to, among other things,
support features that need to retrieve other entities by name, like Service
injection and `getOwner`. To ensure your custom instance setup logic takes
place after this important work is done, avoid using the `constructor` in
favor of `init`.
* Properties passed to `create` will be available on the instance by the time
`init` runs, so any code that requires these values should work at that
time.
* Using native classes, and switching back to the old Ember Object model is
fully supported.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supported/encouraged/discouraged?

export default class XFoo extends Ember.Component({

  // properties that I deliberately want to share between instances
  tagName: 'ul',

  fullName: computed('firstName', 'lastName', function() {
	return `${this.firstName} ${this.lastName}`;
  }),

  // immutable "defaults" for member data, deliberately shared across all instances
  firstName: '',  
  lastName: ''

}) {
 
  // standard ES6 class stuff

  logName() {
    console.log(this.fullName);
  }
}

Copy link
Contributor Author

@pzuraq pzuraq Nov 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discouraged, since it will be very difficult to codemod half-way states like this. It's much easier for us to go from a clean-slate to full ES classes in the codemod. The purpose of the ability to do this is so addon authors can fully adopt early on, and so we have a path forward for mixins.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be addressed in here, but in a linting rule. We shouldn't even suggest this is a possibility in our docs, ideally. We could cover it in guides though.


@class CoreObject
@public
*/
Expand Down