简体   繁体   English

Ember:从实用程序类访问ember数据“store”对象

[英]Ember: Access ember data 'store' object from utility class

I have a utility class for validating usernames in my ember application and have it setup as specified in the ember-cli docs. 我有一个实用程序类,用于验证我的ember应用程序中的用户名,并按照ember-cli文档中的说明进行设置。 I do client-side username validation in several places in my application (components and controllers) so I wanted to pull the validation logic out into a reusable method. 我在我的应用程序(组件和控制器)中的几个位置进行客户端用户名验证,因此我想将验证逻辑拉出到可重用的方法中。

The file is at /app/utils/username-validator.js and I can successfully include the file in my app by importing it like so: import usernameValidator from 'my-app/utils/username-validator'; 该文件位于/app/utils/username-validator.js ,我可以通过导入它成功地将文件包含在我的应用程序中: import usernameValidator from 'my-app/utils/username-validator';

This works great so far and I've used the pattern for several utility classes. 到目前为止,这很有用,我已经将模式用于了几个实用程序类。 The problem I'm running into now is that I'd like the username-validator method to include a check to see if the username already exists. 我现在遇到的问题是我想使用username-validator方法来检查用户名是否已经存在。

As I am using Ember-Data I'd like to check the Ember-Data store . 当我使用Ember-Data时,我想查看Ember-Data store By default, the store appears to only be accessible in controllers and routes. 默认情况下,商店似乎只能在控制器和路由中访问。 How can I make it accessible in my utility class? 如何在我的实用程序类中访问它? All the injection examples I've seen deal with injecting the store into other first class Ember objects like components. 我见过的所有注入示例都涉及将存储注入到其他第一类Ember对象(如组件)中。

Is it possible to inject the store into a simple utility class as well? 是否可以将商店注入一个简单的实用程序类?

Thank you! 谢谢!

I am using the following versions: 我使用以下版本:

Ember-cli v0.2.6
ember.debug.js:4888 DEBUG: -------------------------------
ember.debug.js:4888 DEBUG: Ember             : 1.12.0
ember.debug.js:4888 DEBUG: Ember Data        : 1.0.0-beta.18
ember.debug.js:4888 DEBUG: jQuery            : 1.11.3
ember.debug.js:4888 DEBUG: Ember Simple Auth : 0.8.0-beta.2
ember.debug.js:4888 DEBUG: -------------------------------

===== Updated with detailed solution based on answer from torazaburo ====== =====根据torazaburo的答案更新了详细的解决方案======

Creating a service works great. 创建服务非常有效。 Here is how I did it using ember-cli (v0.2.6) and ember v1.12.0 以下是我使用ember-cli(v0.2.6)和ember v1.12.0的方法

  1. Create your service inside of /app/services/<service-name>.js /app/services/<service-name>.js创建您的服务

The service blueprint will look like this (note the name of the service is based on the name of the file): 服务蓝图将如下所示(注意服务的名称基于文件的名称):

import Ember from "ember";

export default Ember.Service.extend({
   myFunction: function(){

   }
});
  1. Create an initializer for your service in /app/initializers/<service-name>.js which is used to inject your service into the different top level Ember objects (such as routes, controllers, components etc). /app/initializers/<service-name>.js为您的服务创建初始化/app/initializers/<service-name>.js ,用于将您的服务注入不同的顶级Ember对象(例如路由,控制器,组件等)。 Note that the file name of the initializer should match the file name of your service. 请注意,初始化程序的文件名应与服务的文件名匹配。

The blueprint for the initializer will look like this: 初始化程序的蓝图如下所示:

export function initialize (container, app) {
       // Your code here
}

export default {
  name: '<service-name>',
  initialize: initialize
};

To give a concrete example, lets say your service is called validator and contains a bunch of validation routines. 举一个具体的例子,假设你的服务被称为validator并包含一堆验证例程。 You want to inject the validator into all controllers, and you also want to inject the Ember Data store into the validator itself. 您希望将验证器注入所有控制器,并且还希望将Ember数据存储注入验证器本身。 You can do it like this: 你可以这样做:

export function initialize (container, app) {
  // Inject the Ember Data Store into our validator service
  app.inject('service:validator', 'store', 'store:main');

  // Inject the validator into all controllers and routes
  app.inject('controller', 'validator', 'service:validator');
  app.inject('route', 'validator', 'service:validator');
}

export default {
  name: 'validator',
  initialize: initialize
};

Make your utility into a "service", into which you can inject the store. 将您的实用程序变为“服务”,您可以将其注入商店。 Actually, it sounds like your utility should be a service anyway, even if it doesn't need the store. 实际上,听起来您的实用程序应该是一项服务,即使它不需要商店。 By making it a service, for instance, it becomes much easier to stub it out when writing tests. 例如,通过将其作为服务,在编写测试时将其删除会变得更加容易。 With a service, you need neither import anything nor do any global injections in initializers, you can simply say 使用服务,您既不需要导入任何内容也不需要在初始化程序中进行任何全局注入,您可以简单地说

export default Ember.Component.extend({

    myService: Ember.inject.service(), // inject services/my-service.js

    foo: function() {
         this.get('myService').api1(...);
    }

});

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

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