1

在 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
4

1 回答 1

0

通过将 require.js 和 main.js 的调用从 application.js 移到我需要它们的页面中(好吧调用是 monitor.html.erb),我能够停止重复。

监视器.html.erb

...
<% @stylesheetIncludes= ["bootstrap.min", "jquery-ui-1.10.2.custom.min", "monitor", "jquery.dataTables.min"] %>
<% @javascriptIncludes= ["monitor/lib/require", "monitor/main"] %>
<!-- Monitor Service -->
<%= render 'monitor/monitorService' %>
...
于 2013-07-26T02:14:47.980 回答