Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between ember.js extend and create?

Tags:

ember.js

What's the difference between ember.js Object methods extend and create?

TLDR: You will find the answer in Ember guides: classes and instances.

Sometimes I see that one in the examples and sometimes the other. In particular, what's the difference between Em.Application.extend({}) and Em.Application.create({})?

If I declare my app like this, what does it mean?

Ember.Application.create({     MyController : Ember.ArrayController.extend({      }), }); 

How can I access the instance of MyController? Do I need to create it somehow? I need to push some data into it.

like image 768
Pavel S. Avatar asked Jul 25 '12 16:07

Pavel S.


People also ask

How to create a Object in Ember?

create (arguments) public Accepts either no arguments, or an object containing values to initialize the newly instantiated object with. import EmberObject from '@ember/object'; const Person = EmberObject. extend({ helloWorld() { alert(`Hi, my name is ${this. get('name')}`); } }); let tom = Person.

How do I create a class in Ember?

To define a new Ember class, call the extend() method on Ember. Object : App. Person = Ember.


1 Answers

The dead simple answer is that extend defines a new JS Class which inherits from the class you're extending, but does not create an instance of that class. create creates an instance of the class.

Em.Application is a particular case, because you're creating a namespace, not an object instance. I don't know when you'd ever want to extend Em.Application.

 App = Em.Application.create(); // I have a new Em.Application namespace App.A = Em.Object.extend(); // I have defined a new class, App.A,                              // which inherits from Em.Object var a = App.A.create();  // a now contains an instance of App.A. 

I'd suggest you read "Naming Conventions", too.

ETA: And "Understanding Ember Objects", as suggested in zaplitny's comment.

like image 200
pjmorse Avatar answered Oct 11 '22 12:10

pjmorse