L
L
likejavascript2013-12-25 21:03:27
backbone.js
likejavascript, 2013-12-25 21:03:27

What is the correct way to interact with models and collections in Backbone?

There is a task to implement tables for ordering goods on Backbone. It seems that the task is not difficult, but the problem comes when it is necessary to interact with them. Here is an example structure of what is planned to be done:

Document
     |- OrderList   
            |- Order
            |      |- OrderTablesList
            |              |- OrderTable
            |- Order
                  |- OrderTablesList
                          |- OrderTable

A brief description of what is what:
Document (Model) - document data
OrderList (Collection) - list of orders
Order (Model) - order data
OrderTablesList (Collection) - list of order tables
OrderTable (Model) - order table model.
Here are the questions that interest me:
1. How to correctly structure all these entities in Backbone models and collections, and is it possible to store both the model and the collection in the view at the same time (for example, to implement Order and OrderTablesList).
2. How to correctly send events up the chain (something like a bubbling), for example, if I need to send some event from the TableTable to Order and somehow react to it, but only to the parent Order.
Thank you!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
Konstantin Kitmanov, 2013-12-25
@k12th

1. You can transfer many collections and models in a view, but in fact it is more convenient to work when the view is responsible for displaying one business entity. To solve this, nested views are used.
2. When instantiating a TableTable, a reference to the parent must be passed to it.
In order to somehow tame such a branching structure (by the way, is it really needed?), You can look for some plugin for nested models and collections, for example, this one .

A
Artem, 2013-12-26
@artemf

For such purposes, you can make an event manager and through it organize interaction between views.
Actually, the manager itself:
This is how you can trigger an event:
This is how you can subscribe:
A detailed example can be found here
lostechies.com/derickbailey/2011/07/19/references-...

A
Artem, 2013-12-27
@artemf

@likejavascript Your OrderTable and Order are models, right? Can you provide a code example of what you want to link?

N
Nikolai Markov, 2014-06-03
@manameiz

I recommend Marionettejs. The library contains many best practices of the backbone world and is its logical continuation. Work with nested views is conveniently organized, there are ready-made solutions for rendering collections and hierarchies (just what you need).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question