- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试构建一个可靠的 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/
我有一个主 View 负责呈现其他 View ...... 这是完整的代码 (1) (2) (3)。 当我第一次加载 View (View1、View2、View3)时,一切正常。 然后,如果我尝试重
我正在第一次尝试使用 Backbone.Marionette,并想知道当一个简单的 Backbone.View 就足够了时,是否有任何理由使用 Backbone.Marionette.ItemView
我正在尝试将 Backbone Validation 与 Backbone Stickit 结合使用,我希望在用户输入时一次验证一个属性。但是,当用户输入一个值时,模型上的所有属性都会得到验证,而不仅
我在结合使用 T. Hedersen 的 backbone.validation 插件 ( https://github.com/thedersen/backbone.validation ) 和 D
在下面编辑了这个 在下图中,我有两个主要区域。 左边的用户列表:allusersRegion 另一个用于显示布局的右侧,其中包含在 allusersRegion 中单击的用户的唯一属性和用户的文章列表
如果您单击链接,我的主干路由器工作正常,但在尝试直接访问 URL 或刷新页面时不起作用。 路由器 var app = app || {}; var appRouter = Backbone.Route
我想创建一个 Backbone 模型并将另一个模型的集合存储到它的属性中。所以,有父子模型。每个父级都有一个或多个子级存储在其属性中的数组中。 JSON 将是这样的。 Parent = { n
我正在使用 Backbone 和木偶, 我想对我的收藏和渲染 View 进行排序。 但是发生了一些奇怪的事情。 '/api/note/getList' ,它返回(并在集合被 View 初始化时调用)
我有一个相当通用的模型,并且正在收集该模型(请参见下文),作为一系列观点的基础。在几种 View 上,选择一个模型会生成操作(通过“selected”属性),我需要能够仅在客户端跟踪选择。 但是,似乎
这是一个单一的问题,但我对这是否是一个好习惯这一事实深有感触。 基本上,假设我们有这个微不足道的场景: (function(){ window.App = { Models: {},
我正在使用 Signalr 集线器订阅服务器上的事件。将什么事件分派(dispatch)到集线器,它成功地将项目添加到 Marionette CollectionView。反过来,这会呈现到表格中。
我正在使用 require js 和 Backbone 为 android 开发应用程序。我必须通过 touchend 事件将从集合中获取的模型传递给路由器。我该怎么做? define(["jquer
我有一个 Backbone 集合。如何对集合进行切片,或者至少将列表截断为特定长度? 最佳答案 假设您已经定义并初始化了您的集合,并且您想要改变集合(就地更改),您必须执行以下操作: collecti
我有一个集合,其中有一个添加模型时会触发的事件。我已经在文档中阅读了应该具有options参数但无法获取的参数。 我基本上想在集合中找到模型所在的索引。 在我的收藏夹中,我有这个。 initi
从集合中删除模型时,如何获取模型的索引。 在下面的代码中有一个回调函数 doSomething(){} ,它在 remove 被触发时被调用,我希望索引在那里。 Backbone 文档说“移除前模型的
在项目中同时使用Polymer和Backbone是否有任何限制? 我想不出什么具体的东西,但我想我会在匆忙之前先问清楚。有人可能已经同他们一起工作过,并且发现了某种不兼容之处。 欢迎任何反馈和经验分享
我正在使用bone.js编写应用程序,并在页面之间进行动画处理(有点像iPhone风格的ui)。因此,当您单击按钮时,下一页将从右侧滑入,而单击后退按钮将使下一页从左侧滑入。我希望能够使用路由器对浏览
我刚刚开始学习ribs.js。我在理解如何/何时使用模型和集合时遇到问题。我在网上找到了几个教程,每个教程都使用不同的方法来构建应用程序。在某些情况下,是从Collection对象的REST API中
我有一个由 REST API 给出的模型,其中一个名称的属性是这样的: defaults: { ... "user-name" : "", ... } 然后当我尝试通过这种方式在模板中呈现它时: U
我有一个 Backbone 应用程序,它从我使用 Backbone 的代码中的不同位置不确定地获取错误 Backbone is not defined 。有时它会先加载然后加载网站,有时则不会。我将以
我是一名优秀的程序员,十分优秀!