Note that there are some explanatory texts on larger screens.

plurals
  1. POBackboneJS: How to cascade Model#destroy?
    primarykey
    data
    text
    <p>I have a relational DB (MySQL) at the backend and cool BackboneJS at the frontend. I have several tables (models in BackboneJS jargon) that are interconnected through foreign keys.</p> <p><strong>PROBLEM</strong></p> <p>How do I destroy a specific model, such that destruction cascades to all child models?</p> <p><strong>EXAMPLE</strong></p> <p>Consider this quick schema:</p> <pre><code>PERSON id name PHONE_NUMBER id person_id number EMAIL_ADDRESS id person_id email </code></pre> <p>Consider that we have a person "Jack" stored in DB with some phone numbers and email addresses. Also, we have BackboneJS models/collections defined for all of the 3 tables and data is loaded into them. A user on the frontend decides to delete records of Jack with a click.</p> <p>Now here are a few approaches to delete all records of Jack:</p> <p><strong>APPROACH#1</strong></p> <p>Call "destroy" on all models of PhoneNumbers and EmailAddresses related to Jack, then call "destroy" on Jack itself.</p> <p><strong>Problem(s)</strong> </p> <ul> <li>Too many AJAX calls for one action. </li> <li>User will have to wait for a lot of time. (More dependants, more time)</li> <li>Deletion will be non-transactional. If user closes the browser during, data will be corrupted.</li> </ul> <p><strong>APPROACH#2</strong></p> <p>Define foreign key relationships at database level, that ensure cascade deletion of PHONE_NUMBERs and EMAIL_ADDRESSes when a PERSON row is deleted. Then "destroy" BackboneJS model for Jack at frontend.</p> <ul> <li>BackboneJS Models for dependants will never know what happened to their corresponding records in the backend. So they'll remain intact.</li> </ul> <p><strong>APPROACH#3</strong></p> <p>Create a URL on server side application "/thoroughly-delete-person" (which makes sure to delete a person with all of its dependants) and invoke it from the frontend instead of calling BackboneJS's Model#destroy on Jack.</p> <ul> <li>Same problem as in Approach#2</li> </ul> <p>So seems like there is no perfect solution for this simple problem. Did you face it too? What approach did you take and why was it better?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload