lundi 16 novembre 2015

Accessing an Ember-cli model class from a service

I'm trying define a Person class within an Ember-cli project which I can then call from within a service. I've got a basic service working that updates when various inputs are changed but I can't reference the Person class at all. This is what I currently have:

# app/models/person.js 

import Ember from "ember";

var Person = Ember.Object.extend({
  helloWorld: function() {
    alert("Hi, my name is " + this.get('name'));
  }
});

export default Person; 

and in the router I've got:

# app/router.js

import Ember from 'ember';
import config from './config/environment';
import Person from './models/person';

var Router = Ember.Router.extend({
  location: config.locationType,
  model: function() {
    return Person.create({
      name: "Tom Dale"
    });
  }
});

I'm pretty sure it's referenced the "import Person" correctly because if it's wrong I see an error about the file not being found.

then in the service I've got:

# /app/services/calculator.js

import Ember from 'ember';

export default Ember.Service.extend({

  init: function () {

    // this gives "Person is not defined"
    const person = new Person();

    // this gives "Person is not defined"
    const person = Person.create({ name: "Tom Jones" });

    // this gives "store not defined" 
    const person = this.store.createRecord('person', { name: "Tom Jones" });

  },
});

Any pointers on how I can access the Person class from the service would be greatly appreciated. Thanks!

Aucun commentaire:

Enregistrer un commentaire