- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我想我有一个非常简单的问题,很难说出来,因此很难找到解决方案。设置:
paths = new PathCollection()
paths.fetch()
最佳答案
所以我写了几个关于使用 parse()
的答案和 set()
实例化和填充子模型和子集合(嵌套数据)。但是,我还没有看到一个真正全面的答案来整合我所看到的许多实践中的一些。当我写很多东西时,我倾向于漫无边际,所以我可能会离题一点,但这可能对遇到类似问题的人有用。
有几种方法可以做到这一点。使用 parse()
是一个。操纵set()
是另一个。在您的 initialize()
中实例化这些是另一个。在 Path 模型之外做这一切是另一回事(例如 path = new Path(); path.nodes = new NodeCollection();
等)
第二个考虑是这个。您是否希望节点和边集合成为模型属性?还是模型属性?
哦,这么多选择。很多自由,但有时(令我们沮丧)它使确定“正确方式”变得更加困难。
由于这种情况经常出现,我将写一篇长篇文章并逐一浏览这些内容。所以请耐心等待我继续更新这个答案。
在模型之外做 - 简单直接
当您只在特定模型或集合上需要它时,这通常是添加嵌套模型和集合的简单方法。
path = new PathModel();
path.nodes = new NodeCollection();
path.edge = new EdgeCollection();
// Continue to set up the nested data URL, etc.
initialize()
每个模型中的子模型/集合
initialize()
功能。
Path = Backbone.Model.extend({
initialize: function() {
this.nodes = new NodeCollection();
this.paths = new PathCollection();
// Maybe assign a proper url in relation to this Path model
// You might even set up a change:id listener to set the url when this
// model gets an id, assuming it doesn't have one at start.
this.nodes.url = this.id ? 'path/' + this.id + '/nodes' : undefined;
this.paths.url = this.id ? 'path/' + this.id + '/paths' : undefined;
}
});
path.nodes.fetch()
一样被获取它将路由到正确的 URL。十分简单。
parse()
用于实例化和设置子数据
fetch()
时才需要嵌套模型/集合发回这样的数据。这是使用
parse()
的情况可以派上用场。
parse()
的事是它需要任何 json 服务器响应,并且可以在将其传递给模型之前正确命名空间和处理它
set()
功能。因此,我们可以检查是否包含模型或集合原始数据并在将响应减少到父模型属性之前对其进行处理。
// Path model JSON example with nested collections JSON arrays
{
'name':'orange site',
'url':'orange.com',
'nodes':[
{'id':'1', 'nodeColor':'red'},
{'id':'2', 'nodeColor':'white'},
{'id':'3', 'nodeColor':'blue'}
],
'edge':[
{'id':'1', 'location':'north'},
{'id':'1', 'location':'south'},
{'id':'1', 'location':'east'}
]
}
parse()
Backbone 将吞并它,并为您的路径模型属性“节点”和“边缘”分配一个数组()数据(不是集合)。所以我们要确保我们的
parse()
适本地处理这个。
parse: function(response) {
// Check if response includes some nested collection data... our case 'nodes'
if (_.has(response, 'nodes')){
// Check if this model has a property called nodes
if (!_.has(this, 'nodes')) { // It does not...
// So instantiate a collection and pass in raw data
this.nodes = new NodeCollection(response.nodes);
} else {
// It does, so just reset the collection
this.nodes.reset(response.nodes);
}
// Assuming the fetch gets this model id
this.nodes.url = 'path/' + response.id + '/nodes'; // Set model relative URL
// Delete the nodes so it doesn't clutter our model attributes
delete response.nodes;
}
// Same for edge...
return response;
}
set()
处理你的子数据。经过多次来回,哪个更好,操纵
set()
或在
parse()
中进行我决定我喜欢使用
parse()
更多的。但我愿意接受其他人对此的看法。
set()
处理你的子数据
parse()
依赖于获取数据或使用选项
parse:true
将数据传递到集合中有些人觉得改换
set()
功能。同样,我不确定是否有正确的选择,但这是如何工作的。
set: function(attributes, options) {
// If we pass in nodes collection JSON array and this model has a nodes attribute
// Assume we already set it as a collection
if (_.has(attributes, 'nodes') && this.get("nodes")) {
this.get('nodes').reset(attributes.nodes);
delete attributes.nodes;
} else if (_.has(attributes, 'nodes') && !this.get('nodes')) {
this.set('nodes', new NodeCollection(attributes.nodes));
delete attributes.nodes;
}
return Backbone.Model.prototype.set.call(this, attributes, options);
}
reset()
它。如果我们有一个属性但它不是一个集合,我们就实例化它。在将子数据的 JSON 数组传递给原型(prototype)
set()
之前,确保将其正确转换为集合非常重要。 . Backbone,不会将 JSON 数组解释为集合,您只会得到一个直接的数组。
initialize()
当我知道某些东西总会有那些子模型/系列和
parse()
当情况仅需要
fetch()
上可能的嵌套数据时调用。
fetch()
路径模型或者甚至是一个 pathCollection...
pathCollection.fetch({parse:true})
有约定吗?也许也许不是。我喜欢根据我认为我将使用模型/集合的上下文使用多种方式的组合。
关于javascript - 主干模型的转换/初始化子模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12350218/
我开发了一个 jquery &backbone.js 网络应用程序。 一个组件有一个 html 表,该表后面是一个主干.js 集合。 此集合中的任何更改都应该导致 html 表的更新,所以我写 thi
我创建了一个由 HTTP Basic 保护的无状态 RESTful API。 在客户端,我将创建一个登录表单,向/auth-check 发送请求以检查凭据,并根据状态代码,将这些凭据存储在客户端并使用
我已经有一个具有经典结构的存储库 /branches /current /beta /tags /1.0 /2.0 /trunk 我想从当前的编辑器切换到 Eclipse,但我应该创建
我正在阅读 red bean SVN book ,它说要像这样布局主干、分支和标签文件夹: $ svn list file:///var/svn/multi-project-repo project-
当特定属性发生变化时,Backbone 模型触发自定义事件的好方法是什么? 到目前为止,这是我得到的最好的: var model = Backbone.Model.extend({ initi
我想避免在 Backbone.Collection 上的每个“添加删除重置”事件上重新渲染。 所以在 Backbone.View 中我可能有: this.listenTo(this.collectio
这一定是我没有正确理解的东西。 我有一个名为 Album 的 Backbone 模型: var Album = Backbone.Model.extend({ idAttribute: "_id",
假设我有一个具有一堆 bool 属性的 Backbone 模型: Car = Backbone.Model.extend({}); car_one = new Car({ a_c: true,
我从 collection.fetch() 调用中得到一个包含多个嵌套对象的对象。有没有办法解析这些子对象并将它们作为单独的模型传递到集合中? 这是我要返回的数据的示例。从服务器获取时,我想将它作为
有一个脚本(js),我希望在它上面运行,比如$(document).ready()。有一种方法可以在模板(例如 home_tpl.html)文件中编写脚本。但我确信这不是一个好方法。 我正在从主干 V
配合我正在尝试从主干集合中包含的所有模型中获取更大的属性。 举个例子: App.Models.Example = Backbone.Model.extend({ defaults: {
我遇到这样一种情况,我需要为一个人保存几个对象,但一次保存一个对象,并且在获取时,我将所有对象作为一个数组以及该人的信息。 这是模型的样子: //Model for one save { pe
我开始阅读一些 Backbone 教程,发现一件让我惊讶的事情。为什么模型是在 View 内部创建的?如果我想要一个模型有两个不同的 View (我认为 MVC 的用途)怎么办?假设我需要一个模型 C
我使用backbone.js 创建了一个小型网站。它托管在 IIS 7 中。 路由: routes: { "/": "index", "": "ind
我现在正在构建一个 View ,当我单击 .organization 链接时,我想触发我的编辑事件,但是单击此元素时,没有任何内容被触发,我无法理解为什么。 这是构建我的 View 的代码, App.
我有一些全局参数,我想在每次调用集合上的提取时发送这些参数...我的问题是我不想声明 data: { ... }每次我获取。 有没有一种方法可以在 Collection 本身内部提供默认参数,并可以添
Backbone 新手在这里。我想开始在 Web 应用程序上使用主干(后端是 Ruby on Rails),直到现在它的客户端功能还很少(一些用于执行 slideToggles 的 jquery,以及
我想知道是否有办法从其中一个模型中获取对集合的引用。例如,如果下面集合中的任何人以某种方式知道属于一个集合或多个集合。 Fiddle (function() { window.App = {
如何在集合获取中阻止模型的解析功能? $(function() { var Task = Backbone.Model.extend({ url : function(
我们一直在使用位于文件夹根目录中的存储库,一切都很好。然而,自从网站上线后,我们需要对一些工作进行分支,但由于缺少主干而无法这样做。 我的问题是,如何在当前目录结构中创建一个主干并将所有当前内容移动到
我是一名优秀的程序员,十分优秀!