我正在尝试从同一文件中的另一个函数调用一个函数作为回调。
下面的代码没有显示错误,但什么也不做 - 有没有不同/更好的方法来做到这一点?
我正在从 require 配置文件 (require-main.js) 调用第一个函数,并且工作正常
define(['require-main'], function() {
require(['getJSON'], function(getJSON) {
getJSON.stations();
});
});
getJSON.js
define(['jquery', 'domChange', 'setUp'], function($, domChange, setUp) {
var self = this;
return {
stations: function() {
$.get('/js/ajax/stations.json', function(sol) { /* local JSON */
tmv.stations = sol;
console.log(sol); /* this is fine */
self.lines; /* <-- call next function */
});
},
lines: function() {
console.log('foo'); /* NOT called */
$.get('/js/ajax/lines.json', function(lines) { /* local JSON */
/* do something */
});
}
}
});
I've seen this question but I can't work this way as the order is not predetermined
更新:如上所述,尝试将this
缓存到var中,但仍然没有乐趣
最佳答案
尝试对 getJSON.js 的内容进行此操作:
define(['jquery', 'domChange', 'setUp'], function($, domChange, setUp) {
var getJSON = {
stations: function() {
$.get('/js/ajax/stations.json', function(sol) { /* local JSON */
tmv.stations = sol;
console.log(sol); /* this is fine */
getJSON.lines(); /* <-- call next function */
});
},
lines: function() {
console.log('foo'); /* NOT called */
$.get('/js/ajax/lines.json', function(lines) { /* local JSON */
/* do something */
});
}
}
return getJSON;
});
关于javascript - RequireJS - 在同一文件中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38761327/