Backbone remove and new view will not reset the contents of its attributes array

I have an idea about Backbone with these two attributes:

var MyBasketView = Backbone.View.extend({

    _totalCost: 0,
    _devices:  [],

and upon initialization, I scan the array in localStorage with several devices, which I pop them into the _devices array and sum them up total with _totalCost like this

var self = this;
_.each(this._cartItems, function(cartItem) {
    self._totalCost += cartItem.item.cost;
    self._devices.push(cartItem.item);
}

In my router, I delete this view when I leave this page, and again create this view when I return. The problem is that when I print the attributes in my initialization function, the total cost is 0, and the device array still has the devices before.

Should it be reset when I delete the view or create it using the new MyBasketView ()?

, , , , , ?

+4
2

, Model.defaults ( ):

, JavaScript , , .

, , _devices, :

var v1 = new MyBasketView();
var v2 = new MyBasketView();

v1._devices === v2._devices // true

http://jsfiddle.net/nikoshr/67pr1gnk/

MyBasketView ( ).

initialize:

var MyBasketView = Backbone.View.extend({
    _totalCost: 0,
    initialize: function() {
        this._devices = [];
    }
});

http://jsfiddle.net/nikoshr/67pr1gnk/1/

+2

, Backbone. , , . initialize, , .

: http://jsfiddle.net/jrbxnr0t/

+1

Source: https://habr.com/ru/post/1606940/


All Articles