So.. hmm you have a collection of collections for one. Your data is a collection that contains forms. Forms are a collection of entries.
your form collection should take in your data structure.
then it should make form models. those form models have entry collections.
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
models within EntryCollection
can access @collection.meta
You should note that this sort of nesting is prone to memory leaks, so if your page stays open for days, you should consider delete @parent
, etc, but you might not need to worry about it.
This is just a first shot at it, you could probably make improvements, but if you will be building this up more, you want to have a model for every object and a collection for every array. your _data is actually array.
you have
"_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"
}
]
},
it should be
"_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"
}
],
},
];
unless I am misunderstanding, but I think Another Form
is user generated.. there could be infinite forms right?
In regard to your follow up
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