Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js 恩伯斯和托阿斯特?兼容性问题?_Ember.js_Toastr - Fatal编程技术网

Ember.js 恩伯斯和托阿斯特?兼容性问题?

Ember.js 恩伯斯和托阿斯特?兼容性问题?,ember.js,toastr,Ember.js,Toastr,我有一个余烬应用程序,我想包括Toastr通知。我想使用这个库来提供一个cookie警告消息,然后我可以重新设置它的样式以适应网站主题的其余部分 但是,包含后,我无法使库正常工作,控制台报告正在执行get: 然而,我相信余烬独特的工作方式可能会给我带来问题?我发现了另一个SO问题,它将我引向了这一系列问题。我们在应用程序中使用Toastr with Ember,因此我将详细介绍我们是如何做到这一点的: 安装Toastr Toastr通过Bower安装,然后通过ember cli build.js

我有一个余烬应用程序,我想包括Toastr通知。我想使用这个库来提供一个cookie警告消息,然后我可以重新设置它的样式以适应网站主题的其余部分

但是,包含后,我无法使库正常工作,控制台报告正在执行get:
然而,我相信余烬独特的工作方式可能会给我带来问题?我发现了另一个SO问题,它将我引向了这一系列问题。

我们在应用程序中使用Toastr with Ember,因此我将详细介绍我们是如何做到这一点的:

安装Toastr Toastr通过Bower安装,然后通过
ember cli build.js
文件包含到构建中

// ember-cli-build.js

...

module.exports = function(defaults) {
  ...
  app.import('bower_components/toastr/toastr.js');
  ...
}
访问Toastr 与这样的库接口的“余烬方式”是将其包装在服务中。我们创建了一个非常简单的“通知”服务来包装Toastr库。这就是全部:

// app/services/notifications.js

/* global toastr */

import Ember from 'ember';

const { Service, on } = Ember;

export default Service.extend({

  initToaster: on('init', function() {
    toastr.options = {
      debug: false,
      positionClass: 'toast-top-right',
      onclick: null,
      fadeIn: 300,
      fadeOut: 1000,
      timeOut: 5000,
      extendedTimeOut: 1000
    };
  }),

  clear() {
    toastr.clear();
  },

  success(message, title) {
    toastr.success(message, title);
  },

  info(message, title) {
    toastr.info(message, title);
  },

  warning(message, title) {
    toastr.warning(message, title);
  },

  error(message, title) {
    toastr.error(message, title);
  }

});
使用服务 现在,您可以将您的服务注入到您想要使用Toastr的任何地方。例如,控制器可以这样使用它:

// some controller

import Ember from 'ember';

const { Controller, inject } = Ember;
const { service } = inject;


export default Controller.extend({

  notifications: service(),

  actions: {
    save() {
      this.get('model').save()
      .then(() => {
        this.get('notifications').success('Saved successfully!');
      })
      .catch(() => {
        this.get('notifications').error('There was an error!');
      });
    }
  }
});

我试图包含控制台输出的图像,但由于缺少代表而被阻止。图像没有代码那么有用,您如何包含它?你是怎么用的?您是否正在使用ember cli?您选择要求每个需要通知的路由/控制器/组件使用
ember.inject.service
而不是通过初始值设定项注入服务的原因是什么?这样做会更容易在测试期间删除通知服务,这是主要原因。我还更喜欢这样一个事实,即这个方法对于添加到类中的内容是明确的;比起需要记住在其他地方注射了什么,告诉它什么是可用的要容易得多。