Ext JS: Template with period in dataIndex - extjs

I'm trying to create an XTemplate, and one of my dataIndexes has a period... so my data looks something like this:
{
"one": 1,
"two.one": 21,
"two.two": 22
}
Now, when I'm creating the template, I can't do something like {two.one} because that thinks two is the beginning of an object, and then I'm accessing its key one. I've tried doing something like {'two.one'}, but this also doesn't work, and I've tracked it down to the culprit in Ext.XTemplateCompiler.parseTag. This code is what breaks it:
// compound Javascript property name (e.g., "foo.bar")
else if (isNaN(name) && name.indexOf('-') == -1 && name.indexOf('.') != -1) {
v = "values." + name;
}
// number or a '-' in it or a single word (maybe a keyword): use array notation
// (http://jsperf.com/string-property-access/4)
else {
v = "values['" + name + "']";
}
So with my two.one string, I get into that else if, but what I really want is the else that follows right after it. Unfortunately, it doesn't seem like I can override this in an easy way... does anybody have any thoughts? I'm using Ext JS 4.2.1.

Thanks to skirtle over on the Sencha Forums, I was able to solve it:
Instead of using {two.one} or {'two.one'}, it should be {[values['two.one']]}. Using values directly was the ticket.

Related

Using .Filter When the Filter Criteria are in Pre-existing String (TypeScript/Node.js)

