router.js 函数未执行

<块引用>

//文件名:router.jsconsole.log('测试路线');定义(['jquery','下划线','骨干','视图/工作/列表'], 函数($, _, Backbone, JobListView){var AppRouter = Backbone.Router.extend({路线:{//定义一些 URL 路由'/dalo​​/jobs': 'showJobs',//默认'*actions': 'defaultAction'}});var 初始化 = 函数(){var app_router = 新的 AppRouter;app_router.on('route:showJobs', function(){//在我们通过依赖数组加载的模块上调用渲染//'视图/工作/列表'console.log('显示作业路径');var jobListView = new JobListView();jobListView.render();});app_router.on('defaultAction', function(actions){//我们没有匹配的路由,让我们记录一下 URL 是什么console.log('No route:', actions);});Backbone.history.start();};返回 {初始化:初始化};});

我的 main.js 的一部分,我没有使用 NEW,因为它给出了一个问题,说它不是一个函数,不确定它是否与上面的错误有关

require(['app'], function(AppView){AppView.initialize();});

我在 Router.initialize() 之后做了一个 console.Log;在 app.js ,它可以显示.我还在这个应用程序 router.js 上面一直做了一个控制台日志,它也显示,除此之外,它没有显示函数内部的任何内容.

控制台只显示 2 个控制台日志(在 Route.Initialize 之后 & 在 router.js 定义之前

有什么建议吗?我正在使用 http://backbonetutorials.com/organizing-backbone-using-modules/

我的 App.js

define(['jquery','下划线','骨干','router',//请求 router.js], 函数($, _, Backbone, Router){var 初始化 = 函数(){//传入我们的路由器模块并调用它的初始化函数路由器初始化();console.log('路由器初始化');}返回 {初始化:初始化};});

解决方案

可能您使用的是 非 AMD 版本的 Backbone.js 和 Underscore.js.

通过这种方式,您必须将所谓的垫片"添加到您的主/配置文件中.

<块引用>

shim:为较旧的传统浏览器全局变量"脚本配置依赖项、导出和自定义初始化,这些脚本不使用 define() 来声明依赖项并设置模块值.http://requirejs.org/docs/api.html#config-shim

正如您所看到的,设置依赖项并导出您的库,以便您在脚本中使用它.

因此,在您的主/配置文件中,在路径之后尝试添加此垫片部分:

路径:{...},垫片:{'主干':{deps: ['jquery','underscore'],出口:骨干"}}

现在我想你可以继续......

// Filename: router.js
console.log('TEST ROUTE');
define([
    'jquery',
    'underscore',
    'backbone',
    'views/jobs/list'
], function($, _, Backbone, JobListView){
    var AppRouter = Backbone.Router.extend({
        routes: {
            // Define some URL routes
            '/dalo/jobs': 'showJobs',

            // Default
            '*actions': 'defaultAction'
        }
    });

    var initialize = function(){
        var app_router = new AppRouter;
        app_router.on('route:showJobs', function(){
            // Call render on the module we loaded in via the dependency array
            // 'views/jobs/list'
            console.log('Show Job Route');
            var jobListView = new JobListView();
            jobListView.render();

        });

        app_router.on('defaultAction', function(actions){
            // We have no matching route, lets just log what the URL was
            console.log('No route:', actions);
        });
        Backbone.history.start();
    };
    return {
        initialize: initialize
    };
});

Part of my main.js , i didnt use NEW because it gave issues saying it's not a function not sure if it's related to the error above

require(['app'], function(AppView){
    AppView.initialize();
});

I did a console.Log after Router.initialize(); at app.js , it can show. I also did a console log all the way above in this app router.js it's also showing, other than that , it doesnt show anything inside the function.

The console is only showing that 2 console Log (After Route.Initialize & Before router.js define

Any advice? I'm using http://backbonetutorials.com/organizing-backbone-using-modules/

My App.js

define([
    'jquery',
    'underscore',
    'backbone',
    'router', // Request router.js
], function($, _, Backbone, Router){
    var initialize = function(){
        // Pass in our Router module and call it's initialize function
        Router.initialize();
        console.log('Router Initialized');
    }

    return {
        initialize: initialize
    };
});

解决方案

Probably you're using a non-AMD version of Backbone.js and Underscore.js.

This way you've to add what it's called a "shim" to your main/config file.

shim: Configure the dependencies, exports, and custom initialization for older, traditional "browser globals" scripts that do not use define() to declare the dependencies and set a module value. http://requirejs.org/docs/api.html#config-shim

As you can see this set dependencies and export your lib in order to let you using it in your scripts.

So, in your main/config file, after the paths try adding this shim part:

paths: {
    ...
},
shim: {
    'backbone': {
        deps: ['jquery','underscore'],
        exports: 'Backbone'
    }
}   

Now I suppose you could proceed ...

相关文章