New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameterized virtual properties #1755

Merged
merged 4 commits into from Feb 4, 2018

Conversation

Projects
1 participant
@ricardograca
Member

ricardograca commented Feb 3, 2018

Introduction

This combines the best features of the previous two PRs. It's simple in its changes like #1474, but allows some control of which parameters go to which virtuals when calling .toJSON() like #1614.

Also fixes the sinon warnings in the test suite and rearranges the Virtuals Plugin test file to nest some test cases under their respective describe blocks.

Motivation

No one else seemed interested in changing their PR.

Proposed solution

This builds on the previous work and works exactly like those two PRs when it comes to the changes done to the model's .get() method:

var PersonWithTitle = bookshelf.Model.extend({
  virtuals: {
    fullName: function(name) {
      return this.get('firstName') + ' ' + name
    }
  }
})
var john = new Person({firstName: 'John'})

john.get('fullName', 'Connor') // John Connor

However, it adds a new virtualParams option to toJSON() that allows passing values to specific virtuals. It must be an object with keys corresponding with the virtual name their values are meant for:

john.toJSON({virtualParams: {fullName: 'Connor'}})
john.fullName  // John Connor

Closes #1474 and closes #1614.

Current PR Issues

Can't pass more than one argument to virtuals in toJSON(). This is possible when using .get() directly though. This was done on purpose to allow passing arrays to virtuals, although the benefit of that is yet to be determined.

Also lacks documentation, but that will be done separately on the Wiki page. In the future Plugin docs should be a part of the main website, but currently the docs for plugins aren't being generated.

@ricardograca ricardograca added this to To Do in Version 0.13.0 via automation Feb 4, 2018

@ricardograca ricardograca moved this from To Do to In Progress in Version 0.13.0 Feb 4, 2018

@ricardograca ricardograca merged commit fe49187 into master Feb 4, 2018

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

Version 0.13.0 automation moved this from In Progress to Done Feb 4, 2018

@ricardograca ricardograca deleted the rg-parameterized-virtuals branch Feb 4, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment