Skip to content

v2.13.22

Compare
Choose a tag to compare
@danielspaniel danielspaniel released this 04 Jan 17:12
· 256 commits to master since this release
  • allow traits to be functions, for example
FactoryGuy.define("project", {
  default: {
    title: (f) => `Project ${f.id}`
  },
  traits: {
    //  this trait is a function
    // note that the fixure is passed in that will have 
    // default attributes like id at a minimum and in this 
    // case also a title ( `Project 1` ) which is default
    medium: (f) => {  
      f.title = `Medium Project ${f.id}`
    },
    goofy: (f) => {  
      f.title = `Goofy ${f.title}`
    }
  }
});

So, when you make / build a project like:

let project =  make('project', 'medium');
project.get('title'); //=> 'Medium Project 1'

let project2 =  build('project', 'goofy');
project2.get('title'); //=> 'Goofy Project 2'

Your trait function assigns the title as you described in the function