So the documentation has this nifty "in" operator which I like the idea of more that using a multiple step or statement (||). The documentation gives me this example.
trace("PI" in Math); // true
trace("myProperty" in Math); // false
and
public var myArray:Array = ["zero", "one", "two"];
trace(0 in myArray); // true
trace(1 in myArray); // true
trace("two" in myArray); // true
trace(3 in myArray); // false
So I try to use it like this:
var quickArray:Array = ["#icd9_color","#icd9_icd9","#templateIcd9_name","#templateIcd9_name","#templateIcd9_templateIcd9ID"];
return (element.dataField in quickArray);
Now I can trace or Alert.show() the element.datafield and it will match exactly with an array item, but it never returns true. Can anyone help me figure out why?
The only thing I can get to work this ugly thing:
return (
element.dataField == "#icd9_color" ||
element.dataField == "#icd9_icd9"
etc..
)
The in operator checks whether an object has a specified property - not what the value of that property is.
You want to use Array.indexOf and check for a non-negative value.
Related
not sure what is happening. i have a recursive function getting data from a table. and it's finds an key to be true and then false twice within it being false..
i am wanting to check the isActive boolean and if it's false return false. if it's true then continue the script.
DUMMY_DATA
local DummyData = {
data = {
['id'] = 34523456,
['question'] = 'whats milk?',
['isActive'] = true,
['questionCountdownTimerInSeconds'] = (60),
}
}
RECURSIVE
function FindQuestionInfo(Object)
local Data = {
['id'] = '',
['question'] = '',
['isActive'] = true or false,
['questionCountdownTimerInSeconds'] = (0),
}
for index, child in pairs(Object) do
local ChildIsTable = type(child) == 'table'
if not ChildIsTable then
local isActive = index == 'isActive'
local isId = index == 'id'
local isQuestion = index == 'question'
local isQuestionCountDDownTImerInSeconds = index == 'questionCountdownTimerInSeconds'
if isQuestion then
Data['question'] = child
end
if isId then
Data['id'] = child
end
end
if ChildIsTable then
local FoundItem = FindQuestionInfo(child)
if FoundItem then
return FoundItem
end
end
end
return Data
end
PRINT
Your code doesn't make too much sense. I'm not even sure what you want to achieve with it.
I'll just mention a few issues:
['isActive'] = true or false
As Nifim already pointed out in his comment true or false equals true. Sou you could simply do
['isActive'] = true
You don't need parenthesis around numbers as in ['questionCountdownTimerInSeconds'] = (0)
You don't mention how you use this code. I assume you call FindQuestionInfo(DummyData)
So let's run your code. First you define Data
local Data = {
['id'] = '',
['question'] = '',
['isActive'] = true or false,
['questionCountdownTimerInSeconds'] = (0),
}
Then you traverse over the table Object with a generic for loop and the pairs iterator. Assuming Object is DummyData this will give us a key value pair of DummyData each cycle.
First you check if child (our value) is a table. I don't see how it can be a table with the provided code. If it is not a table you create various booleans.
local isActive = index == 'isActive'
local isId = index == 'id'
local isQuestion = index == 'question'
local isQuestionCountDDownTImerInSeconds = index == 'questionCountdownTimerInSeconds'
And then you assign values conditionally.
if isQuestion then
Data['question'] = child
end
if isId then
Data['id'] = child
end
So only if index equals one of the keys you assign the same table field from Object to Data.
This whole loop doesn't make sense. If you want to assign values from one table to another you simply assign them. You don't traverse over the entire table until you find the right key to assign.
Aside from your isTable condition which seems to be always false you can replace that for loop by
Data.isQuestion = Object.isQuestion and Object.isQuestion or Data.isQuestion
Data.isId = Object.isId and Object.isId or Data.isId
Because you simply assign those values if they exist in Object.
Then there is this section which I cannot make sense of as I don't see how child will ever be a table:
if ChildIsTable then
local FoundItem = FindQuestionInfo(child)
if FoundItem then
return FoundItem
end
end
Also FindQuestionInfo(child) always returns Data so the condition
if FoundItem then
return FoundItem
end
is not necessary.
So unless your Object will have a table inside that you didn't show in your example I don't see any reason to have this code at all. Especially not the recursive part.
You only copy parts of Object into a new table Data.
I cannot make sense of your problem description either.
I'm assuming you're asking about a xy-problem here. So I suggest you ask a new question about the actual problem you're trying to solve rather than about how to fix this code.
When you bind a property it will be reevaluated if one of the proprieties to which is bounded is changed.
Example:
property bool test: prCond1 || prCond2 || ... || prCondN
When a condition is changed test is reevaluated.
Now... I want something similar but for triggering a javascript function:
when one of several conditions prCond1 || prCond2 || ... || prCondN is changed I want a function to be called.
If there was only one condition I could write:
onPrCond1Changed: {
functionCall()
}
But when you take into account more than one condition what is the best way to do it? Is there a standard way?
Basically I need something like this:
functionCall() if one of these changes: prCond1 || prCond2 || ... || prCondN
Where prCond's may be of different types.
A possible solution would be to group the variables into a variant list and look for changes on the list.
property var myObject = {'prop': 'value'}
property variant conditions = [prCond1, prCond2, myObj]
onConditionsChanged: {
console.log("one of the conditions have changed");
}
Note that changes in the properties of myObj will not trigger the changeEvent, unless the object itself is changed (e.g. myObj = new Object({'prop': 'newValue'}) )
I've looked at this other question, but can't get my select box to work correctly:
Binding initial/default value of dropdown (select) list
I've got the following Game object:
function Game(visitingTeamDetails, homeTeamDetails, game) {
if (arguments.length > 0) {
this.VisitingTeamDetails = visitingTeamDetails;
this.HomeTeamDetails = homeTeamDetails;
this.GameId = ko.observable(game.GameId);
this.HomeTeamName = ko.observable(game.HomeTeamName);
this.VisitingTeamName = ko.observable(game.VisitingTeamName);
this.SportTypeName = ko.observable(game.SportTypeName);
this.HomeAccountName = ko.observable(game.HomeAccountName);
this.VisitingAccountName = ko.observable(game.VisitingAccountName);
this.GameDateString = ko.observable(game.GameDateString);
this.GameTimeString = ko.observable(game.GameTimeString);
this.AvailableSportTypes = ko.observableArray(game.Sports);
this.sportTypeFunction = function () {
for (sportType in this.AvailableSportTypes()) {
if (this.AvailableSportTypes()[sportType].Name == this.SportTypeName()) {
return this.AvailableSportTypes()[sportType];
}
}
return null;
};
this.SportType = ko.observable(game.SportType);
}
}
SportType is an object with Name and SportTypeId.
I have the following template:
<td rowspan="3"><select data-bind="options: AvailableSportTypes, value: SportType, optionsText:'Name', optionsCaption: 'Choose...'" class="sportType"></select></td>
AvailableSportTypes is a list of SportType.
The list is coming in with the names of the SportTypes in the drop down list, but I can't make the initial selection be SportType. I wrote sportTypeFunction to show myself that the data was coming in correctly, and it would select the correct value, but changing my selection in the drop down would not update SportType.
I'm sure I'm doing something wrong. Anyone see it?
Thanks
When game.SportType gets passed in, it needs to be a reference to the an item in the game.AvailableSportTypes and not just an object that looks the same.
Basically two objects are not equal unless they are actually a reference to the same object.
var a = { name: "test" },
b = { name: "test" };
alert(a === b); //false
So, you would need to call your function to locate the correct object in the array and set it as the value of your observable.
Not that it is way better, but in KO 1.3 you can extend .fn of observables, observableArrays, and dependentObservables to add additional functionality.
Here is a sample: http://jsfiddle.net/rniemeyer/ZP79w
in Flex I have something like that:
var dg:DataGrid = new DataGrid();
if (something) dg = dg1 else if (something_2) dg = dg2;
dg.dataProvider.getItemAt(3).id;
and dg is ALWAYS pointing at DataGrid (even if dg1 has name DataGrid_test and dg2 = DataGrid_test2) and finally action is made on my first DataGrid (DataGrid_test).
Why?
How can I pass dg1 or dg2 to dg?
Here is pasted almost full code of this part of application. I edited it to make that more clear.
var dg:DataGrid = null;
if ( currentState == "state1" ) { //if this condition is true then app. go into if and
dg = dataGrid_first; // make dg = DataGrid (1)
test.text = "inco"; // shows "inco" in "test" label
} else if ( currentState == "state2" ) { // if this is true then app. go..
dg = dataGrid_second; //here and set dg as DataGrid (exactly!) (2)
test.text = "outgo"; // and change test label into blank text (earlier text disapears)
}
search(dg);
It is modified with advice of '#splash'
Still not working.
EDIT:
I made this sceond edit to answer for all You who are helping me with that :) I think that it will be the best way. In codeblock above I added comments. (please read now comments and after that come back here :) )
Now I will explain exactly what happens.
I debug it many times and here are results:
dg is pointing at DataGrid (as component in flex, not as my dataGrid_first), I needed to extend DataGrid so now it is ColorColumn component (I don't know if I called it properly), not DataGrid. And dg is pointing at ColorColumn not at dataGrid_first or dataGrid_second. I even tried today the same thing what suggest #splash:
if ( currentState == "state1" ) {
test.text = "inco";
search(dataGrid_first);
} else if ( currentState == "state2" ) {
test.text = "outgo";
search(dataGrid_second);
}
and search still points at ColorColumn :/ My problem is really easy- I just want to pass to search different dataGrid on each state. If You have other ideas how I can do that in right way then I will pleased to hear about it. :)
But still I don't understand why it doesn't work. My search function uses algorhitm Boyer-Moor for searching through dataGrid.dataProvider for some text. If it find something then it is pushed into new array and after passing whole dataProvider I colorize rows with searched word.
If dg is never pointing to dg1 and dg2 then your (something) expressions may be evaluate to false. Check the value of your if-conditions - this should be easy to debug.
This should work:
var dg:DataGrid = null;
if (something)
dg = dg1;
else if (something_2)
dg = dg2;
if (dg)
{
// do something with dg
}
[Update]
I still can't see why your code isn't working, but you could simplify it like this:
if ( currentState == "state1" ) {
test.text = "inco";
search(dataGrid_first);
} else if ( currentState == "state2" ) {
test.text = "outgo";
search(dataGrid_second);
}
I'd propose to write this - since I guess either dg1 or dg2 should be assigned:
if (something) {
dg = dg1;
} else {
dg = dg2;
}
There may be cases, where if () {} else () {} neither executes the first or the second conditional block.
Finally a small hint, which structurally eliminates unwanted assignments in if conditions: Always write the literal left of the comparison operation: if ( "state1" == currentState ). If you accidentally typed = instead of ==, the flex compiler emits an error. The other notation silently assigns a value.
Additionally: Did you single-stepped through your code and watched the variables dg1, dg2 and dg? If not, set a breakpoint a few line before the if-statement and run the code step by step from there on. What do you see?
Here's a another tip: Use assertions to check for inconistencies:
package my.company.utilities {
public function assert(expression:Boolean):void {
// probably conditionally compile this statement
if (!expression) {
throw new Error("Assertion failed!");
}
} // assert
}
Use it e.g. at the beginning of a method like this:
public function doTransaction( fromAccount:int, toAccount:int ) {
assert( 0 < fromAccount );
assert( 0 < toAccount );
}
A typically good use of assert is to check variables regarding their range. As of the above example, fromAccount and toAccount should always be positive. Due to a bug, bad values might get passed to doTransaction(). In this case, the assertion fires an error.
I have a LINQ query like this:
from i in _db.Items.OfType<Medium>()
from m in i.Modules
from p in m.Pages
where i != null && i.Type == 1 && i.Published == true && p.PageId == 2
select p
I use the query like this because I have strongly typed view (ASP.NET MVC).
I need to have items sorted by the i.Sort property. orderby i.Sort and i.OrderBy(it => it.Sort) doesn't work. How can I fix this?
When sorting with Linq you usually give OrderBy a property, and eventually an IComparer, not a sorting function. For example:
class Person {
public int Age {get; set;}
}
public static void Main() {
var ps = new List<Person>();
ps.Add(new Person{Age = 1});
ps.Add(new Person{Age = 5});
ps.Add(new Person{Age = 3});
var sorted = ps.OrderBy(p => p.Age);
foreach(p in sorted) {
Console.WriteLine(p.Age);
}
}
Here Linq will know how to correctly sort integers.
Without giving more context (such as what exactly is i.Sort, what is its purpose, what do you want to do with it), it would be difficult to be more specific to your problem.
However, I'm pretty sure you are misunderstanding OrderBy: you should give it a lambda expression that identifies a property of the objects contained in your sequence, and then Linq will sort your sequence according to the usual order of the type of that property (or according to another order you define for that type, by using IComparer).
Let's say your Pages include page-numbers among their properties. Let's pretend this property is called "pagenumber". You would then add the following 'orderby' line between the 'where' and 'select' lines.
// (snip...)
where i != null && i.Type == 1 && i.Published == true && p.PageId == 2
orderby p.pagenumber
select p
Or maybe you don't have page numbers, but only page titles. You would do nearly the same thing:
where i != null && i.Type == 1 && i.Published == true && p.PageId == 2
orderby p.title
select p
Just from reading your code, I can't tell what criteria should be used for sorting. You need some kind of ordered element, an id number, a page number, or some text can be alphabetized.
from i in _db.Items.OfType<Medium>().OrderBy(x => x.Sort)
...