Arrays in a Backbone.js Model are essentially static?

前端 未结 4 2104
傲寒
傲寒 2020-12-10 19:23

Why are arrays in a Backbone.js Model essentially static variables?

class exports.Content extends Backbone.Model
    tags: []

then if i mak

4条回答
  •  遥遥无期
    2020-12-10 20:15

    "tags" is being declared on the Content.prototype - which is shared across all instances of Content. You probably need to use defaults instead: http://backbonejs.org/#Model-defaults. However, it should be noted that you must use a function (instead of a hash) when defining defaults, otherwise you still run into the same problem when using types which are passed by reference (e.g. Array).

    var Content = Backbone.Model.extend({
      defaults: function() { 
        return {
          tags: []
        };
      }
    });
    

提交回复
热议问题