javascript - RequireJS - 在同一文件中调用函数

标签 javascript requirejs

我正在尝试从同一文件中的另一个函数调用一个函数作为回调。

下面的代码没有显示错误,但什么也不做 - 有没有不同/更好的方法来做到这一点?

我正在从 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/

相关文章:

node.js - 带有nodejs的Requirejs不告诉我错误的行号,只是错误

knockout.js - 在 Durandal/SPA 应用程序中配置 Knockout 验证

javascript - contextmenu/click 事件在 Firefox 中触发,只有 contextmenu 在 Webkit 中触发

javascript - RequireJS + NodeJS 返回 XMLHttpRequest 无法加载 'http' 没有 'Access-Control-Allow-Origin' header

javascript - 使用 javascript 检测鼠标滚轮的类型(平滑与缺口)

javascript - Play 框架外部 Javascript URL 语法

RequireJS - shim 中 "exports"属性的用途是什么

javascript - 自动完成 : how to pass an extra parameter to the response callback

javascript - Google Charts 水平条形图计算百分比

javascript - map 传单标记