問題描述
//文件名:router.jsconsole.log('測(cè)試路線');定義(['jquery','下劃線','骨干','視圖/工作/列表'], 函數(shù)($, _, Backbone, JobListView){var AppRouter = Backbone.Router.extend({路線:{//定義一些 URL 路由'/dalo??/jobs': 'showJobs',//默認(rèn)'*actions': 'defaultAction'}});var 初始化 = 函數(shù)(){var app_router = 新的 AppRouter;app_router.on('route:showJobs', function(){//在我們通過依賴數(shù)組加載的模塊上調(diào)用渲染//'視圖/工作/列表'console.log('顯示作業(yè)路徑');var jobListView = new JobListView();jobListView.render();});app_router.on('defaultAction', function(actions){//我們沒有匹配的路由,讓我們記錄一下 URL 是什么console.log('No route:', actions);});Backbone.history.start();};返回 {初始化:初始化};});
我的 main.js 的一部分,我沒有使用 NEW,因?yàn)樗o出了一個(gè)問題,說它不是一個(gè)函數(shù),不確定它是否與上面的錯(cuò)誤有關(guān)
require(['app'], function(AppView){AppView.initialize();});
我在 Router.initialize() 之后做了一個(gè) console.Log;在 app.js ,它可以顯示.我還在這個(gè)應(yīng)用程序 router.js 上面一直做了一個(gè)控制臺(tái)日志,它也顯示,除此之外,它沒有顯示函數(shù)內(nèi)部的任何內(nèi)容.
控制臺(tái)只顯示 2 個(gè)控制臺(tái)日志(在 Route.Initialize 之后 & 在 router.js 定義之前
有什么建議嗎?我正在使用 http://backbonetutorials.com/organizing-backbone-using-modules/一個(gè)>
我的 App.js
define(['jquery','下劃線','骨干','router',//請(qǐng)求 router.js], 函數(shù)($, _, Backbone, Router){var 初始化 = 函數(shù)(){//傳入我們的路由器模塊并調(diào)用它的初始化函數(shù)路由器初始化();console.log('路由器初始化');}返回 {初始化:初始化};});
可能您使用的是 非 AMD 版本的 Backbone.js 和 Underscore.js.
通過這種方式,您必須將所謂的墊片"添加到您的主/配置文件中.
<塊引用>shim:為較舊的傳統(tǒng)瀏覽器全局變量"腳本配置依賴項(xiàng)、導(dǎo)出和自定義初始化,這些腳本不使用 define() 來聲明依賴項(xiàng)并設(shè)置模塊值.http://requirejs.org/docs/api.html#config-shim
正如您所看到的,設(shè)置依賴項(xiàng)并導(dǎo)出您的庫,以便您在腳本中使用它.
因此,在您的主/配置文件中,在路徑之后嘗試添加此墊片部分:
路徑:{...},墊片:{'主干':{deps: ['jquery','underscore'],出口:骨干"}}
現(xiàn)在我想你可以繼續(xù)......
// 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 ...
這篇關(guān)于router.js 函數(shù)未執(zhí)行的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!