1
在Rails服務器上運行時,當我通過提交表單進行ajax調用時,表單經歷了3次。這張照片顯示3次失敗,但是他們是失敗還是成功,他們跑了3次。 RequireJS Ajax被調用太多
當我帶走:
require(['/assets/monitor/monitor'], function(Monitor){
var monitor = new Monitor();
monitor.initial();
});
它停止工作完全。當我只帶走:
var monitor = new Monitor();
monitor.initial();
它使1 ajax呼叫。
爲什麼在我不調用監視器文件的實例時仍然會調用ajax?
main.js
requirejs.config({
baseUrl: './',
paths: {
'jquery': '/assets/monitor/lib/jquery-1.10.2.min',
'jquery-ui': '/assets/jquery-ui-1.10.2.custom.min',
'async': '/assets/monitor/lib/async',
'bootstrap': '/assets/monitor/lib/bootstrap',
'underscore': '/assets/monitor/lib/underscore',
'datatables': '/assets/monitor/lib/jquery.dataTables.min',
'DT_bootstrap': '/assets/monitor/lib/DT_bootstrap'
},
shim: {
'jquery': {
exports: '$'
},
'async': {
exports:'async'
},
'bootstrap': {
deps: ['jquery']
},
'underscore': {
exports: '_'
},
'datatables': {
deps: ['jquery']
},
'DT_bootstrap': {
deps: ['datatables']
}
}
});
require(['/assets/monitor/monitor'], function(Monitor){
var monitor = new Monitor();
monitor.initial();
});
monitor.js
define(['jquery', 'async', 'bootstrap'], function($, async, jQuery) {
var Monitor = function() {};
// Kicks off the file
Monitor.prototype.initial = function() {
var hostname = 'http://somehostthatworks';
var portNumber = 'some port that works';
var contentType = 'JSON';
var versionNumber = '1.1';
this.receiveMonitor(hostname, portNumber, contentType, versionNumber);
};
...
Monitor.prototype.recieveMonitor = function(hostname, portNumber, contentType, versionNumber) {
$("form").submit(function() {
//Some ajax that works when form is submitted.
});
};
...
});
的application.js
// Call requireJS and main.js
//= require ./monitor/lib/require
//= require ./monitor/main