2016-11-02 78 views
0

我們將Ember 1.10.1與Ember CLI 0.2.1一起使用,我目前正在嘗試將Natero整合到我們的應用程序中,查看quickstart guide我考慮過使用初始化程序來執行此操作。它似乎與Chrome一起工作,但與其他瀏覽器,我得到的錯誤:_na is undefined。總的想法是動態注入腳本並等待解決的承諾,以在窗口上設置對象_na將Natero與ember-cli集成

什麼是更好的方法來處理這個問題?

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

你爲什麼不只需在index.html中包含腳本? –

回答

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/腳本可以在應用程序來完成beforeModel