-
-
Notifications
You must be signed in to change notification settings - Fork 274
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
Feature: add vue component/master #71
Feature: add vue component/master #71
Conversation
First of all, thank you for all that work! it's really hard to review because there are a lot of files :) P.S.: remove |
packages/casl-vue/spec/can.spec.js
Outdated
import { abilitiesPlugin } from '../src'; | ||
|
||
chai.use(spies); | ||
const should = chai.should(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use expect
because all packages in repo uses expect
not should
packages/casl-vue/package.json
Outdated
@@ -46,6 +46,8 @@ | |||
"@casl/ability": "^2.0.0", | |||
"@vue/test-utils": "^1.0.0-beta.12", | |||
"vue": "^2.5.13", | |||
"vue-template-compiler": "^2.5.13" | |||
"vue-template-compiler": "^2.5.13", | |||
"chai": "^4.1.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be removed. chai
and chai-spies
are installed on the top level. So, you can run tests with 2 commands:
npm test -- --scope @casl/vue
- or from package root:
PATH=$PATH:../../node_modules/.bin npm test
@@ -1,86 +1,91 @@ | |||
import { createLocalVue } from '@vue/test-utils' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove all semicolons from tests. I prefer to keep tests clean and I'm not using semicolons in tests )) Probably need to add eslint rules for this
packages/casl-vue/src/plugin.js
Outdated
methods: { | ||
$can(...args) { | ||
watcher.rules = watcher.rules; // create dependency | ||
return this.$ability.can(...args); | ||
}, | ||
$defineAbility(fn) { | ||
const watcherForComponent = new Vue({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need a separate watcher. Also I'd like to not add this function into Vue components. People can use regular AbilityBuilder.define
instead.
You can use parent.$can()
from component, instead of adding additional watcher
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return parent.$can(action, subject, field) ? children : null
packages/casl-vue/src/plugin.js
Outdated
methods: { | ||
$can(...args) { | ||
watcher.rules = watcher.rules; // create dependency | ||
return this.$ability.can(...args); | ||
}, | ||
$defineAbility(fn) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please remove this function
packages/casl-vue/src/plugin.js
Outdated
} | ||
} | ||
}); | ||
Vue.component('Can', Can); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be removed and I'd like people (i.e., clients of CASL) to do it manually in case if they want to use Can
component. In case if not, component can be shaked out from the build by webpack or rollup.
So, @casl/vue
should just export Can
component and later in the app if you want to use it you will do this:
import { Can, abilitiesPlugin } from '@casl/vue'
import Vue from 'vue'
Vue.use(abilitiesPlugin)
Vue.component('Can', Can)
new Vue(...)
props, children, parent, injections | ||
}) { | ||
validateProps(props); | ||
if (injections.ability) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this. I'd prefer to implement inject/provide
in a separate PR after dicussion
|
||
export default { | ||
name: 'Can', | ||
inject: ['ability'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this, explanations below
props: { | ||
I: String, | ||
do: String, | ||
a: [String, Object], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be only string
or Function
(i.e., class)
on: [String, Object] | ||
}, | ||
render(createElement, { | ||
props, children, parent, injections |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to use slots. What is the difference between slots
and children
?
there is #63 task with subtasks for this. You can look for details there |
1c2a1ad
to
38b6a82
Compare
Codecov Report
@@ Coverage Diff @@
## master #71 +/- ##
==========================================
+ Coverage 99.62% 99.63% +<.01%
==========================================
Files 15 16 +1
Lines 267 274 +7
==========================================
+ Hits 266 273 +7
Misses 1 1
Continue to review full report at Codecov.
|
38b6a82
to
07f17a6
Compare
the AFAIK the |
Awesome! Thanks for explanation. Frankly speaking, I don’t think that it make sense to have more than 1 instance of Ability in application. However your point is valid. In some edge cases it may be required (I don’t know such). But for simplicity I’d like to proceed with smaller steps (I wrote article about CR). Let’s add support for provide/inject in a separate PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now it's better. I will change tests a bit and merge it. Thanks!
packages/casl-react/spec/Can.spec.js
Outdated
@@ -149,4 +149,4 @@ describe('`Can` component', () => { | |||
function validateProps(Component, props, propName) { | |||
assertPropTypes(Component.propTypes, props, 'prop', Component.name) | |||
} | |||
}) | |||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revert this change please as it doesn’t relates to the purpose of this commit :)
created #72 for provide/inject functionality |
just have one instance of ability also has another issue at this time.let's say, I have one App have 3 components. like: https://jsfiddle.net/saL0ocLj/ , when |
Ok. I’ll take a look at that tomorrow. Thanks! |
I see your point. You want to not rerender component if The main idea of CASL is to keep all permissions in one place. So I don’t think it make sense to create separate abilities (I.e. instances of Ability) Another point is that user permissions are not changed very often. Thus performance shouldn’t be a big issue here |
Thanks for the explanation, It's helpful! |
An interesting reading about functional components: vuejs/vue#4037 (comment) |
first, I want to thanks the casl team for providing the awesome package.
I'm now using casl to my Vue project for permissions. and I've implemented a Can component with Vue provide/inject which #64 mentioned.if you guys have good advice. please feel free to tell me.