documentcloud/backbone — Issue #926: Need more guidance with nested models and localstorage
Author: meecect
Created: 2012-02-02 01:47:39+00:00
State: open

I know there are many issues and forum threads on this issue already, but I just need a little more guidance. There seem to be a variety of approaches but I keep hitting roadblocks with each one, and I haven't found yet a complete example that uses nested models and collections along with localstorage.

I am trying to create a simple app that has Project models and Task Models. Each Project can have more than one Task, and there can be more than one Project. Additionally, I will eventually need Tasks to potentially contain other tasks, but I can leave that aside for now.

If anyone can provide a full working (simple) example for any of the approaches that are out there it would go a long way at helping newbs like me. Most approaches, like the example in the FAQ and backbone-relational all rely, at some point, on having a collection embedded in a model in some way, and they set the url on that collection to retrieve the right models. This doesn't seem to play nicely with localstorage.

Additionally, the FAQ example does something like this:

```javascript
var Mailbox = Backbone.Model.extend({

initialize: function() {
this.messages = new Messages;
this.messages.url = '/mailbox/' + this.id + '/messages';
this.messages.on("reset", this.updateCounts);
},

...

});

var Inbox = new Mailbox;

// And then, when the Inbox is opened:

Inbox.messages.fetch();
```

So, instead of setting the collection URL, I initially tried to create a new Store with a custom name like 'taskStore-projectid'. That mirrors the example where it sets the collection url to something with 'this.id' in it. First, I had trouble figuring out if I could create the collection store in my model initialize function, or would I need to pass the id to the collection initialize function and have it create the store. Next, I discovered, that in my model initialize function, 'this.id' is still undefined. I _think_ this is because in the example, the Mailbox is created with 'var Inbox= new Mailbox', and in my app, the Mailbox equivalent (Project for me) is created from my Mailbox(Project) Collections 'create' function.

But I don't know. I just am too new to backbone to know. Heck, I don't even know if it will work to create the store that way even if I just gave it a random string. Plus, I'm not relishing the idea of having a giant list of stores like that, as it seems horribly un-normalized.

The backbone-relational approach seems good too, but it also hinges on setting a url parameter that is meant to only return the proper models, so I have the same issue there.

The FAQ goes on to list a few other approaches like:

* Mirror an SQL database's structure, or the structure of a NoSQL database.
* Use models with arrays of "foreign key" ids, and join to top level collections (a-la tables).
* For associations that are numerous, use a range of ids instead of an explicit list.
* Avoid ids, and use direct references, creating a partial object graph representing your data set.
* Lazily load joined models from the server, or lazily deserialize nested models from JSON documents.

Some of which sound good, and some sound pretty similar to each other, but none of which have some good examples. I'd love to see some of those bullet points fleshed out.

I guess, ideally, I'd like something that allowed me to use just two stores, one for projects and one for tasks, and a performant way to easily get the tasks for each project, and still be able to leverage collections and all they bring.

I know I could work around this by setting a project id in each task model, like the 'foreign key' approach above, but is that going to cause any other issues? Is it backboney enough?

anyway, thanks for letting me vent a bit. Any help will be appreciated. ;-)