Riot.js: mounting tags with same variable - riot.js

I have 3 customs tags: my-tag1, my-tag2 and my-tag3
And I am using them like this:
<my-tag1>
<my-tag2 attr1="a">
<my-tag3 attr2="b"></my-tag3>
</my-tag2>
</my-tag1>
I am mounting all tags like this:
riot.mount('*', { store:reduxStore });
my-tag1 can access to store but my-tag2and my-tag3 can not do it.
However, if I do the following, my-tag2 can use store:
<my-tag1>
<my-tag2 attr1="a" store={opts.store}>
<my-tag3 attr2="b"></my-tag3>
</my-tag2>
</my-tag1>
Why? I have to do that in all my tags?

riot.mount('*') mounts all top-level tags. The top-level tags take care of mounting their sub-tags respectively
If you want to pass a store, I think it is best to use mixins. See http://riotjs.com/guide/#mixins
Cheers!

When tags are nested, a new context is created
for a child tag. In the new context, all the parent's properties
inherited are set to undefined.
References:
https://github.com/riot/riot/issues/1720
http://riotjs.com/guide/#context
In your example, by adding store={opts.store} in my-tag2, you are
defining an option store in my-tag2's new context. If you don't do
that, the option store in my-tag2's context is inherited from
my-tag1, but it is set to undefined (so my-tag2 sees undefined for
the store's value).
In order to share the store's value across my-tag1, my-tag2, and my-tag3,
there are two ways in my opinion:
1) my-tag3 initializes store's value to my-tag2's store
<my-tag1>
<my-tag2 attr1="a" store={opts.store}>
<my-tag3 attr2="b" store={opts.store}></my-tag3>
</my-tag2>
</my-tag1>
2) my-tag3 initializes store's value to my-tag1's store using the
parent variable.
<my-tag1>
<my-tag2 attr1="a" store={opts.store}>
<my-tag3 attr2="b" store={parent.opts.store}></my-tag3>
</my-tag2>
</my-tag1>

Related

VueX: populating associations between modules

I'm trying to build my store as closely to a REST API as possible. Now I load 2 modules from the API directly into the sotre, where one of them can be referenced in the other one.
For example:
Each Car in my Car module has an Engine attribute which is a number. That number refers to the ID of an Engine which I also have in another store module.
Now when accessing the Cars getter I want a convenient way to merge each Engine into the associated Car, so that my components don't have to do that.
Now of course I could just get both getters and write some computed property like so:
carWithEngine () {
return this.cars.map(c => ({
...c,
engine: this.engines.find(e => e.id === c.engine)
}))
}
I could even outsource this into a mixin to make it reusable but it'll still only work for cars and engines.
I'm looking for a more generic way, where I could just give the store two getters and the attributes it's supposed to merge, without losing the reactiveness.

Lua modules inheritance

Need your help with modules inheritance in Lua .
Let's say I've got 2 modules:
The 1st one is "Parent" It defines 1 field called "port" and method "connect" that uses port & domain fields to connect to some service. I wanna define the 2nd field (domain) in Child module, not in Parent one. Or at least to override this field by Child module.
module('Parent', package.seeall)
port = 1234
function connect()
ngx.say("connecting to "..domain..":"..port.."\n")
end
Note that "domain" variable is not defined here!
Now let's see the 2nd one, it's "Child":
local base = _G
module('Child', package.seeall)
local Parent = base.require('Parent')
base.setmetatable(Child, { __index = Parent })
domain = '127.0.0.1'
And here goes main lua code creating Child instance:
local Child = require "Child"
Child.connect()
The problem is that variable defined in Child module is invisible for the method defined in Parent module.. I need to change this behavior to let Parent routines code see variables defined in Child module.. Is that possible?
Can i copy Child's namespace to Parent module somehow?
I'm not particularly familiar with Lua modules, but it seems to me the right solution is to redefine the method as function connect(self) and then access port and domain off of self, which will be the package.
function connect(self)
ngx.say("connecting to "..self.domain..":"..self.port.."\n")
end
-- this could also be written as function Parent:connect()
...
local Child = require "Child"
Child:connect()
That's certainly how I'd do it if I were just setting up regular table inheritance without modules.

Arbitrarily nesting some attributes in rabl

