如何销毁BackboneFactory中创建的对象以及它们存储在哪里?

How to destroy the created objects in BackboneFactory and where are they stored?

这是 BackboneFactory, a Backbone equivalent of FactoryGirl 的来源:

// Backbone Factory JS
// https://github.com/SupportBee/Backbone-Factory

(function(){
  window.BackboneFactory = {

    factories: {},
    sequences: {},

    define: function(factory_name, klass, defaults){

      // Check for arguments' sanity
      if(factory_name.match(/[^\w-_]+/)){
        throw "Factory name should not contain spaces or other funky characters";
      }

      if(defaults === undefined) defaults = function(){return {}};

      // The object creator
      this.factories[factory_name] = function(options){
        if(options === undefined) options = function(){return {}};
        arguments =  _.extend({}, {id: BackboneFactory.next("_" + factory_name + "_id")}, defaults.call(), options.call());
        return new klass(arguments);
      };

      // Lets define a sequence for id
      BackboneFactory.define_sequence("_"+ factory_name +"_id", function(n){
        return n
      });
    },

    create: function(factory_name, options){
      if(this.factories[factory_name] === undefined){
        throw "Factory with name " + factory_name + " does not exist";
      }
      return this.factories[factory_name].apply(null, [options]);        
    },

    define_sequence: function(sequence_name, callback){
      this.sequences[sequence_name] = {}
      this.sequences[sequence_name]['counter'] = 0;
      this.sequences[sequence_name]['callback'] = callback; 
    },

    next: function(sequence_name){
      if(this.sequences[sequence_name] === undefined){
        throw "Sequence with name " + sequence_name + " does not exist";
      }
      this.sequences[sequence_name]['counter'] += 1;
      return this.sequences[sequence_name]['callback'].apply(null, [this.sequences[sequence_name]['counter']]); //= callback; 
    }
  }
})();

所以看起来在一些复杂的逻辑之后,create 方法将创建我们在 backbone 中定义的模型的新对象。如果我们有一个 User 模型,Backbone.create(User) 将创建一个新的用户对象。

简而言之,我担心 运行 测试套件时这些对象会发生什么情况。

If we have a User model... Backbone.create(User) will create a new user object.

BackboneFactory.create('user') 仅当您之前使用 BackboneFactory.define('user', User);.

User 定义了一个工厂时才有效

Where are these objects stored?

在全局 BackboneFactory 对象的以下属性中:

factories: {},
sequences: {},

Do they share the same database as production?

这无关紧要,因为工厂只是 returns 来自您已经定义的模型 class 的模型实例。

假设你有这个模型:

var User = Backbone.Model.extend({
    urlRoot: "api/user",
    defaults: {
        name: "Foo",
        randomNumber: 4
    }
});

用工厂实例化:

BackboneFactory.define('user', User);
// somewhere later
var instance = BackboneFactory.create('user', { name: "bar" });

与手动实例化相同。

var instance = new User({ name: "bar" });

If it's in memory, are they garbage collected eventually?

看起来它没有提供删除工厂的简单功能,但它并不是很有用,因为您创建工厂是为了在您的应用程序中使用它们。

您可以手动删除工厂哈希 and/or 序列:

BackboneFactory.factories = {};
BackboneFactory.sequences = {};

但除非出现特定问题,否则我不会在正常用例中这样做。对于测试套件,也许这是 "reset" BackboneFactory 对象的最简单方法。


话虽如此,我不会按原样使用该库。有很棒的想法,但是对于这样一个简单的库,代码缺少很多改进。一个很大的禁忌是默认情况下它是全局的,并且不可能创建 BackboneFactory 的不同实例。它有 problems of the singleton pattern.