dojo how to query node that has been removed from DOM - dom

From my testing, it appears that dojo's query() function only selects from among elements that are part of a page's DOM.
For example, suppose I have a node named rootNode that has some number of descendents with the class someClass. This code:
var nodeList = query(".someclass", rootNode);
will return an empty NodeList if rootNode has been removed from the DOM (but, of course, if rootNode is part of the DOM, it will return all the nodes with class someClass.
My question: is there a way to use query() in this situation? If not, what is the preferred way for handling this? It looks to me like some of NodeList's methods can be used with a filter(remove and place), but that's not quite the same.

You can get a dojo NodeList as the return result of your dojo.query. Then you can use nodes.map or nodes.every to find the elements you want.
http://dojotoolkit.org/reference-guide/1.9/dojo/NodeList.html

My answer turned out to be very simple: although I didn't find it in the documentation, you can simply call query() on a NodeList. e.g.
var nodeList = query.NodeList();
nodeList.push(rootNode);
var results = nodeList.query(yourSelector);
Note, however: if yourSelector is a unique id and rootNode has been removed from the DOM, it doesn't appear to work. Other types of selectors (class, children, etc) seem to work. I wonder if that's a bug in Dojo...

Related

How to make copy of array's elements in the dart

Getting wired issue like main array has been changed if changed value of another array. I think issue is about copying same address, not sure but just thinking of it. I have tried from last 3 hours but unable to get rid from it.
Look at below illustration to get better idea.
List<page> _pageList;
List<page> _orderList = [];
_pageList = _apiResponse.result as List<page>;
_orderList.add(_pageList[0].element);
_orderList[0].title = "XYZ"
//--> Here if I change the `_orderList[0].title` then it also change the `title` inside "_pageList"
How can we prevent the changes in main array?
I got same issue in my one of the project. What I have done is, use json to encode and decode object that help you to make copy of the object so that will not be affected to the main List.
After 3rd line of your code, make changes like below
Elements copyObject = Elements.fromJson(_pageList[0].element.toJson());
// First of all you have to convert your object to the Map and and map to original object like above
_orderList.add(copyObject);
Hope that will help you.
You can use a getter function to create a copy of your list and use that instead of
altering your actual list.
example:
List<Page> get orderList{
return [..._orderList];
}
Lists in Dart store references for complex types, so this is intended behaviour.
From your code:
_orderList.add(_pageList[0].element);
_orderList[0] and _pageList[0].element point to the same reference (if they are non-primitive).
There is no general copy() or clone() method in dart, as far as i know. So you need to copy the object yourself, if you want a separate instance. (see this question)

Internal mechanism of TreeViewer for updte

I am new to SWT and RCP I am trying to use TreeViewer.
By referring to some documents, I came to know there is method:
treeViewer.Updte(Object , Properties).
I need to know how SWT figure out which data is for which field.
The method is called update:
public void update(Object element, String[] properties)
Here element must be an object that equals one of the objects returned by the content provider for the tree.
If you have called
treeViewer.setUseHashlookup(true);
then a hash table (similar to HashMap) is used to find the tree element corresponding to the element. Otherwise the tree is just searched exhaustively to find the element.

Spring List Binding in Form

I'm trying to bind a list/arraylist/hashmap/etc of custom objects to my form in JSP using Spring. Right now, the controller creates a Map of the two lists (Boolean list and custom object list) in referenceData(), and provides it to the form which uses those values to populate the fields. The values are initialized from a MySQL database using Hibernate, and all that works fine. The list is a known length before the form is initialized, so that part is easier. Now what I'd like to do is correctly bind those objects in the form, so that when there are changes made, I can detect that in onSubmit() (or wherever is appropriate), and update the database accordingly. I can't seem to bind them correctly in the form so that I can see changes made. I tried just using a list of the form fields as the model, but even that wasn't working correctly. Do I just need to inject the list in a particular way? Any ideas or examples here? Any help would be greatly appreciated.
UPDATE: At Ralph's request here is the solution I used:
In my data object class, I lazy loaded a map using MapUtils.lazyMap(), with a String key and other custom object value. The other custom object is just a class that contains List<String> and getters/setters. In the corresponding .jsp file, I just nest several loops to loop through the keys first using loop.current.key and then loop2.current.value.paramsList to loop through the values for that key. This was not really what I asked for in my original post, as I was looking for a more general solution, and the lazy loading pointed me in the right direction.
In Spring 2 you need a special List in your Command object, that is able to grow if one add the x-th element event if the list has not this size yet.
One way to do that is to use LayzList decorator from commons-collections.
#Override
protected Object formBackingObject(final HttpServletRequest request)
throws Exception {
List<PosterSelectionRow> posterSelectionRowList = LazyList.decorate(
new ArrayList<PosterSelectionRow>(),
new PosterSelectionRowListFactory());
return new PosterSelectionCommand(posterSelectionRowList);
//PosterSelectionCommand contains a list of selected poster rows
}
private static class PosterSelectionRowListFactory
implements org.apache.commons.collections.Factory {
/** Invoked if the list need a new element */
public Object create() {
return = new PosterSelectionRow();
}
}
When I remember right, there is a way without that Factory stuff, but I am not sure.

zend_form access parent form element

I couldn't find any reference on how to use a parent form element in a subclassed form. May be because it's obvious to everyone but me. It's got me stumped. This is what I tried.
At first, within my form constructor I called
parent::__construct($options = null);
then accessed the parent elements like this
$type = parent::setName($this->type);
The problem was that ALL the parent form elements would display whether explicitly called or not. Someone said, "don't use __construct(), use the init() function instead. So I changed the constructor to init(), commented out the parent constructor, then ran the form. It bombed saying it couldn't pass an empty value for setName(). I commented out all the seName() calls and the form ran, but only displayed the elements instantiated in the subclassed form.
My question is this: If I don't use the parent constructor, how do i get and use the parent's form elements?
Solved: Since the constructor was switched to init, the call to the parent also needed to be switched. Easy for someone with php background. Not so much for one who doesn't.
Use
parent::init();
Solved: Since the constructor was switched to init, the call to the parent also needed to be switched. Easy for someone with php background. Not so much for one who doesn't.
Use
parent::init();
You should learn OOP principles first. Obviously you have no understanding of it whatsoever. You need to call parent::init() in you Form_Class::init() method as you wrote, but why? Because otherwise the parent method is not called and is overriden by the From_Class method.
Other thing is that when you have a parent class "SuperForm" with input and submit, then your "SuperForm_Subclass" would have the same elements assigned. There is no need to use "parent::*" to access element (only exception would be if you used static SuperForm variable to store them - which makes no sense).
You can easily use $this->inputElement and $this->submitElement inside your SuperForm_Subclass like you would in the SuperForm class.
In your example you could used the __contruct() as good, but with the same condition of calling the parent constructor. You would be able to access elements generated there too...

Returning Array Fragments

I need to get array fragments from an array. I'm sick of using Array.Copy().
new ArraySegment(..).Array returns the original [full] array. The one below is what I came up with but I feel it's pretty lame. Is there a better way to do this?
class Program
{
static void Main(string[] args)
{
var arr = new ArraySegment<byte>(new byte[5] { 5, 4, 3, 2, 1 }, 0, 2).ArrayFragment();
for (int i = 0; i < arr.Length; i++)
Console.WriteLine(i);
Console.Read();
}
}
static class Extensions
{
public static T[] ArrayFragment<T>(this ArraySegment<T> segment)
{
var arr = new T[segment.Count];
Array.Copy(segment.Array, segment.Offset, arr, 0, segment.Count);
return arr;
}
}
Thanks in advance.
Update:
The above was just an example.
I have a method: byte [] CalculateXXX(byte [] key, byte [] message);
I do array manipulations inside this method. I want to return portion of an array.
ArraySegment does not implement IEnumerable and it does NOT return an array with just the segment new ArraySegment(arr...).Array returns the complete original array.
var rval = new byte[4];
//new ArraySegment(finalOutputBuffer, 0, 4).SegmentedArray();
Array.Copy(finalOutputBuffer, 0, rval, 0, 4);
I find I had to do the above to return a array fragment. Was just wondering if there's a better way of returning fragments of an array [as new array].
Vyas, I am truly sorry for having posted this useless pile of ****. It's been ages since I've actually used ArraySegment and I simply assumed that it implemented a (more or less) consistent interface. Someone (Jon?) please tell me which drugs were used during the implementation of this useless struct.
Finally, to make a long story short, the best solution is probably to implement your own version of ArraySegment, only doing it right.
I don't understand your problem with using ArraySegment. There's no additional overhead involved here, if that's what you mean, since no copying of the original data is done. Rather, ArraySegment offers a lightweight view on the data.
Th make this work, change your (return) type from T[] to IEnumerable<T>, or, if you need indexed access, to IList<T>. Generally prefer using interface types in method signatures, and completely avoid array types. The rationale is very simple: it makes problems such as yours go away and makes the interface more flexible and future-proof. It also provides better information hiding since it hides parts of the implementation which aren't relevant for the consumer of this method.
Define better. What is the downside with ArraySegment? What problem are you having that it doesn't solve?
Edit: Ok, I now understand your point of view, but this is wrong. It might be a bug in the sense that you feel it should do more, but it does exactly what it is supposed to do. It allows you to pass information to a piece of code about an array you wish to use, and which portion of the array to use.
It doesn't provide IEnumerable or anything that gives you a narrow view of the array, ie. x[0] doesn't give you the first element of the segment, nor can you foreach over it.
So yes, it's rather useless on its own, but you wouldn't be able to get something that is an array at heart, and yet is also a segment of a larger array.
You could easily make your own collection-like class that references an array and uses the array as storage, and provides indexing, enumeration, etc.
But that's not what this structure does. I guess the main problem here is that they made you expect more from it through its name.
Just another side-effect of poor iterator design in C# btw. There are many instances similar (just as 'lame') where it would just plain good design to be able to pass or point or control segments (aka concept called range) without all the archane shickanery.. copy semantics or not, array is also a well-defined concept.
Just use C++. :-)