9

Question Revised: 04/30/2015

I would like to define a new Ember.Object.extend() somewhere in my Ember CLI project, so that I can make new object instances from it using a line like myObject.create()

Is it correct to store these in a directory like app/custom-objects? and then i can do something like import myObject from './custom-objects/my-object'

Is an initializer required to register the object into ember, before I can import it?

Is my assumption here correct?

Grapho
  • 1,654
  • 15
  • 33
  • 1
    This might be what you're looking for http://stackoverflow.com/a/23729225/548568 – blessanm86 Jun 25 '14 at 23:36
  • @blessenm thanks for that... you know ember-cli removed the "initializers" directory from it's default installation, so i assumed (because I am still new) that perhaps it was a deprecated thing... but apparently it will still work if I add it in. I will give it a go :) – Grapho Jun 26 '14 at 15:02

1 Answers1

10

Initializer is not required.

You could define myObject:

//app/custom-objects/my-object.js
import Ember from 'ember';

export default Ember.Object.extend({
   name: 'DefaultName'
});

and then you could import it anywhere:

//app/pods/parent/children/controller.js
import Ember from 'ember';
import MyObject from '../../../custom-objects/my-object'

export default Ember.Controller.extend({
  //...
  myObj: MyObject.create({name: 'SuperName'}),
});

//app/pods/parent/children/template.hbs
name: {{myObj.name}} {{!--  => SuperName --}}
artych
  • 3,669
  • 1
  • 17
  • 30