Skip to content
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

close #6097: Allow defining optional inject dependency with default values #6322

Merged
merged 3 commits into from Oct 5, 2017
Merged

close #6097: Allow defining optional inject dependency with default values #6322

merged 3 commits into from Oct 5, 2017

Conversation

pdanpdan
Copy link
Contributor

@pdanpdan pdanpdan commented Aug 8, 2017

Allow to define inject as object with name and default properties. The default is used if no provide
is found, and no warning is emitted.

close #6097

What kind of change does this PR introduce? (check at least one)

  • Bugfix
  • Feature
  • Code style update
  • Refactor
  • Build-related changes
  • Other, please describe:

Does this PR introduce a breaking change? (check one)

  • Yes
  • No

If yes, please describe the impact and migration path for existing applications:

The PR fulfills these requirements:

If adding a new feature, the PR's description includes:

  • A convincing reason for adding this feature (to avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it)

Other information:

In case of components that can work both as standalone and as children, it would be nice to be able to provide default values for the not provided dependencies.
It would also avoid the warning for missing inject in case this is one of the expected use case.

inject: Array | { [key: string]: string | Symbol | { name?: string | Symbol, default?: any } }

@@ -270,12 +270,21 @@ function normalizeProps (options: Object) {
*/
function normalizeInject (options: Object) {
const inject = options.inject
const normalized = {}
let val
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to declare val here

}
} else if (isPlainObject(inject)) {
for (const key in inject) {
val = inject[key]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be a const

@@ -362,6 +362,23 @@ describe('Options provide/inject', () => {
expect(`Injection "baz" not found`).not.toHaveBeenWarned()
})

// Github issue #6097
it('should not warn when injections cannot be found but have default value', () => {
const vm = new Vue({})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add also an injection with a value and check that it takes precedence?

if (Array.isArray(inject)) {
const normalized = options.inject = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can keep this line (and move it outside of the if) and remove the last line options.inject = normalized

@pdanpdan
Copy link
Contributor Author

pdanpdan commented Aug 9, 2017

Fixed. I added a separate test for provided value taking precedence over default.

@posva
Copy link
Member

posva commented Aug 9, 2017

Thanks, we'll wait for @yyx990803 to review it as well 🙂
Others may chime in in the meantime as well

@@ -55,8 +55,15 @@ export function resolveInject (inject: any, vm: Component): ?Object {
}
source = source.$parent
}
if (process.env.NODE_ENV !== 'production' && !source) {
warn(`Injection "${key}" not found`, vm)
if (!source) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't if (!result[key]) easier to understand here?

@pdanpdan
Copy link
Contributor Author

No, it's not the same. You don't want to check the value of the injection, but it's presence. It would work !key in result

@jkzing
Copy link
Member

jkzing commented Aug 11, 2017

@pdanpdan Oh, yes I missed falsy values. NVM

@rstoenescu
Copy link
Contributor

@yyx990803 Hi, any chance you can take a look at this? Quasar Framework developers cannot upgrade to Vue 2.4.x until this PR gets in. Thanks!

@posva
Copy link
Member

posva commented Aug 21, 2017

By chime in, I meant to give further feedback 😉 , upvoting a comment is enough to say +1, no need to comment I need this to upgrade , +1 or similar

I don't remember seeing weex files getting updated, any idea why that weex PR got in the git history of this PR @pdanpdan ?

@vuejs vuejs deleted a comment from ariesjia Aug 22, 2017
@pdanpdan
Copy link
Contributor Author

I don't understand where did the weex commit come from. I'm on vacation till next Monday and I'll have to wait till then to get to a computer.

@javoski
Copy link
Member

javoski commented Aug 22, 2017

Seems all open PR are in the similar situation.

@pdanpdan
Copy link
Contributor Author

I just changed the PR to exclude the weex commit.

…default values

Allow to define inject as object with name and default properties. The default is used if no provide
is found, and no warning is emitted.

#6097
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow defining optional inject dependency with default values
7 participants