dop83362 2013-11-16 10:13
浏览 84
已采纳

router.js函数未执行

// 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条回答 默认 最新

  • doujia9204 2013-11-16 11:26
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突