2013-07-26 88 views
1

在Rails服務器上運行時,當我通過提交表單進行ajax調用時,表單經歷了3次。這張照片顯示3次失敗,但是他們是失敗還是成功,他們跑了3次。 enter image description hereRequireJS 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 

回答

0

通過移動的require.js和main.js了應用程序的調用。 js和我需要他們的頁面(好的電話是monitor.html.erb),我可以停止重複。

monitor.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' %> 
...