gpt4 book ai didi

javascript - BackboneJS - 导航菜单

转载 作者:行者123 更新时间:2023-11-29 19:44:50 26 4
gpt4 key购买 nike

我想在我的页面顶部创建一个固定位置的导航菜单,它应该在我点击相关链接后显示不同的 View 。我有一个 index.php 文件、一个 mainpage.js 文件和用于集合、模型、 View 、路由器和 html 模板的单独文件夹。显示菜单和 View 到目前为止有效,只是导航不起作用,例如我不知道如何让它正常工作。导航栏应该始终可见,#mainContent Div 应该显示不同的 View ...

我的 index.php(正文部分):

<body> 
<div id="container">
<div id="mainMenu"></div>
<div id="mainContent"></div>
</div>
<script data-main="js/config" src="js/lib/requirejs/require.js"></script>
<script> require(['config'], function(config) {
require(['app/mainpage']);
});
</script>
</body>

所以我创建了一个 MainMenuCollection,json 文件:

[
{
"name":"HOME",
"id":"1",
"url":"home"
},
{
"name":"FIRST PAGE",
"id":"2",
"url":"firstpage"
},
{
"name":"SECOND PAGE",
"id":"3",
"url":"secondpage"
},
{
"name":"THIRD PAGE",
"id":"4",
"url":"thirdpage"
},
{
"name":"ABOUT",
"id":"5",
"url":"about"
},
{
"name":"CONTACT",
"id":"6",
"url":"contact"
}
]

我的收藏文件:

define([
"backbone",
"models/MainMenuModel"
],

function(Backbone, MainMenuModel) {
var MainMenuCollection = Backbone.Collection.extend({
model: MainMenuModel,

url: "MainMenuCollection.json"
});

return MainMenuCollection;
});

我的菜单模型:

define([
"jquery",
"backbone"
],

function($, Backbone) {
var MainMenuModel = Backbone.Model.extend({
urlRoot: "/MainMenuCollection.json"
});

return MainMenuModel;
});

这是我的 Menu.html 文件:

<ul>
{{#each mainmenu}}
<li>
<a href="{{this.url}}">{{this.name}}</a>
</li>
{{/each}}
</ul>

菜单 View 本身:

define(['backbone','handlebars', 'text!templates/MainMenu.html'],

function(Backbone,Handlebars, Template) {


'use strict';

var MainMenuView = Backbone.View.extend({

template: Handlebars.compile(Template),

events: {
'click a':'mainMenu_event'
},

mainMenu_event: function(e) {
var id = $(e.currentTarget).attr('href');
Backbone.history.navigate(id, {trigger:true});
},

initialize: function () {
},

render: function() {
//this.model.toJSON())
$(this.el).html(this.template({mainmenu:this.collection.toJSON()}));
return this;
}

});

return MainMenuView;

}
);

最后是路由器:

define([
'backbone',
//
'views/MainMenu',
'views/Home',
'views/Firstpage',
'views/Secondpage',
'views/Thirdpage',
'views/About',
'views/Contact',
'collections/MainMenuCollection'
],

function(
Backbone,
MainMenuView,
HomeView,
FirstpageView,
SecondpageView,
ThirdpageView,
AboutView,
ContactView,
MainMenuCollection
) {

'use strict';

var Router = Backbone.Router.extend({

routes: {
'': 'index',
'pages/firstpage' : 'firstpage',
'pages/secondpage' : 'secondpage'
},

//Initializing the application
initialize: function () {
var self = this;

//Collections
this.mainMenuCollection = new MainMenuCollection();

//Views
this.mainMenuView = new MainMenuView({el:'#mainMenu', collection:this.mainMenuCollection});
this.homeView = new HomeView({el:'#mainContent'});
//this.menuView = new Menu({el:'.menu'});

self.homeView.render();
//self.gridView.render();

this.mainMenuCollection.fetch({success: function(collection) {
self.mainMenuView.collection=collection;
self.mainMenuView.render();
}});


Backbone.history.start({
pushState: false
});

},

//Default route.
index: function () {
var self = this;

},

firstpage: function() {
this.firstpageView = new FirstpageView({el:'#mainContent'});
self.firstpageView.render();
},

secondpage: function() {
this.secondpageView = new SecondpageView({el:'#mainContent'});
self.secondpageView.render();
}

});

return Router;
}
);

我的问题是,我不知道如何浏览不同的页面/ View ,所以如果有人可以帮助我吗?如何设置路由器?帮助会很棒! :-)

最佳答案

[更新]好的,我自己得到了:

因为我的路由器中有“页面”作为引用:

        routes: {
'': 'index',
'pages/firstpage' : 'firstpage',
'pages/secondpage' : 'secondpage'
},

我所要做的就是在 html 文件中,其中 anchor 是:

<a href="#pages/{{this.url}}">{{this.name}}</a>

关于javascript - BackboneJS - 导航菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20457102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com