How to create mixins for Polymer.AppLocalizeBehavior for Polymer 2.x?
I am using Redux mixins using following syntax:
class MyCreateAccount extends ReduxBehavior(Polymer.Element) {..}
I need to add Polymer.AppLocalizeBehavior to my class declaration and need help in creating the required mixin.
In the class-based syntax you can simulate element multiple inheritance of class mixins with something.
For example if you've created a redux store file like this
<...your redux store content...>
const store = Redux.createStore((state = {}, action) => state)
const ReduxMixin = PolymerRedux(store)
You can import that file and bind elements using redux mixin wrap the mixin behaviors which you can include your behaviors inside.
class MyCreateAccount extends ReduxMixin(Polymer.mixinBehaviors([Polymer.AppLocalizeBehavior], Polymer.Element)) { .. }
or you can use the Polymer 2 hybrid behaviours as mixins by extending Polymer.mixinBehaviors([behaviors], superclass) as below
class MyCreateAccount extends Polymer.mixinBehaviors([Polymer.AppLocalizeBehavior, ReduxBehavior], Polymer.Element) { .. }
Related
In my studies of Dart languages, I found some packaged using this kind of class declaration:
class _Base = Authentication with Utilities, Validators;
I don't understand what the operator = is doing here, is it a kind of alias?
Is a shortcut to create inherit classes with mixins.
The example is same to:
class _Base extends Authentication with Utilities, Validators{
_Base(): super(); // or with args
}
Utilities and validators are mixins.
Can anyone help me understand this piece of code?
#immutable
abstract class User with _$User {
const factory User(String name, int age) = _User;
}
The syntax I particularly want to understand is the benefit for the class to mixin itself apparently and what the prefix _$ means in the declaration.
From what I see you most likely dealing with class that will have more code generated using code generator.
Class User does not mixin itself. It mixins class $User. Class $User will be generated and placed in appropriate *.g.dart file which will be part of your *.dart file once code generator is complete.
I'm learning Vaadin 11 and I have a question. What is the difference between this:
public class MyClass extends Div { /... }
...and this?:
#Tag(Tag.DIV)
public class MyClass { /** }
According to the documentation:
Div class:
Component representing a <div> element.
Tag annotation:
Defines the tag to use for the root element for a component created using the default Component constructor.
Looks like they do the same thing: making a class to represent a root element. Is that so?
Both ways give you a <div> element in the browser (except that you need to add extends Component to the #Tag(Tag.DIV) example).
The difference is that the Div class also defines some additional API specifically for <div> elements, whereas you have full control over the public API of the component if create a generic Component subclass with #Tag(Tag.DIV).
The difference is the server-side API. The Div class is really trivial by itself, but it does implement some helpful mixin interfaces through its parents, like HasSize and HasStyle. This means that you can e.g. call myClass.setHeight("500px") if you're extending Div.
On the other hand, if you use the #Tag annotation to tell that your MyComponent class has the div tag on the client, you don't expose any server-side methods in addition to what you decide to provide yourself. Whether this is a good thing or a bad thing depends on what kinds of features you want to provide to your class's users.
I was trying to write something with the following design pattern:
void main()
{
document.registerElement('abs-test', Test);
TestExtend test = new TestExtend();
document.body.append(test);
}
abstract class Test extends HtmlElement
{
Test.created() : super.created();
factory Test() => new Element.tag('abs-test')..text = "test";
}
class RedTest extends Test
{
TestExtend() : super() => style.color = 'red';
}
My aim with this is to create a custom HtmlElement registered to the abstract class "Test". This abstract class "Test" would have some properties that all elements of type Test need to have. In this example, all elements of type Test need to have the word "test" as their text.
However, I wanted then to only allow the user to create subclasses of "Test" with more specific properties. In this example we have RedTest which then sets the color of Test to red. The resulting HTML would be:
<abs-test style="color:red;">test</abs-test>
I have 2 problems with this:
1) Is it possible to call the factory constructor of a parent class? (If not, is it possible to extend HtmlElement in a different way that doesn't require a factory constructor)
2) Is it possible to extends HtmlElement with an abstract class?
I have been testing for a while with different constructors but am unable to make it work. Can anyone advice?
You register a tag with a class so that the browser can instantiate it for you. If this isn't an 1:1 relation, there is no way for the browser to know what class to instantiate.
Therefore
You need
a different tag for each class
register each tag with a concrete (non-abstract) class
You normally can call the constructor of a subclass in a factory constructor of an abstract class
factory Test() => new RedTest()..text = "test";
is valid in Dart, but because extending an element requires to return
new Element.tag('abs-test')
this won't work, because you can't return both at the same time.
You need to register each concrete subclass with a different tag like
document.registerElement('abs-test-red', TestRed);
document.registerElement('abs-test-blue', TestBlue);
I'm trying to use the ORM RedBeanPHP (v3.3) in a ZF2 (v2.0.2) application and I'm having trouble with its automatic FUSE model. I can't make it link to my model classes. It's not picking them up automatically and using "regular" beans instead.
I'm using the RjhRedbean module to load up RedBean in ZF2.
My model class is the following, placed in the folder .\module\Check\src\Check\Model\Model.
<?php
namespace Check\Model;
use \RedBean_SimpleModel;
class Model_Check extends RedBean_SimpleModel
{
public $id;
public $type;
...
public function open()
{
}
public function toArray()
{
return array($this->id, $this->type);
}
I confirm it's picked up by the autoloader since $c = new Model_Check(); works.
My controller code trying to load all the Check model objects from the DB is:
<?php
namespace Check\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;
use RjhRedbean;
use Check\Model\Model_Check;
class CheckController extends AbstractActionController
{
public function listAction()
{
$rb = $this->getServiceLocator()->get('RjhRedbean');
$checks = $rb->findAll('Check'); // does not link to my Model object
foreach ($checks as $check) {
$ar = $check->toArray(); // does not exist in the objects returned
...
The objects returned are RedBean_OODBBean
What should I put as the bean name in the findAll() method? I tried:
$checks = $rb->findAll('Model_Check');
$checks = $rb->findAll('Check/Model/Check');
$checks = $rb->findAll('Check/Model/Model_Check');
Nothing seems to do the trick. When creating a bean, I get the same problems too...
Thanks.
The reason that this isn't working is that FUSE cannot automatically find the Model for the bean as it would normally do due to problems introduced in using namespaces.
You can work around this problem by using a class map file and defining all Models that you are using in there. Making sure you models are in the global namespace.
More information and examples can be found on my blog: How to use FUSE models in RjhRedbean
I modified RjhRedbean module as followed:
1.Created a class named ModelFormatter
this class does nothing else but return the appropriate namespace related to your models.
the namespace can be set in your config.
2.In the RjhRedbeanServiceFactory class added
RedBean_ModelHelper::setModelFormatter($serviceLocator->get('ModelFormatter'));