Giter Site home page Giter Site logo

backbone.factotum's Introduction

Backbone.Factotum

Build Status npm version

A Backbone fully-compatible factory lib for creating JavaScript objects.

Example

factories.js

/*
 *
 * Let's suppose we have defined the following entities somewhere:
 *
 * const Job = Backbone.Model.extend({});
 *
 * const JobCollection = Backbone.Collection.extend({ model: Job });
 *
 * const User = Backbone.Model.extend({
 *   parse(attrs) {
 *     this.jobs = new JobCollection(attrs.jobs);
 *
 *     return _.omit(attrs, 'jobs');
 *   }
 * });
 *
 *
 */

const Factotum = require('backbone.factotum');

const Job = require('../job');
const User = require('../user');

Factotum.define('job', Job, { name: Factotum.sequence((i) => `Job ${i}`) });

Factotum.define('user', User, {
  id: Factotum.sequence((i) => i),
  name: 'Henry Chinaski',
  jobs: Factotum.create('job', 5)
});

user.spec.js

const Factotum = require('backbone.factotum');

const JobCollection = require('../jobCollection');

describe('User', function() {
  it('creates a user model with a collection of jobs', function() {
    const user = Factotum.create('user', { parse: true });

    user.jobs.should.be.an.instanceof(JobCollection);
  });
});

Features

This lib is heavily inspired in factory_girl, so they way for working with both of them is pretty similar.

  • Sequences can be defined by using Factotum.sequence
Factotum.sequence((i) => `My sequence item ${i}`)
  • Factory definitions receive an object with the expected attributes.
Factotum.define('user', User, {
  id: Factotum.sequence((i) => i),
  name: 'Henry Chinaski',
});
  • Several objects can be created at once by passing an additional parameter to Factotum.create
const users = Factotum.create('user', 10);
  • Backbone options such as parse can be bridged from Factotum to the required object initialization
const users = Factotum.create('user', 10, { parse: true });

backbone.factotum's People

Stargazers

Roman avatar Pablo Rivera Conde avatar

backbone.factotum's Issues

Factotum.create could received parameters to be used during the definition

In some scenarios, I want to create the same entity but changing some of its attribute. I would like to be able to do it with just one definition instead of having several entity definitions quite similar.

If you consider that it makes sense taking into account for what this library was created, please let me know... I could think in how it should be implemented and even create a pull requests ;)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.