dhruvaray / backbone-associations

Create object hierarchies with Backbone models; Respond to hierarchy changes using regular Backbone events.
http://dhruvaray.github.io/backbone-associations/
MIT License
492 stars 75 forks source link

Problem with the relational terminology #152

Closed artemrizhov closed 8 years ago

artemrizhov commented 9 years ago

Hi! I'm looking for a Backbone plugin that adds a method to work with relations. And I was surprised and confused when reading your project documentation. Could you please help me to understand how this works?

The project home page says:

Backbone plugin to specify 1:1 & 1:N associations between Backbone Models.

You provide two types of relations: Backbone.One and Backbone.Many, so I assume they are 1:1 and 1:N respectively. Let's see the examples:

var Employee = Backbone.AssociatedModel.extend({
    relations: [
        {
            type: Backbone.One, //nature of the relationship
            key: 'manager', // attribute of Employee
            relatedModel: 'Employee' //AssociatedModel for attribute key
        }
    ],

This probably means each employee is associated with one manager. This is a kind of foreign key in relational database, and corresponds to one-to-many relation in the relational model, because each manager is associated with many employees. Is this what you mean?

Let's see the next example:

var Project = Backbone.AssociatedModel.extend({
  relations: [
      {
        type: Backbone.Many,//nature of the relation
        key: 'locations', //attribute of Project
        relatedModel:Location //AssociatedModel for attribute key
      }
  ],

This probably means each project is associated with many locations. I'm not sure why your project has many locations and what use case you demonstrate here, but this is probably what you mean because the key is locations, not location, and you also do this:

project1.get("locations").add(loc1);

This probably means we can add more locations to the project. While we also probably can add same locations to another projects. If this is true then this means each project can be associated with many locations, and each locations can be associated with many projects. This is called many-to-many in the relational model.

If I understand correctly, Backbone.One is one-to-many (or many-to-one to be more correct) relation, and Backbone.Many is many-to-many relation. This is usually specified as 1:N and N:M.

Am I right or wrong regarding to the meaning of your examples? If I'm right then don't you think your naming is a bit confusing? Why not to use traditional notation?

vinnymac commented 9 years ago

The relations are a bit different. 1:1 is referring to employee:manager, and 1:N is referring to project:locations. In Backbone Associations when you want to create a one to one relation, you create a Backbone.One relation on a single model which relates to a another model. If you then decide to create the opposite relation on that model, you have created a cyclical relationship. Locations don't have a single project, a project has many locations, and so if you add locations to a project, the collection of locations on that project are updated, which create the Backbone.Many relationship. Locations do not have many projects.

Backbone Associations was created with one to one and one to many in mind. You can create many to many relations, but they won't be very efficient, and you will most likely be better off coming up with your own solution.

So to answer your question, the naming is not confusing it is describing its behavior. Why do you feel that example represents a many to many relationship?

artemrizhov commented 9 years ago

Well, maybe the examples is not clear enough for me. Project and locations make as much sense as "foo" and "bar". I don't understand why project has many locations, why location can't have many project, and how many employes the manager has. Maybe the examples would be much more clear if you use City, Country and ZipCode, i.e. something that all of us can understand without doubts.

artemrizhov commented 9 years ago

What is the relation between employe and manager in your examples?

dhruvaray commented 8 years ago

@artemrizhov : Please look at the ER diagram provided with the help for the relationships http://dhruvaray.github.io/backbone-associations/docs/img/cd_example.png

artemrizhov commented 8 years ago

@dhruvaray Thanks for the diagram. It is helpful. If so, I have to say that imo the example provided is very confusing. I'm still in doubt.

  1. Project may have many locations, but location can not have many projects. How do you imagine this? What kind of projects the example is about? What is the location?
  2. How do you locate a department in few locations?
  3. Individual manager for each employee? Are you kidding? :) Let's better provide many dedicated managers to each employee ;)
  4. Each department has one employee, really? What is your business about?
  5. Who are those dependents? I thought employee is that man who is dependent. Is there even more dependent people?
dhruvaray commented 8 years ago

I think you are reading the diagram wrong...