我正在尝试掌握Backbone,并要求JS使用marionette来实现它的一些优秀功能。然而,我发现该应用程序在视图中可用时存在一些问题:
main.js
require(['application'], function(app){
app.start();
});application.js
define([
'marionette',
'router'
], function(marionette, Router){
"use strict";
var app = new marionette.Application();
app.addRegions({
header : 'header',
main : '#main'
});
app.addInitializer(function(){
this.router = new Router();
});
return app;
});dashboard.js
define([
'application',
'underscore',
'backbone',
], function(app, _, Backbone) {
var DashboardView = Backbone.View.extend({
initialize: function() {
console.log(app);
$('a').click(function(e){
e.preventDefault();
app.router.navigate("claims", {
trigger: true
});
});
},
});
return DashboardView;
});我在控制台日志中收到未定义的?这个应用程序应该使用requirejs模块吗?
编辑:使用require更新
require.config({
paths: {
'jquery' : '../vendors/jquery-1.8.2',
'underscore' : '../vendors/underscore',
'text' : '../vendors/text',
'json2' : '../vendors/json2',
'backbone' : '../vendors/backbone',
'marionette' : '../vendors/plugins/backbone.marionette',
'paginator' : '../vendors/plugins/backbone.paginator',
'relational' : '../vendors/plugins/backbone.relational',
'moment' : '../vendors/moment',
'bootstrap' : '../vendors/bootstrap',
'datepicker' : '../vendors/plugins/bootstrap.datepicker',
'templates' : 'templates/'
},
shim: {
backbone: {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
},
marionette : {
exports : 'Backbone.Marionette',
deps : ['backbone']
},
paginator: {
deps: [
'backbone',
'underscore',
'jquery'
],
exports: 'Backbone.Paginator'
},
relational: ['backbone'],
underscore: {
'exports': '_'
},
bootstrap: {
deps: ['jquery'],
exports: "bootstrap"
},
datepicker: {
deps: ['jquery','bootstrap'],
exports: "datepicker"
},
moment: {
exports: 'moment'
}
}
});
require(['application'], function(app){
app.start();
});路由器
define([
'views/claims/PaginatedList',
'views/dashboard/Dashboard'
], function(PaginatedClaimListView, DashboardView){
var Router = Backbone.Router.extend({
routes: {
"": "index",
"claims": "claims"
},
initialize: function(){
Backbone.history.start({
pushState: true,
root: '/app_dev.php/hera'
});
},
index: function(){
var dashboard = new DashboardView();
},
claims: function(){
var claimListView = new PaginatedClaimListView();
}
});
return Router;
});发布于 2012-12-16 20:54:07
我想我已经弄明白了,即使我不是百分之百确定为什么。
问题出在您的Router定义中。将引用Application的视图放在那里可以使路由器在main.js中调用app.start()之前启动。实际上,如果您在main.js中放入一个console.log(app),您会注意到它是在dashboard.js中的那个之后调用的。
下面是我解决这个问题的方法:
define(['backbone'], function(Backbone){
var Router = Backbone.Router.extend({
routes: {
"": "index",
"claims": "claims"
},
initialize: function(){
Backbone.history.start({
pushState: true,
root: '/app_dev.php/hera'
});
},
index: function(){
require(['views/dashboard/Dashboard'],function(DashboardView){
var dashboard = new DashboardView();
});
},
claims: function(){
require(['views/claims/PaginatedList'],function(PaginatedClaimListView){
var claimListView = new PaginatedClaimListView();
});
}
});
return Router;
});我不确定是否有更好的解决方案来保持你的视图在你的路由器中定义,不管怎样,这是有效的,并将使你的路由器更轻,特别是当你的视图数量增长的时候…
发布于 2012-12-15 07:55:38
我发现,通常当我将不应该设置为undefined的Require.js模块设置为undefined时,这是因为循环依赖。例如,假设"vent“依赖于"dashboard";你会有:vent需要dashboard需要application需要vent ...
当这种情况发生时,Require的响应本质上只是选择其中一个文件并使其工作,但循环依赖中涉及的任何其他文件都是作为undefined传入的。
您可以通过删除导入来测试此理论,以查看这是否修复了您的undefined。或者,需要的人员可以下载一个名为xrayquire的工具,它可以帮助查找循环依赖项。
发布于 2013-07-06 01:21:10
您是否必须通过app.router访问路由器?您能不能只使用仪表板来要求路由器本身,并直接访问它?只要应用程序已经执行了(在这一点上,它应该已经执行了),那么你就不需要通过应用程序访问它。我认为你的问题绝对是一个复杂的循环依赖。
https://stackoverflow.com/questions/13887790
复制相似问题