javascriptbackbone.jsfactorybackbone-model

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


This is the source for 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; 
    }
  }
})();

So it looks like after some complicated logic, the create method will create a new object of a model that we define in backbone. If we have a User model, Backbone.create(User) will create a new user object.

In short, I'm concerned about what happens to these objects when running the test suite.


Solution

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

    It's BackboneFactory.create('user') which works only if you have previously defined a factory for User using BackboneFactory.define('user', User);.

    Where are these objects stored?

    In the following properties of the global BackboneFactory object:

    factories: {},
    sequences: {},
    

    Do they share the same database as production?

    That's irrelevant as the factory only returns a model instance from a Model class that you have defined already.

    Say you have this model:

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

    Instantiating with the factory:

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

    is the same as instantiating manually.

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

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

    It looks like it doesn't provide a simple function for removing a factory, but it's not really useful, because you create factories to use them in your app.

    You could manually delete the factory hash and/or the sequences:

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

    But I would not do that in a normal use-case unless a specific problem arises. For the test suite, maybe it's the easiest way to "reset" the BackboneFactory object.


    That being said, I wouldn't use that library as-is. There are great ideas, but the code is missing a lot of improvements for such a simple lib. One big no-no is that it's a global by default and it's not possible to create different instances of BackboneFactory. It has the problems of the singleton pattern.