我知道问题标题有点令人困惑,所以请原谅 - 希望我能解释我的问题。
我有这样的数据结构:
{
"_data": {
"Test Alignment Form": [
{
"review_form": "Test Alignment Form",
"rvee_uid": "52",
"firstName": "Joe",
"lastName": "Bloggs",
"status": "NOT_STARTED",
"status_clean": "Not started"
},
{
"review_form": "Test Alignment Form",
"rvee_uid": "54",
"firstName": "Steve",
"lastName": "Stevenson",
"status": "NOT_STARTED",
"status_clean": "Not started"
},
{
"review_form": "Test Alignment Form",
"rvee_uid": "13",
"firstName": "Anne",
"lastName": "Boleyn",
"status": "COMPLETED",
"status_clean": "Completed"
}
],
"Another Form": [
{
"review_form": "Another Form",
"rvee_uid": "10",
"firstName": "Luther",
"lastName": "Vandross",
"status": "NEVER_TOO_MUCH",
"status_clean": "Never too much, never too much... duh duh duh"
},
{
"review_form": "Another Form",
"rvee_uid": "54",
"firstName": "Steve",
"lastName": "Stevenson",
"status": "NOT_STARTED",
"status_clean": "Not started"
},
{
"review_form": "Another Form",
"rvee_uid": "13",
"firstName": "Anne",
"lastName": "Boleyn",
"status": "COMPLETED",
"status_clean": "Completed"
}
]
},
"_meta": {
"generated": 1397642209,
"length": 62,
"duration": 3,
"author": 0
}
}
我的代码目前是这样的:
window.app = app = window.app or
models: {}
collections: {}
views: {}
routes: {}
init: () ->
console.log 'Initialised app.'
app._app = new app.views.table()
#-----------------------------------------------------------------------------#
app.models.form = Backbone.RelationalModel.extend
defaults:
firstName: ""
lastName: ""
review_form: ""
rvee_uid: "0"
status: ""
status_clean: "UNKNOWN"
initialize: () ->
console.log "Initialised model 'form'."
app.collections.mainCollection = Backbone.Collection.extend
model: app.models.form
url: "data/data.json"
initialize: (models, options) ->
console.log "Initialised collection 'mainCollection'."
@options = options
@fetch reset: true
@on "reset", () ->
console.log "Collection reset!"
app.views.tableItem = Backbone.View.extend
tagName: 'li'
template: _.template $('#row').html()
initialize: () ->
console.log "Initialised view 'tableItem'."
render: () ->
console.log "Rendered view 'tableItem'."
@$el.html @template @model.toJSON()
@
app.views.table = Backbone.View.extend
el: '#table'
initialize: (data) ->
console.log "Initialised view 'table'."
@collection = new app.collections.mainCollection data
@listenTo @collection, 'reset', () ->
@render()
render: () ->
console.log "Rendered view 'table'."
@$el.empty()
console.log @collection.models
_.each @collection.models, (_item) =>
@renderItem(_item)
renderItem: (_item) ->
item = new app.views.tableItem
model: _item
@$el.append item.render().el
#-----------------------------------------------------------------------------#
app.init()
(请记住,我有一个更大的可用版本,但具有非嵌套结构,因此这仅用于沙盒)。
无论如何,假设我有另一个 View ,其中包含一个 select
下拉输入,由“Test Alignment Form”和“Another Form”填充。当我选择一个时,我希望返回的模型是该表单的子项。因此,相当于解析出 @['_data']['Test Alignment Form']
。我也希望能够访问“_meta”对象,因为我希望能够在另一个 View 中打印出生成的日期,例如。有谁知道实现这一目标的任何最佳实践?我一直在拔头发!
谢谢:)
最佳答案
所以.. 嗯,你有一个集合的集合。您的数据是一个包含表单的集合。表单是条目的集合。
您的表单集合应该采用您的数据结构。
然后它应该制作表单模型。这些表单模型具有条目集合。
FormCollection
parse: (res) ->
{@_meta} = res
_.map res._data, (data, formName) =>
{formName, data} # we have 2 attributes on the form model
FormModel
initialize: ->
@on 'reset', ->
# Since each form also has a collection of entries, we create a collection
@entries ?= new EntryCollection
@entries.parent = this # this circular dependency will create memory leaks
@entries.reset @get('data'), parse: true #remember data is an array
EntryCollection
parse: (res) ->
@meta = parent.collection._meta
res
EntryModel
EntryCollection
中的模型可以访问 @collection.meta
你应该注意这种嵌套很容易出现内存泄漏,所以如果你的页面保持打开几天,你应该考虑delete @parent
等,但你可能不需要担心
这只是第一次尝试,您可能会做出改进,但如果您要构建更多,您需要为每个对象创建一个模型并为每个数组创建一个集合。你的 _data 实际上是数组。
你有
"_data": {
"Test Alignment Form": [
{
"review_form": "Test Alignment Form",
"rvee_uid": "52",
"firstName": "Joe",
"lastName": "Bloggs",
"status": "NOT_STARTED",
"status_clean": "Not started"
},
{
"review_form": "Test Alignment Form",
"rvee_uid": "54",
"firstName": "Steve",
"lastName": "Stevenson",
"status": "NOT_STARTED",
"status_clean": "Not started"
},
{
"review_form": "Test Alignment Form",
"rvee_uid": "13",
"firstName": "Anne",
"lastName": "Boleyn",
"status": "COMPLETED",
"status_clean": "Completed"
}
],
"Another Form": [
{
"review_form": "Another Form",
"rvee_uid": "10",
"firstName": "Luther",
"lastName": "Vandross",
"status": "NEVER_TOO_MUCH",
"status_clean": "Never too much, never too much... duh duh duh"
},
{
"review_form": "Another Form",
"rvee_uid": "54",
"firstName": "Steve",
"lastName": "Stevenson",
"status": "NOT_STARTED",
"status_clean": "Not started"
},
{
"review_form": "Another Form",
"rvee_uid": "13",
"firstName": "Anne",
"lastName": "Boleyn",
"status": "COMPLETED",
"status_clean": "Completed"
}
]
},
应该是
"_data": [
{
name: "Test Alignment Form",
contents: [
{
"review_form": "Test Alignment Form",
"rvee_uid": "52",
"firstName": "Joe",
"lastName": "Bloggs",
"status": "NOT_STARTED",
"status_clean": "Not started"
},
{
"review_form": "Test Alignment Form",
"rvee_uid": "54",
"firstName": "Steve",
"lastName": "Stevenson",
"status": "NOT_STARTED",
"status_clean": "Not started"
},
{
"review_form": "Test Alignment Form",
"rvee_uid": "13",
"firstName": "Anne",
"lastName": "Boleyn",
"status": "COMPLETED",
"status_clean": "Completed"
}
],
},
{
name: "Another Form",
contents: [
{
"review_form": "Another Form",
"rvee_uid": "10",
"firstName": "Luther",
"lastName": "Vandross",
"status": "NEVER_TOO_MUCH",
"status_clean": "Never too much, never too much... duh duh duh"
},
{
"review_form": "Another Form",
"rvee_uid": "54",
"firstName": "Steve",
"lastName": "Stevenson",
"status": "NOT_STARTED",
"status_clean": "Not started"
},
{
"review_form": "Another Form",
"rvee_uid": "13",
"firstName": "Anne",
"lastName": "Boleyn",
"status": "COMPLETED",
"status_clean": "Completed"
}
],
},
];
除非我误解了,但我认为 Another Form
是用户生成的..可能有无限的形式吧?
关于您的跟进
FormView
render: ->
@$el.empty().append formTemplate(this)
@model.entries.each (model) =>
# if you need more speed or re-render often, you can cache these views later
entryView = new EntryView {model}
# assumes you have an entries-container in your form template
@$('.entries-container').append entryView.render().el
关于javascript - 在嵌套 JSON (BackboneJS) 中按父节点动态过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23222643/