How do I get rid of model attached to view after using Backbone's model#destroy?

akantoword

I'm seeing the success callback but in the debugger Chrome Dev Tools, I'm still seeing the model when I type in this.model. I know that it's destroyed on the server side, but can you explain why it's still attached to the view? How do I get rid of it?

        delete: function () {
            this.model.destroy({success: function () {console.log("success in destroy");}});
            debugger;
        }
Lochlan

What you are seeing is correct. Looking at the documentation on model.destroy (or looking to the code) we can see it basically does two things:

  • HTTP DELETEs the server representation of the model
  • Removes the model from any containing collections

Note that nothing happens to the model itself or to any objects the model may be attached to.

We can see this behavior with a simple example:

var foo = new Backbone.Model({foo: 'bar'});
var foos = new Backbone.Collection([foo]);
var fooView = new Backbone.View();
fooView.model = foo;

foo.destroy({success: function () {
    console.log('success');
}});

console.log(foo, foos, fooView);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone.js"></script>

Note that nothing happens to foo or fooView.model after executing this code, although foos no longer contains an instance of foo.

Removing view.model

If you want to remove the model from the view you can leverage the success callback. Just change your view's delete method (from your question) to something like the following:

delete: function () {
    this.model.destroy({success: function () {
        delete this.model;
    }.bind(this)});
}

Alternatively, since we know from the docs that the model will fire a "destroy" event, we can also listen for that event and fire a callback that deletes our model.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I get rid of model attached to view after using Backbone's model#destroy?

From Dev

How do i get rid of methods for view in model class

From Dev

After upgrade: what's this envelope systray icon, and how do I get rid of it?

From Dev

After upgrade: what's this envelope systray icon, and how do I get rid of it?

From Dev

How do I get rid of the square brackets after the code is implemented?

From Dev

What's this window called, and how do I get rid of it

From Dev

How do I get rid of the /#/ when using ui-router?

From Dev

How do I get rid of errors using TaskStackBuilder on Android Studio?

From Dev

How do I get rid of the HWaddr that docker0 is using?

From Dev

How do I pass properties to a Backbone view?

From Dev

How do I get automatic model binding of this view model property?

From Dev

How do I get to the properties of the view model in the view?

From Dev

How do I get a Strongly typed MVC view to post back the instance of the model it's passed?

From Dev

How do I get rid of the NOTE's generated by R CMD check when using for example ddply in my package?

From Dev

How do I get my model into my view using C#?

From Dev

How do I get rid of the NameError and TypeError

From Dev

How do I get rid of this jump on slideToggle()?

From Dev

How do I get rid of "[1]"?

From Dev

What is &shy; and how do i get rid of it

From Dev

How do I get rid of this new line?

From Dev

How do I get rid of SKTextures?

From Dev

How do i get rid of an "MySQLSyntaxErrorException"

From Dev

How do I get rid of this goto?

From Dev

NoSuchElementException on Scanner, how do i get rid of it?

From Dev

How do I get rid of this new line?

From Dev

How do I get rid of this Anthy toolbar?

From Dev

How do I get rid of this allocated space?

From Dev

How do i get rid of this warning

From Dev

How do I get rid of the last comma?

Related Related

  1. 1

    How do I get rid of model attached to view after using Backbone's model#destroy?

  2. 2

    How do i get rid of methods for view in model class

  3. 3

    After upgrade: what's this envelope systray icon, and how do I get rid of it?

  4. 4

    After upgrade: what's this envelope systray icon, and how do I get rid of it?

  5. 5

    How do I get rid of the square brackets after the code is implemented?

  6. 6

    What's this window called, and how do I get rid of it

  7. 7

    How do I get rid of the /#/ when using ui-router?

  8. 8

    How do I get rid of errors using TaskStackBuilder on Android Studio?

  9. 9

    How do I get rid of the HWaddr that docker0 is using?

  10. 10

    How do I pass properties to a Backbone view?

  11. 11

    How do I get automatic model binding of this view model property?

  12. 12

    How do I get to the properties of the view model in the view?

  13. 13

    How do I get a Strongly typed MVC view to post back the instance of the model it's passed?

  14. 14

    How do I get rid of the NOTE's generated by R CMD check when using for example ddply in my package?

  15. 15

    How do I get my model into my view using C#?

  16. 16

    How do I get rid of the NameError and TypeError

  17. 17

    How do I get rid of this jump on slideToggle()?

  18. 18

    How do I get rid of "[1]"?

  19. 19

    What is &shy; and how do i get rid of it

  20. 20

    How do I get rid of this new line?

  21. 21

    How do I get rid of SKTextures?

  22. 22

    How do i get rid of an "MySQLSyntaxErrorException"

  23. 23

    How do I get rid of this goto?

  24. 24

    NoSuchElementException on Scanner, how do i get rid of it?

  25. 25

    How do I get rid of this new line?

  26. 26

    How do I get rid of this Anthy toolbar?

  27. 27

    How do I get rid of this allocated space?

  28. 28

    How do i get rid of this warning

  29. 29

    How do I get rid of the last comma?

HotTag

Archive