热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将服务注入烬对象[不是烬控制器]-InjectaserviceintoanEmberObject[notanEmberController]

ImtryingtoinjectanEmberserviceintoanEmberObjectbutkeepgettingthefollowingerror:我试图

I'm trying to inject an Ember service into an Ember Object but keep getting the following error:

我试图将一个烬服务注入到一个烬对象中,但仍然得到以下错误:

"Assertion Failed: Attempting to lookup an injected property on an
object without a container, ensure that the object was instantiated 
via a container."

My code looks essentially something like the following:

我的代码基本上如下所示:

const Model = Ember.Object.extend({
  store: Ember.inject.service(),

  destroyRecord() {...},

  serialize() {...},

  deserialize() {...},
});

let newModel = Model.create();
newModel.get('store');

Note: it does work if I inject the service into a Controller, but not an object. Haven't had any luck trying to figure out how to register the Object with the Ember container.

注意:如果我将服务注入控制器而不是对象,那么它就可以工作。还没有任何运气试图弄清楚如何将对象注册到Ember容器中。

2 个解决方案

#1


17  

It works for an Ember.Controller because Ember controls the lifecycle of the object. In short, when Ember needs an instance of a certain controller, it asks the container for one, and the container will provide the instance, initializing if necessary.

它适用于灰烬。控制器,因为余烬控制对象的生命周期。简而言之,当Ember需要一个特定控制器的实例时,它会向容器请求一个实例,容器将提供这个实例,如果需要的话初始化它。

What this implies is that for dependency injection to work, you would need to get a new instance of Model through the container. Assuming Ember 2.3 because of getOwner, and that this is somewhere inside the Ember application:

这意味着对于依赖项注入,您需要通过容器获得一个新的模型实例。假设烬2.3是由于getOwner的缘故,而且这是烬应用程序中的某个地方:

let owner = Ember.getOwner(this);
let newModel = owner.lookup('object:model');
newmodel.get('store');

You can consult the lookup documentation here.

您可以在这里查阅查找文档。

But how to register? Use an application initializer:

但是如何注册?使用一个应用程序初始化:

$ ember generate initializer register-model

Then, open up the generated initializer and, assuming that your file is in app/folder/model.js, put something like:

然后,打开生成的初始化器,假设文件在app/文件夹/模型中。js,把类似的:

import Model from 'app-name/folder/model';

export function initialize(application) {
  application.register('object:model', Model);
}

export default {
  name: 'register-model',
  initialize
};

You can consult the register documentation here.

您可以在这里查阅注册文档。

Hope this is helpful!

希望这是有帮助的!

#2


-1  

Well you need to passing in the container instance when you create a instance of your model. The container is accessible in the route, controllers, components with this.get('controller'). AFAIK basically anything created with the container gets the container property set. Thats why service injections work in controllers etc..

你需要在创建模型实例时传递容器实例。通过this.get('controller'),可以在路由、控制器和组件中访问容器。基本上,任何用容器创建的东西都会得到容器属性设置,这就是为什么服务注入在控制器中工作的原因。

So if you are creating the model in a route's method. The code will look like below

如果你在路径的方法中创建模型。代码如下所示

App.IndexRoute = Ember.Route.extend({
  model: function() {
    var newModel = Model.create({
      container: this.get('container')
    });    
    return newModel.get('test').getText();
  }
});

Here is a working demo.

这是一个可用的演示。


推荐阅读
  • vue使用
    关键词: ... [详细]
  • 本文介绍了在go语言中利用(*interface{})(nil)传递参数类型的原理及应用。通过分析Martini框架中的injector类型的声明,解释了values映射表的作用以及parent Injector的含义。同时,讨论了该技术在实际开发中的应用场景。 ... [详细]
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • Html5-Canvas实现简易的抽奖转盘效果
    本文介绍了如何使用Html5和Canvas标签来实现简易的抽奖转盘效果,同时使用了jQueryRotate.js旋转插件。文章中给出了主要的html和css代码,并展示了实现的基本效果。 ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • 本文介绍了H5游戏性能优化和调试技巧,包括从问题表象出发进行优化、排除外部问题导致的卡顿、帧率设定、减少drawcall的方法、UI优化和图集渲染等八个理念。对于游戏程序员来说,解决游戏性能问题是一个关键的任务,本文提供了一些有用的参考价值。摘要长度为183字。 ... [详细]
  • 怀疑是每次都在新建文件,具体代码如下 ... [详细]
  • 集合的遍历方式及其局限性
    本文介绍了Java中集合的遍历方式,重点介绍了for-each语句的用法和优势。同时指出了for-each语句无法引用数组或集合的索引的局限性。通过示例代码展示了for-each语句的使用方法,并提供了改写为for语句版本的方法。 ... [详细]
  • Python SQLAlchemy库的使用方法详解
    本文详细介绍了Python中使用SQLAlchemy库的方法。首先对SQLAlchemy进行了简介,包括其定义、适用的数据库类型等。然后讨论了SQLAlchemy提供的两种主要使用模式,即SQL表达式语言和ORM。针对不同的需求,给出了选择哪种模式的建议。最后,介绍了连接数据库的方法,包括创建SQLAlchemy引擎和执行SQL语句的接口。 ... [详细]
  • position属性absolute与relative的区别和用法详解
    本文详细解读了CSS中的position属性absolute和relative的区别和用法。通过解释绝对定位和相对定位的含义,以及配合TOP、RIGHT、BOTTOM、LEFT进行定位的方式,说明了它们的特性和能够实现的效果。同时指出了在网页居中时使用Absolute可能会出错的原因,即以浏览器左上角为原始点进行定位,不会随着分辨率的变化而变化位置。最后总结了一些使用这两个属性的技巧。 ... [详细]
  • Spring框架《一》简介
    Spring框架《一》1.Spring概述1.1简介1.2Spring模板二、IOC容器和Bean1.IOC和DI简介2.三种通过类型获取bean3.给bean的属性赋值3.1依赖 ... [详细]
  • 本文讨论了在ASP中创建RazorFunctions.cshtml文件时出现的问题,即ASP.global_asax不存在于命名空间ASP中。文章提供了解决该问题的代码示例,并详细解释了代码中涉及的关键概念,如HttpContext、Request和RouteData等。通过阅读本文,读者可以了解如何解决该问题并理解相关的ASP概念。 ... [详细]
  • SpringMVC工作流程概述
    SpringMVC工作流程概述 ... [详细]
author-avatar
手机用户2702935840
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有