I'm designing a new API for my project, and I want to return objects that have nested children as json. For that purpose i've decided to use RABL.
I want the client side to be able to understand whether the object is valid, and if not which fields are missing in order to save it correctly.
The design I thought of should include some fields as optional, under an optional hash, and the rest are required. The required fields should appear right under the root of the json.
So the output I try to describe should look something like this:
{
"name": "John",
"last_name": "Doe",
"optional": {
"address": "Beverly Hills 90210",
"phones":[{"number":"123456","name":"work"}, {"number":"654321","name":"mobile"}]
}
}
The above output example describes the required fields name and last name, and the not required address and phones (which is associated in a belongs_to-has_many relationship to the object). name, last_name and address are User's DB fields.
Playing with RABL I didn't manage so far to create this kind of structure.
Any suggestions? I'm looking for a DRY way to implement this for all my models.
RABL is really good in creating JSON structures on the fly, so I don't see why you couldn't achieve your goal. Did you try testing if a field is set to null-able in the schema, and thus presenting it as optional? It seems a good approach for me. For the nested children, just do the same, but extend the template for the children.
For example, in your father/show.rabl display a custom node :optional with all the properties that can be null.
Then, create a child/show.rabl with the same logic. Finally, go back to father/show.rabl and add a child node, extending the child/show.rabl template. This way you could achieve unlimited levels of "optionals".
Hope it helped you.
In this case I'd use the free form option.
From https://github.com/nesquena/rabl
There can also be odd cases where the root-level of the response
doesn't map directly to any object.
In those cases, object can be assigned to 'false'
and nodes can be constructed free-form.
object false
node(:some_count) { |m| #user.posts.count }
child(#user) { attribute :name }

Named singleton instance in StructureMap (Multiple nHibernate session factories)

I have a scenario where I have two Nhibernate SessionFactorys I need to register an use with StructureMap. Only Foo needs mySessionFactory sessions.
Like this:
For<ISessionFactory>().Singleton().Use(NHibernateConfiguration.GetDefaultSessionFactory());
For<ISession>().HybridHttpOrThreadLocalScoped().Use(x => x.GetInstance<ISessionFactory>().OpenSession());
For<ISessionFactory>().Singleton().Use(AnotherNHibernateConfiguration.GetDefaultSessionFactory).Named("mySessionFactory");
For<ISession>().HybridHttpOrThreadLocalScoped().Use(x => x.GetInstance<ISessionFactory>("mySessionFactory").OpenSession()).Named("mySession");
For<Foo>()
.Use<Foo>()
.Ctor<ISession>("session").Is(x => x.TheInstanceNamed("mySession"));
The problem is that mySessionFactory is now used everywhere when I only wanted to to be used in Foo and everywhere else should use my unnamed instance.
What I'm I doing wrong?
On both your named instances, change Use to Add. Use sets that instance as the default as well as adding it as an instance. You could also reverse the order of your config (the last instance of a type added with Use will become the default), but using the Add method is much more explicit.

NHibernate: How to save a new entity without overwriting the parent:

I'm wondering what the best design would be for persisteing a new child entity with NHibernate without accidentally overwriting the parent in the database.
The problem I have is that the child entity will look something like this:
class Child
{
Parent Parent;
// other fields
}
My problem is that the child has been supplied from the UI layer along with the ID of the parent, and that means that the Parent ref is basically uninitialized: It will have the ID populated but everything else null - because the only way to populate its fields would be an extra round trip to the database to read them.
Now if I call Session.SaveOrUpdate(child) on NHibernate, what's going to happen with the parent. I don't want NHibernate to cascade save the uninitialized parent since that would just destroy the data in the database. How would people approach this problem? Any best practices?
You must use the session.Load(parentid) to get the aggregate root. In contrast to the session.Get() method, this does not actually fetch any data from the database, it just instantiates a Parent proxy object used to add Child objects to the correct Parent in the DB (eg. get the foreign key correctly).
Your code would probably look something like:
// Set the Parent to a nhibernate proxy of the Parent using the ParentId supplied from the UI
childFromUI.Parent = Session.Load<Parent>(childFromUI.Parent.Id);
Session.Save(childFromUI);
This article explains Get/Load and the nhibernate caches really well
You should probably be working with the aggregate root (probably the Parent) when doing Saves (or SaveOrUpdates etc).
Why not just:
Fetch the parent object using the parent id you have in the child from the UI layer
Add the child to the parents 'children' collection
I think you have to overview your mapping configuration for nhibernate. If you have defined on the reference by the child to the parent that hi has to Cascade all, it will update it!
So if you say Cascade.None he will do nothing. All other are bad ideas. Because you allready has the information of this parent. So why read from db agane?!
If your models looks like this
class Parent
{
}
class Child
{
Parent myParent;
}
and you are trying to set the parent and save the child without having a full parent object, just the ID.
You could try this:
session.Lock(child.myParent, LockMode.None);
before saving, this should tell nhibernate that there are no changes to the parent object to persist and it should only look at the object for the Id to persist the association between Parent and Child