jakubrohleder / angular-jsonapi

Simple and lightweight, yet powerful ORM for your frontend that seamlessly integrates with your JsonAPI server.
http://jakubrohleder.github.io/angular-jsonapi/
GNU General Public License v3.0
96 stars 34 forks source link

Unlink method leaves relationships object empty #27

Open eduardmartinez opened 8 years ago

eduardmartinez commented 8 years ago

When I call the 'unlink' method of an element, it seems to set empty the whole element's relationship attribute when request is made. For example, having the following schemas:


// Factory schema

      {
          type: 'factories',
          attributes: {
            name: {}
          },
          include: {
            all: [
              'users',
            ],
            get: [
              'users',
            ],
          },
          relationships: {
            users: {
              included: false,
              model: 'users',
              reflection: 'factory',
              type: 'hasMany',
            },
        }

// User schema

      {
          type: 'users',
          attributes: {
            name: {}
          },
          include: {
            all: [
              'factory',
            ],
            get: [
              'factory',
            ],
          },
          relationships: {
            factory: {
              included: false,
              model: 'factories',
              reflection: 'users',
              type: 'hasOne',
            },
        },

Now, the 'unlink' of a 'factory' in a 'user', it'd look like this:

// Assuming 'factory' as a angular-jsonapi object
// with type: 'factories' and id: '2'
// and 'user' with type: 'users' and id: '1'
user.form.unlink('factory', factory);

When the user.save() is executed, the user's relationship attribute in the request is empty and doesn't remove the relationship:

// request content object
{
   "data":{
      "id":"1",
      "type":"users",
      "attributes":{
          "name": "eduard"
      },
      "relationships":{
        /**
          * should have:
          * factory: { data: null }
          * but it's empty
          */
      }
   }
}
  1. Am I doing anything wrong or is there a problem in the unlink method?
  2. In the docs it's not clear what 'target' represents on unlink method, I just assumed it was an angular-jsonapi object, but I don't know what it really means.

Thanks in advance.

jakubrohleder commented 8 years ago

You are doing everything right, I suppose it's a bug. I'll investigate it shortly and improve docs.

eduardmartinez commented 8 years ago

Thank you man! I'll be checking for another bug and I'll tell you. :smile: