I'm having two RelationalModels, Student and School. School has many Students. All the data is to be stored in Backbone.localStorage
I need advice how the following should be done:
- I suppose I need two collections, one for each model, Student and School?
- I'm confused about how to store the data using localStorage. Should each collection store data in its own localStorage?
I understand you are using Backbone.localStorage to accomplish local storage with backbone.js.
As I see it, you would need two collections -
SchoolsCollection
andStudentsCollection
.SchoolsCollection
would implement the local-storage uplink:Within
SchoolsCollection
you would save models of typeSchoolModel
. Instances ofSchoolModel
would carry an attribute namedstudents
, beeing an instance ofStudentsCollection
.This would result in the localstorage look something similar like
As you can see,
StudentModel
looses its type on serialization. You have to implementparse()
inSchoolModel
to complement this effect:A single school model gets restored by
parse()
with a custom treatment of it's attributestudents
. A newStudentsCollection
is beeing created with the array of objects containing key-value-pairs describing each student's model.