Yes, itโs possible , and I created a working example for you, I think itโs just another way to organize your BackBone JS architecture, and I think thatโs a good idea.
explication
All models must have:
a render method that invokes the render method of its view. I am creating a base model that I use for the Bookshelf, Book, and Page models.
a initialize method, it creates a collection (books for bookshelves, pages for books ...)
Finally, I introduced the Bookstore model and did it!
code
Demo: http://jsfiddle.net/Atinux/DvbA3/show/
Try it in the console:
Bookstore.render() Bookstore.bookshelves.get(1).render() Bookstore.bookshelves.get(1).books.get(2).render() Bookstore.bookshelves.get(1).books.get(2).pages.at(0).render()
Code with comments is available here: http://jsfiddle.net/Atinux/DvbA3/
I think itโs best to understand how the code works. Feel free to ask me if you have problems in order to understand my code perfectly.
JSON data
JSON data should look like this:
var data = { bookshelves: [{ id: 1, name: 'Science', books: [{ id: 1, name: 'Abstract Algebra', pages: [ { content: 'Page 1 Abstract'}, { content: 'Page 2 Abstract'}, { content: 'Page 3 Abstract'} ] }, { id: 2, name: 'Chemistry and Technology of Fertilizers', pages: [ { content: 'Chemistry page 1' }, { content: 'Chemistry page 2' }, { content: 'Chemistry page 3' } ] } ] }, { id: 2, name: 'Psychology', books: [{ id: 1, name: 'How to Think Straight About Psychology', pages: [ { content: 'Psychology page 1' }, { content: 'Psychology page 2' }, ] } ] } ] };
source share