我们正在使用Ember 1.10.1和Ember CLI 0.2.1,我正在尝试将Natero集成到我们的应用程序中,查看quickstart guide我想过使用初始化程序来执行此操作。它似乎适用于Chrome,但在其他浏览器中我收到错误:_na is undefined
。一般的想法是动态注入脚本并等待解析承诺以在窗口上设置_na
对象。
处理此事的更好方法是什么?
import Ember from 'ember';
/* jshint ignore:start */
import ENV from 'webapp/config/environment';
/* jshint ignore:end */
export function initialize(/* container, application */) {
/* jshint ignore:start */
let src = 'https://events.natero.com/scripts/natero_analytics.min.js';
let injectScript = function (src) {
return new Ember.RSVP.Promise(function (resolve) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = src;
script.onload = function () {
resolve();
};
document.getElementsByTagName('head')[0].appendChild(script);
});
};
injectScript(src).then(function() {
/*
* On each page that loads, initialize the natero analytics js library.
* The userId and accountId can be set here if known at initialization time.
* Once the userId/accountId are set they are stored in a cookie for later use,
* so that they only need to be set once per session.
*/
// http://apidocs.natero.com/quickstarte.html
// https://login.natero.com/itcenter.html
let authKey = ENV.natero.authKey,
apiKey = ENV.natero.apiKey,
settings = {
trackUnload: true,
debugUrl: "https://test.natero.com/v1/" + authKey + "/" + apiKey,
disableEventSend: false, // disable the sending of events
debug: false // console debug prints
};
if (['production', 'prd'].indexOf(ENV.environment) > -1) {
delete settings['debugUrl'];
}
window._na = new na(
apiKey,
authKey,
settings
);
});
/* jshint ignore:end */
Ember.Router.reopen({
notifyNatero: function () {
// https://github.com/emberjs/ember.js/issues/10180
let currentRoute = Webapp.__container__.lookup('controller:application').get('currentRouteName');
_na.setModuleId(currentRoute);
}.on('didTransition')
});
}
export default {
name: 'natero',
initialize: initialize
};
答案 0 :(得分:0)
我刚改变了订单。尝试一下
let injectScript = function (src) {
return new Ember.RSVP.Promise(function (resolve) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
document.getElementsByTagName('head')[0].appendChild(script);
script.onload = function () {
resolve();
};
script.src = src;
});
};
还有一个选项是你可以将它包含在index.html中并运行你当前的成功回调函数,即。,加载https://test.natero.com/v1/
脚本可以在Application beforeModel
hook中完成