I'm trying to process an array of JSON objects that have various common attributes, filtering each array entry on one or more of those attributes.
Normally, I'd just do it something like this:
let filteredResultsArray = originalArray.filter((obj) => {
return obj.attribute1 <= 3 && obj.attribute2 > 0 && obj.attribute3 === 10;
});
My problem is that the filter parameters (the part after "return" in the code above) are highly variable (and unpredictable) from run to run, so I can't hard-code them in the filter. I compute them on the fly and store the whole thing in a string in my code. For example, on one run it might be:
myAttributeString = "obj.attribute1 <= 3 && obj.attribute2 > 0 && obj.attribute3 === 10";
I've tried doing this:
let filteredResultsArray = originalArray.filter((obj) => {
return myAttributeString;
});
That's failing to filter anything. Apparently .filter() is not properly interpreting what I've stored in myAttributeString as filter criteria.
I have a sneaking suspicion that eval(myAttributeString) might be one way to pull this off, but unfortunately I'm working on a team where we've got tslint set to disallow the use of eval(), so that's not an option.
Anybody have an idea how I can get this to work?
When you "compute them on the fly", instead of creating a string, create a callback function that you can then pass to filter. For example, instead of
const myAttributeString = "obj.attribute1 <= 3 && obj.attribute2 > 0 && obj.attribute3 === 10";
do
const filterCallback = obj => obj.attribute1 <= 3 && obj.attribute2 > 0 && obj.attribute3 === 10
Then, later, when the appropriate time comes to .filter, simply pass that as the callback:
const filteredResultsArray = originalArray.filter(filterCallback);
If you can't pass functions around, another option would be to build an array of conditions, for example
[
{
prop: "attribute1",
constraint: "<=",
value: 3
},
{
prop: "attribute2",
constraint: ">",
value: 0
},
// ...
]
and then turn the object into the filter function needed.
****************************************UPDATE******************************
As I suspected, eval() did work, but since I can't use it in my delivered code, and thanks to CertainPerformance's suggestion (which put my thinking on the right track) as well as the Node.js documentation site (via a lucky Google search), I was able to find a workaround using the vm module:
import * as vm from "vm";
let filteredResultsArray = originalArray.filter(
vm.runInThisContext("(obj) => {
return " + myAttributeString + ";}"));
Case closed.

ColdFusion 8 ArrayFind Substitute

I have an array that has a structure of ImageID and Custnum.
I need to find a particular ImageID and retrieve the Custnum for it.
I’m using ColdFusion 8 which does not have an ArrayFind command.
How would I do this without looping through each item? Thanks.
Your question may be answered to a point here "Is there a function similar to ArrayFind from ColdFusion 9 in ColdFusion 8?" but I don't see any other way apart from looping.
You can always create and use an UDF but it would have to use looping.
Why exactly you don't want to use looping anyway? Do you have that many elements in the array? Just remember to use cfbreak after finding your element to stop going through the rest of the array.
Given your situation, I don't think arrayFind() would help you much anyhow, as to find something with arrayFind(), you need to know exactly what you're looking for anyhow. And whilst you know your ImageID, you don't know the Custnum associated with it (hence your underlying problem).
There's nothing native in CF which can help you here, but there's a UDF on CFLib - structFindKeyWithValue() which'll do the trick.
It's written for CF9, but is easily backported to CF8. The modified, CF8-friendly version - is in the example below:
<cfscript>
a = [
{ImageID=1, Custnum=1},
{ImageID=2, Custnum=2},
{ImageID=3, Custnum=3},
{ImageID=4, Custnum=4}
];
testStruct = {a=a};
result = structFindKeyWithValue(testStruct, "ImageID", 2, "ONE");
function structFindKeyWithValue(struct, key, value, scope){
var keyResult = false;
var valueResult = [];
var i = 0;
if (!isValid("regex", arguments.scope, "(?i)one|all")){
throw(type="InvalidArgumentException", message="Search scope #arguments.scope# must be ""one"" or ""all"".");
}
keyResult = structFindKey(struct, key, "all");
for (i=1; i <= arrayLen(keyResult); i++){
if (keyResult[i].value == value){
arrayAppend(valueResult, keyResult[i]);
if (scope == "one"){
break;
}
}
}
return valueResult;
}
</cfscript>
<cfdump var="#result#">
Note that because it's a stuct function, you need to wrap your data in a struct first. Other than that it's fairly straight fwd.

How can I add strings to array in javascript

I wanna create an array in javascript which looks like this:
[0,0,0,0,0,1,1,0,0,1,1,1,1,0,0],[0,0,0,0,1,1,1,1,0,1,0,1,1,1,0],[0,0,0,0,1,1,1,1,0,1,0,0,1,1,0],[0,0,0,0,0,1,1,0,1,0,1,0,1,0,0]
My problem is that I don't know how to add the opening and closing square brackets to the start and the end of the output string.
here's my code:
game = new Array();
for(row=0;row<matrix.length;++row){
game[row]=matrix[row].join(',');
}
document.getElementById('jsvalue').value=game.join('],[');
document.getElementById('name2').value = name;
I tried a few things, but they didn't seem to work and all I got were errors or this output:
0,0,0,0,0,1,1,0,0,1,1,1,1,0,0],[0,0,0,0,1,1,1,1,0,1,0,1,1,1,0],[0,0,0,0,1,1,1,1,0,1,0,0,1,1,0],[0,0,0,0,0,1,1,0,1,0,1,0,1,0,0
How could I add them? Is there a simple array method that I missed and would solve my problem?
Thanks in advance!
It looks like you are trying to set the value of an HTML element to the format you described in your question. However, you are not setting the value of that HTML element to an Array - you are setting it to a string. the .join function outputs a string. If indeed you want the value to be set to a string formatted in the way you described, then you could take advantage of .join, but have to do a little bit in addition to what you are doing:
game = new Array();
for(row=0;row<matrix.length;++row){
game[row]= "[" + matrix[row].join(',') + "]";
}
document.getElementById('jsvalue').value=game.join(',');
document.getElementById('name2').value = name;
If you are using join to create the string, then why not just manually add the brackets?
For example:
document.getElementById('jsvalue').value= '[' + game.join('],[') + ']';

objectify filter empty values

How can I filter properly using Objectify 4 by several parameters, considering that some of those parameters can come empty, which would mean that I don't want to filter by those?
Example:
Please consider I want to filter something like this:
releases = ofy().load().type(Release.class)
.filter("user.name", searchCriteria.getName())
.filter("category", searchCriteria.getCategory())
.filter("city", searchCriteria.getCity()).list();
In order to match with what I said above, I have now the following code, checking every time which of my parameters come empty so I don't put them on the filter in that case:
if (!nameEmpty && !categoryEmpty && !cityEmpty) {
releases = ofy().load().type(Release.class)
.filter("user.name", searchCriteria.getName())
.filter("category", searchCriteria.getCategory())
.filter("city", searchCriteria.getCity()).list();
} else if (!nameEmpty && !categoryEmpty) {
releases = ofy().load().type(Release.class)
.filter("user.name", searchCriteria.getName())
.filter("category", searchCriteria.getCategory()).list();
} else if (!nameEmpty && !cityEmpty) {
releases = ofy().load().type(Release.class)
.filter("user.name", searchCriteria.getName())
.filter("city", searchCriteria.getCity()).list();
} else if ...
...
How can I avoid this crappy way of filtering and make it with just one line (or a few) using Objectify 4?
Query<Release> query = ofy().load().type(Release.class);
if (!nameEmpty)
query = query.filter("user.name", searchCriteria.getName());
if (!categoryEmpty)
query = query.filter("category", searchCriteria.getCategory())
if (!cityEmpty)
query = query.filter("city", searchCriteria.getCity());
releases = query.list();

How to alter field value drupal 7

I am working with drupal 7, and wanted to change the output of "number_float" value when it is "0.00". I have digged into field.api but has no clue what function to do.
To say it in plain English:
if the field type "number_float" and value is "0.00", print "empty value".
This is also to consider before views output.
Any hint or guidance would be very much appreciated.
Thanks
UPDATE:
I used hook_field_attach_view_alter. It does as expected, however I wonder if this is the right thing.
function mymodule_field_attach_view_alter(&$output, $context) {
foreach (element_children($output) as $field_name) {
$element = &$output[$field_name];
if ($element['#field_type'] == 'number_float' && $element['#formatter'] == 'number_decimal') {
foreach ($element['#items'] as $delta => $item) {
if ($element[$delta]['#markup'] == '0.00' || $element[$delta]['#markup'] == '0,00') {
$element[$delta]['#markup'] = t('Empty value message');
}
}
}
}
}
Any suggestion or betterment will be the answer.
thanks
A more standard Drupal way would be to manipulate the value in a preprocessor function. You can use hook_preprocess_HOOK for a theme function or template that's defined by another module. Inside of it, test for the 0.00 value and replace.
The update with hook_field_attach_view_alter is the way to go since nobody provides any other suggestion.

Resources