私は次のような状況です。
コード (3) はコード (1) では機能しますが、コード (2) では機能しません。
IMH、理由は、(2) の場合の backendController モジュールがコード (3) の後に非同期でロードされるためです。
コード (2) で動作するようにするには、コード (3) をどのように修正すればよいですか?
PS:
コード (2) を使用する理由は、ページの読み込みが速くなるためです。
(1)
define([
'../../utils/backendController'
], function (backendController) {
events: {
'submit form': 'submitForm'
},
submitForm: function (event) {
event.preventDefault();
// in this way I can spy backendController object
this.doSubmitForm(backendController);
// in this way I can not spy backendController object
// require(['../../utils/backendController', this.doSubmitForm);
// see the code (2)
}
});
(2)
define([], function () {
events: {
'submit form': 'submitForm'
},
submitForm: function (event) {
event.preventDefault();
// in this way I can not spy backendController object
require(['../../utils/backendController', this.doSubmitForm);
}
});
(3)
(function () {
define([
'backendController'
], function () {
describe('when submitting a form', function () {
beforeEach(function () {
var mySpy = spyOn(backendController, 'myCall').andCallThrough();
this.view.$el.find('form').submit();
})
it('backendController.myCall should be called', function () {
expect(backendController.myCall.toHaveBeenCalled());
});
});
});
}());