router.js功能不执行 [英] router.js Function not executed

查看:163
本文介绍了router.js功能不执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  //文件名:router.js
的console.log('考试路线');
定义([
    jQuery的,
    下划线,
    '骨干',
    意见/职位/列表
]函数($,_,骨干,JobListView){
    VAR AppRouter = Backbone.Router.extend({
        路线:{
            //定义某些URL路径
            '/达洛/工作:showJobs',            //默认
            *行动':'DEFAULTACTION
        }
    });    VAR初始化函数=(){
        VAR app_router =新AppRouter;
        app_router.on('路线:showJobs',函数(){
            //调用渲染,我们通过依赖阵列装入模块
            //'意见/职位/列表
            的console.log('查看工作路线');
            VAR jobListView =新JobListView();
            jobListView.render();        });        app_router.on('DEFAULTACTION',函数(动作){
            //我们没有匹配的路由,让刚刚登录什么网址是
            的console.log(没有路线:'行动);
        });
        Backbone.history.start();
    };
    返回{
        初始化:初始化
    };
});


我main.js的一部分,我没有使用新的,因为它给了问题,说如果它涉及到上述错误

这不是一个函数不知道

 规定(['应用'],功能(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

 定义([
    jQuery的,
    下划线,
    '骨干',
    路由器,//请求router.js
]函数($,_,骨干路由器){
    VAR初始化函数=(){
        //通过我们的路由器模块,并调用它的初始化函数
        Router.initialize();
        的console.log('路由器初始化');
    }    返回{
        初始化:初始化
    };
});


也许你正在使用的非AMD Backbone.js的版本和Underscore.js的。

您已经通过这种方式增加它之所以被称为垫片你的主/ config文件。


  

垫片:配置的依赖,出口和自定义对老年人,传统的浏览器全局初始化脚本不使用定义()声明的依赖关系和设置模块值。
  
http://requirejs.org/docs/api.html#config-shim


正如你所看到的这一套依赖和导出LIB为了让你在你的脚本中使用它。

所以,在主/ config文件中,以后的路径尝试添加该垫片部分:

 路径:{
    ...
},
垫片:{
    骨干:{
        DEPS:['jQuery的','底线'],
        出口:骨干
    }
}

现在我想你可以前往...

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

这篇关于router.js功能不执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