Provide is used to pass values to child components, so how does the parent component that sends the provide get the data in the provide itself? - vue-native

In Vue, provide can pass values to child components
provide('data',ref("I am data for child components"))
Subcomponents can use inject to get the value.
How to get the data in the parent component? ?

Normal pass by value:
let data = ref("I am data for child components");
provide('data',data);//This way the parent component can get
it and the child component can get it too
Transfer Function:
let children = ref('')
function data(val){
children.value = val;//Get the value passed by the child component
}
provide('data',data);
Sub: let getData = inject('data',data)
data is a function, you can getData('data for father')

Related

JPA parent has no children after saving child with relation to parent

I have a lazy one-to-many relationship between two classes AdministrativeUnit (one) and WorkPosition (many). I am creating an integration test using these classes:
// Initialize the database
AdministrativeUnit administrativeUnit = createEntity(em); // create AdministrativeUnit
administrativeUnitRepository.saveAndFlush(administrativeUnit);
// Create a work position under administrative unit
WorkPosition workPosition = WorkPositionResourceIT.createEntity(em); // create WorkPosition with administrativeUnit set as parent
workPositionRepository.saveAndFlush(workPosition);
List<WorkPosition> workPositionList = workPositionRepository.findAll();
assertThat(workPositionList.size()).isEqualTo(1);
assertThat(workPositionList.get(0).getAdministrativeUnit()).isEqualTo(administrativeUnit);
Set<WorkPosition> workPositions = administrativeUnitRepository.getById(administrativeUnit.getId()).getWorkPositions();
assertThat(workPositions).hasSize(1); // fail: expected size 1, actual 0 in []
Why does the last assertion fail? I understand that I have not really set "both sides of the link", but shouldn't it still know that the created WorkPosition belongs to the AdministrativeUnit as I have set the AdministrativeUnit as the parent of the WorkPosition?
The assertion is successful if I, before the getById query, say administrativeUnit.setWorkPositions(Set.of(workPosition));, which seems unnecessary as I actually do not wish to call administrativeUnit.getWorkPositions(); anywhere in the test. It will be called during an http request afterwards, where it behaves the same way.
The assertion is also successful if I, again before the getById query, say em.detach(administrativeUnit), but how this works, I do not know.

How to update nested redux state

I have the following structure
lookups --> Object
lookups.CATEGORIES --> array of category objects
lookups.TIMEZONES --> array of timezone objects
I would like to add new object, which is a lookup object which has lookup_type property. It could be either 'CATEGORY' or 'TIMEZONE'.
Depending on lookup_type, the newly added object has to be added either to CATEGORIES or TIMEZONES object. How this could be achieved?
The structure of lookups object
lookups: {CATEGORIES:[{obj1}, {obj2}...], TIMEZONES:[{obj1}, {obj2}, {obj3}...]}
You can use spread on the nested object or array too:
return {
...state,
[action.lookupType]: [
...state[action.lookupType],
action.value,
]
};
That would add a new item to Categories or Timezone, if you want to replace a value or insert it at an index etc then you should construct the new array how you want it just above the return and pass that instead. Also note that array spread is ES7.
You probably want to pass your lookup object as the payload of an action, which your lookup reducer handles. In the lookup reducer check for the value action.payload.lookup_type and return the state with a new CATEGORIES or TIMEZONES array containing the old values of that array with the lookup object insterted. You should probably check out some redux examples first, if you are unsure how to work with it.

How to set nested objects?

i am trying to set the redux state but not the whole object tree but just a sub-tree.
Object graph -
Parent
|----Header
|----Summary
|-----List of objects
I am using following code -
return state.setIn([Parent, 'Summary'], summary)
It sets the summary object but the list of objects is null. What am I doing wrong ?
you can try it in this way:
let newSummary = Object.assign(Parent.summary, {...yourchange});
this.setState(Object.assign(Parent.summary, {
summary: newSummary
})})

Why is calling .select() changing my bound data?

In my mental model of d3, calling .select() should not change what data is bound to selections.
I've been encountering a case where calling .select() and passing it a function changes the bound data and I would appreciate an explanation of what's wrong with my mental model.
Here's a minimal case:
// Setup
let body = d3.selectAll('body')
let exampleData =
[
{_id:"groupOne", items:['one','two']},
{_id:"groupTwo", items:['three','four']}
];
// Add some divs
body.selectAll('div').data(exampleData).enter().append('div');
// Add some p children of the divs
body.selectAll('div').selectAll('p').data((d)=>d.items).enter().append('p').text((d)=>d);
// Issue
console.log(body.selectAll('div').data()); // data is the same as exampleData
body.selectAll('p').select(function(){return this.parentNode}); // Select parents of all p
console.log(body.selectAll('div').data()); // Data is now ["two","four"]
and here is a live version on bl.ocks.org .
From the documentation of d3: https://github.com/d3/d3/wiki/Selections#select
If the current element has associated data, this data is inherited by the returned subselection, and automatically bound to the newly selected elements.
So you select the p elements with body.selectAll('p') which has data ["one,"two","three","four"] (this data is inherited by the returned subselection, and automatically bound to the newly selected elements.)
Then you make a subselection with .select(function(){return this.parentNode});
The subselect will 'iterate' 4 times.
Which will be the div with:
<div><p>one</p><p>two</p></div> for the first two
and <div><p>three</p><p>four</p></div> for the last two.
For the first iteration; parentNode <div><p>one</p><p>two</p></div>,
will get(inherit) data "one".
The second iteration: data "two".
In the 3rd iteration; parentNode <div><p>three</p><p>four</p></div>,
will get data "three".
And four the 4th iteration: data "four".
However, I think what you're trying to do is something like this:
body.selectAll('p').select(this.parentNode); // Select parents of all p
console.log(body.selectAll('div').data()); // Data is now the same as exampleData
(Without the return statement)

Set values on Symfony2 validation with no form

I'm coding an API and I'm doing the create method. I'm doing the following without needing a form:
$params = array('title' => 'test', 'parent_id' => 781);
// bind data
$place = new Place();
$place->bind($params);
// validate params
$errors = $this->validator->validate($place);
I need to check that parent_id is a correct value (its object exist - i know how to do this) and after that, I need to set some values dependent on the parent. So at the end the Place object will have the fields: title, parent_id, level, country_id for example.
How would you do this? On the validation? How? If not, how to avoid calling two times the DB to get the parent object?
You should first validate & then set any additional values afterward. Anything that modifies the value does not belong in the validator.
If your using doctrine, it should load the parent object into memory when you first access it, so it won't need to actually query the database again when you access the parent object a second time.

Resources