简体   繁体   English

从服务访问Ember-cli模型类

[英]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. 我正在尝试在Ember-cli项目中定义一个Person类,然后可以从服务中调用它。 I've got a basic service working that updates when various inputs are changed but I can't reference the Person class at all. 我有一项基本服务,可以在更改各种输入时进行更新,但是我根本无法引用Person类。 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. 我将如何从服务访问Person类的任何指针将不胜感激。 Thanks! 谢谢!

I'll try to address what I think are some misunderstandings. 我将尝试解决我认为的一些误解。

First off, you have a Person class but it isn't a DS.Model so you won't be able to use it through Ember Data ( this.store ). 首先,您有一个Person类,但它不是DS.Model因此您将无法通过Ember Data( this.store )使用它。

Your router.js file is where you define your routes, as shown in the guides . 可以在router.js文件中定义路由, 如指南中所示 You seem to be confusing it with how to specify a route's model ? 您似乎将其与如何指定路线的模型混淆了? Pay close attention to the file paths in the code samples. 请密切注意代码示例中的文件路径。

As for the service, Person is not defined because unlike your router.js code, you are not importing Person into the module. 至于服务,未定义Person因为与您的router.js代码不同,您没有将Person导入模块。 Imports are per-module, so you also need import Person from './models/person'; 导入是按模块import Person from './models/person'; ,因此您还需要import Person from './models/person'; in your service. 为您服务。 Check jsmodules.io for more information. 检查jsmodules.io以获取更多信息。
Ember Data's store is injected into Route s and Controllers only, that's why this.store is undefined in the Service . 灰烬数据的商店被注入Route S和Controllers而已,这就是为什么this.store在未定义Service If you want to access the store, do the following: 如果要访问商店,请执行以下操作:

export default Ember.Service.extend({
  store: Ember.inject.service(),

  init() {
    const person = this.get('store').createRecord('person', { name: "Tom Jones" });
  }
});

Mind you, the above should not work with the code you posted as Person isn't a DS.Model 请注意,以上内容不适用于您因为Person不是DS.Model发布的代码

You need to import Person in every file that references it. 您需要在引用它的每个文件中导入Person。

Add import Person from './models/person'; import Person from './models/person';添加import Person from './models/person'; to your service class. 您的服务等级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM