Dynamically adding 1 PolymerElement to another shows in childNodes but not queryable? - dart

I have 2 classes i created which extend PolymerElement, we can call them: ElementA, and ElementB.
So, i want to add ElementB to ElementA dynamically, so i was thinking to add it to the onReady call of ElementA as follows:
class ElemenetA extends PolymerElement{
ElementB get _myElement => $["bid"];
onReady(){
ElementB item = new ElementB(); //item has an id of "bid"
Polymer.dom(this).childNodes.add(item);
}
}
So when i visit the Component, each time, it will add a new ElementB to the childNodes. Its ok, i will resolve this later.
The issue i have bumped into though is that ElementB doesnt render at all and if i try to call a method from it such as open like this: _myElement.open(); it will say null does not have method open.
What am i doing wrong to inject a PolymerElement into another?
My overall goal is to pull out a common element in a bunch of other Components and just use a behavior to inject this re-occurring item into the dom.

You can't access elements that are dynamically added using $[...], this works only for elements added statically to the elements HTML. Even when they are inside a <template is="dom-if"> or <template is="dom-repeat"> $[...] cant be used.
Use instead $$('#bid')

Related

Customize fieldset output from form collection in ZF2

I have correctly set up a form in ZF2, which includes several elements and a collection containing a fieldset that can be duplicated by the user. All works fine, but I need more control on how the fieldset renders, ie. I need to add other things between some elements in the fieldset, rather than just output them one after another.
I'm not expert in ZF2, but from research I seem to understand that you can declare a custom fieldsetHelper in the formCollection, however:
I can't manage to do so; I tried to extend the FormCollection class, and adding protected $fieldsetHelper = 'myFieldsetHelper';, which is also declared in the module config, but I get:
Fatal error: Call to undefined function myFieldsetHelper()
I'm familiar with extending FormRow helper, but I don't know how the fieldset helper should be written (I only need to add my extra stuff between some elements), and can't find any example on the web.
Any help, please?
You can use other view helpers to customize the forms layout in your view script such as FormLabel, FormText, etc.
Please see my answer here
EDIT
You can also loop through all the collection elements in your view script and render separately.
<?php
foreach ($this->form->get('collection') as $fieldset) {
echo $this->formText($fieldset->get('elementName'));
}
?>

Dart Language: Polymer - Working with views

I'm developing my first "complex" application on Dart and I'm finding some trouble on how to work with more than one view (I want to develop the application to run within a single page).
First of all, I'm assuming that this (using Polymer) is the right approach to work with various views (or "app screens") of the application. However, if it's not, I would like to know (of course). And by "app screens", I'm saying something like creating various forms in Java.
So, for instance, I have two buttons at the top of the page. When you click the first one, it shows a view with an user registration area. When you click the second button, it shows a view with an editable grid.
Could you give me some code example of this simple app?
You can use CSS style properties for a polymer element like you do for any native element.
You can query like this:
Element e = querySelector('my-form');
and then hide/display it:
e.style.display = 'none';
e.style.display = 'block';
After(!) you initialized polymer, you can use the element's class (if existent):
#CustomTag(...)
class MyForm ... {
...
}
MyForm e = querySelector('my-form') as MyForm;
Then you can go ahead and set attributes of your class (if you need it) or use the style property because the class inherits from HtmlElement.
You could also use classes for this. Dart comes with some "toggle" functionality:
element.classes.toggle('hidden');
element.classes.toggle('shown');
Regards, Robert
You could use the template if functionality.
This way views that are currently not active are automatically removed from the DOM and automatically inserted again when the model attribut changes.
<polymer-element name='app-element'>
<template>
<template if='{{appModel.view == 'view1'}}'>
<view1></view1>
</template>
<template if='{{appModel.view == 'view2'}}'>
<view2></view2>
</template>
</template>
<script ...>
</polymer-element>

Loop through Form Elements in the order they were added

Zend Form 2 structures all Elements in Fieldsets. (Zend\Form\Form extends Fieldset - Form::add calls parent::add )
If i just add Elements to the Form i can get them via $form->getElements() if i use a fieldset i can get them via
foreach($form->getFieldsets() as $fieldset){
$elements = $fieldset->getElements();
}
But imagine a form where i add a few hidden fields, then a fieldset, and at last a submit button.
How can i get the elements/fieldsets in their right order ?
Reason behind this, I'm working on a view helper which lets me print forms via a simple call to the view helper.
I don't want to call every form element via a call to formRow()
(I know of the concept behind Form2 - separating logic from presentation)
Any help is much appreciated.
TIA
you can do the following to get elements and fieldsets in the order they were added to the form:
/* $form is an instance of \Zend\Form\Form */
foreach ($form as $element) {
// check if it's a form element or a fieldset etc.
// and recursively iterate over elements of fieldsets etc.
}

Can I get Intellij Idea 10.5 to do code completion on model variables in a shared GSP template?

*Edit* I didn't know about 'Add Dynamic Property' feature of Idea, that is exactly what I wanted. Thanks Sergey
I love the fact how Intelli-J recognizes the type of variables put into the model from a common controller and allows autocomplete right in the GSP.
For example if I have a controller method
def mymethod = {
MyDomain myDomainInstance =
.... logic ...
[myDomainInstance: myDomainInstance]
}
In my corresponding mymethod.gsp, Intelli-J will autocomplete methods on ${myDomainInstance} from the MyDomain bean.
However consider a shared template that uses the same domain class and could be rendered via
<g:render template="/shared/somesharedtemplate" model="['myDomainInstance': myDomainInstance]">
When I am editing /shared/_somesharedtemplate.gsp, ${myDomainInstance} is not recognized as something Intelli-J knows about (but of course works fine when Grails runs). Is there any declaration or anything I can add to the GSP to hint it?
Try to remove quotes around variable name in value of 'model' attribute. IDEA does not recognize model variable defined inside quotes. It's a bug: http://youtrack.jetbrains.net/issue/IDEA-80041.
Also you can add a dynamic property 'myDomainInstance' for page _somesharedtemplate.gsp . Press Alt+Enter on unrecognized reference then select 'Add Dynamic Property 'myDomainInstance'' intention.

How to create multiple domain objects from a GSP page

I have a Person class with two properties: name and address. I want to build a GSP page which allows for 10 users to be created at one time. This is how I'm implementing it and was wondering if there is a better way:
First, make 20 text boxes in the GSP page - 10 with someperson.name and 10 with someperson.address field names (make these in a loop or code them all individually, doesn't matter).
Second, process the submitted data in the controller. The someperson object has the submitted data, but in a not-so-nice structure ([name: ['Bob', 'John'], address: ['Address 1', 'Address 2']]), so I call transpose() on this to be able to access name, address pairs.
Then, build a list of Person objects using the pairs obtained from the previous step and validate/save them.
Finally, if validation fails (name cannot be null) then do something... don't know what yet! I'm thinking of passing the collection of Person objects to the GSP where they are iterated using a loop and if hasErrors then show them... Don't know how to highlight the fields which failed validation...
So, is there a better way (I should probably ask WHAT IS the better way)?
You should use Grails' data-binding support by declaring a command object like this
class PersonCommand {
List<Person> people = []
}
If you construct your form so that the request parameters are named like this:
person[0].name=bob
person[0].address=england
person[1].name=john
person[1].address=ireland
The data will be automatically bound to the personCommand argument of this controller action
class MyController {
def savePeople = {PersonCommand personCommand->
}
}
If you call personCommand.validate() it might in turn call validate() on each Person in people (I'm not sure). If it doesn't you can do this yourself by calling
boolean allPersonsValid = personCommand.people.every {it.validate()}
At this point you'll know whether all Person instances are valid. If they are not, you should pass the PersonCommand back to the GSP and you can use the Grails tags:
<g:eachError>
<g:hasErrors>
<g:renderErrors>
to highlight the fields in errors. If you're not exactly sure how to use these tags to do the highlight, I suggest you run grails generate-all for a domain class and look at the GSP code it generates.

Resources