在 Rails 服务器上运行,当我通过提交表单进行 ajax 调用时,表单经过 3 次。这张图显示了 3 次失败,但无论失败还是成功,它们都运行了 3 次。
当我带走:
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();
});
监视器.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.
});
};
...
});
应用程序.js
// Call requireJS and main.js
//= require ./monitor/lib/require
//= require ./monitor/main