gpt4 book ai didi

javascript - Backbone JS多级导航示例

转载 作者:行者123 更新时间:2023-11-29 17:30:01 25 4
gpt4 key购买 nike

我正在尝试构建一个可靠的 Backbone JS 实验,其中我有一个包含我的页面的本地 JSON 数据文件(我正在做的一个项目无论如何都有这种要求)。我已经编写了这个示例,所以我可以在页面数据中有无穷无尽的嵌套子页面。它似乎工作得很好。但是当涉及到 URL 时,我有点不知所措。

如何为这个多级导航示例提供完全动态的 URL?我的意思是,正确使用模型和集合的 url 属性为所有顶级和嵌套元素构建正确的 URL。有可能吗?我只是想不出该怎么做。

观看我现在所处位置的现场演示: http://littlejim.co.uk/code/backbone/multiple-level-navigation-experiment/

为了方便起见,源码在下面...

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Multiple Level Navigation Experiment</title>
<script type="text/javascript" src="../../media/scripts/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="../../media/scripts/underscore-min.js"></script>
<script type="text/javascript" src="../../media/scripts/backbone-min.js"></script>
<script type="text/javascript" src="application.js"></script>
<script type="text/javascript">
// wait for the DOM to load
$(document).ready(function() {
App.initialize();
});
</script>
</head>
<body>
<div id="header">
<h1>Multiple Level Navigation Experiment</h1>
<p>Want to get this page structure pulled from JSON locally and have a fully functional multiple level nested navigation with correct anchors.</p>
</div>
<div id="article">
<!-- dynamic content here -->
</div>
</body>
</html>

内容.json

{
"pages": [
{
"id": 1,
"title": "Home",
"slug": "home"
},
{
"id": 2,
"title": "Services",
"slug": "services",
"subpages": [
{
"id": 1,
"title": "Details",
"slug": "details",
"subpages": [
{
"id": 1,
"title": "This",
"slug": "this"
},
{
"id": 2,
"title": "That",
"slug": "that"
}
]
},
{
"id": 2,
"title": "Honest Service",
"slug": "honest-service"
},
{
"id": 3,
"title": "What We Do",
"slug": "what-we-do"
}
]
},
{
"id": 3,
"title": "Contact Us",
"slug": "contact-us"
}
]
}

应用程序.js

// global app class
window.App = {
Data: {},
Controller: {},
Model: {},
Collection: {},
View: {},
initialize : function () {
$.ajax({
url: "data/content.json",
dataType: "json",
success: function(json) {
App.Data.Pages = json.pages;
new App.Controller.Main();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log(errorThrown);
}
});
}
}

// main controller class
// when called it should have 'data' in JSON format passed to it
App.Controller.Main = Backbone.Controller.extend({
initialize: function() {
var pagesCollection = new App.Collection.Pages(App.Data.Pages);
var pagesView = new App.View.Pages({collection: pagesCollection});
$('#article').html(pagesView.render().el);
}
});

// pages model class
App.Model.Page = Backbone.Model.extend({
initialize: function() {
if (!_.isUndefined(this.get("subpages"))) {
this.subpages = new App.Collection.Pages(this.get("subpages"));
} // end if
this.view = new App.View.Page({model: this});
},
});

// page collection class
App.Collection.Pages = Backbone.Collection.extend({
model: App.Model.Page
});

// single page view class
App.View.Page = Backbone.View.extend({
tagName: "li",
initialize: function() {
_.bindAll(this, "render");
},
render: function() {
$(this.el).html(_.template("<%=title%>", {title: this.model.get("title")}));
return this;
}
});

// multiple pages view class
App.View.Pages = Backbone.View.extend({
tagName: "ul",
initialize: function() {
_.bindAll(this, "render");
},
render: function() {
var that = this;
this.collection.each(function(page) {
$(that.el).append(page.view.render().el);
if (!_.isUndefined(page.subpages)) {
var subpagesView = new App.View.Pages({collection: page.subpages});
$(that.el).append(subpagesView.render().el);
} // end if
});
return that;
}
});

我只需要关于如何正确处理 URL 的正确指导。我想要的想法是我可以为路由设置我的 Controller ,这样它就可以期待任何嵌套级别的任何页面。模型、集合和嵌套集合应该能够自行生成它们的 URL,但哈希 URL 必须反射(reflect)级别。

理想情况下,此导航会转到如下 URL:

...使用 content.json 数据中的“slug”的 URL。这些有意义吗?我对 Backbone JS 很陌生,只想把事情做好。谢谢,詹姆斯

最佳答案

这是我最喜欢的这个问题的解决方案:使用 PathJS !

关于javascript - Backbone JS多级导航示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5519619/

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