Skip to content

Instantly share code, notes, and snippets.

@ultimatemonty
Last active August 3, 2016 14:11
Show Gist options
  • Save ultimatemonty/b95622067c088b1424e28cbb5ba5720b to your computer and use it in GitHub Desktop.
Save ultimatemonty/b95622067c088b1424e28cbb5ba5720b to your computer and use it in GitHub Desktop.
// app/models/node.js
export default Model.extend({
sourceNodes: hasMany('node', { inverse: 'destinationNodes' }),
destinationNodes: hasMany('node', { inverse: 'sourceNodes' }),
destinationNodeOrder: attr('array') // custom transform, manually set in serializer
});
{
    "nodes": [{
        "id": 3,
        "destination_nodes": [{
            "node_id": 2,
            "sort_order": 0,
            "type": "NODE"
        }],
        "source_nodes": [{
            "node_id": 1,
            "sort_order": 0,
            "type": "NODE"
        }]
    }]
}
/**
 * Initial State
 * 
 * +---+
 * | 1 | (1:node)
 * +---+
 *   |
 *   | <-- need to insert new node here
 *   |
 * +---+
 * | 2 | (2:node)
 * +---+
 */
 
node1.get('destinationNodes').mapBy('id') -> [2]
/**
 * Create new node (API POST)
 * 
 * +---+
 * | 1 | (1:node)
 * +---+
 *   |
 * +---+
 * | 3 | (3:node)
 * +---+
 *   |
 * +---+
 * | 2 | (2:node)
 * +---+
 */

node1.get('destinationNodes').mapBy('id') -> [3]

Everything in Ember-Data looks good at this point. Creating the new node (node 3) involves manually updating the destinationNodeOrder property on the sourceNode (node1).

/**
 * Update node 3 (API PUT)
 * 
 * +---+
 * | 1 | (1:node)
 * +---+
 *   |
 * +---+
 * | 3 | (3:node)
 * +---+
 *   |
 * +---+
 * | 2 | (2:node)
 * +---+
 */

node1.get('destinationNodes').mapBy('id') -> [2,3]

The only call made when updating node3 is model.save() on that node. No manual relationship management is happening. Yet once the promise from node3.save() resolves node1 has reloaded the previously removed relationship out of nowhere. That relationship data is not contained in the API response from node3.save()

(╯°□°)╯︵ ┻━┻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment