router.js Function not executed

2019-01-29 13:29发布

// 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
    };
});

1条回答
Bombasti
2楼-- · 2019-01-29 13:57

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 ...

查看更多
登录 后发表回答