how to change multiple function arguments into coffescrpt - coffeescript

here is the javascript:
$inputor.on("keyup.inputor", $.proxy(function(e) {
var stop_key = e.keyCode == 40 || e.keyCode == 38
lookup = !(this.view.isShowing() && stop_key)
if (lookup) this.lookup()
},this))
how can i translate it into coffesscript? the first argument of a function like $.proxy is a function and still have a second one.
my solution is assign a variable for the first argument, the function, and poss it to $.proxy.
but i want a better solution.
coffeescript:
??????

Instead of using the jQuery.proxy function, you could use the CoffeeScript fat arrow => since the context you're trying to use is this
$inputor.on "keyup.inputor", (e) =>
stop_key = e.keyCode == 40 || e.keyCode == 38
lookup = !(#view.isShowing() && stop_key)
#lookup() if lookup

Related

Unable to access cellvalue when using external filter ag-grid with angularjs

I'm using an external filter in ag-grid which is supposed to filter the records based on a select value dropdown which has values corresponding to a specific field in the grid.
And I'm unable to access the value of the field using node.data.fieldName as mentioned in the documentation here.
Below is what I'm doing:
function isExternalFilterPresent() {
return $scope.filterval.ReleaseType!='All' && $scope.filterval.ReleaseType!='';
}
function doesExternalFilterPass(){
console.log('$scope.filterval.ReleaseType : ' ,$scope.filterval.ReleaseType);
if($scope.filterval.ReleaseType == 'A'){return node.data.ReleaseType = 'A';}
if($scope.filterval.ReleaseType == 'B'){}
if($scope.filterval.ReleaseType == 'C'){}
if($scope.filterval.ReleaseType == 'D'){}
if($scope.filterval.ReleaseType == 'D'){}
}
It throws an error : node is not defined
When I try using just data.fieldName it says 'data is not defined'
Can someone please help me understand how I can access the value of the specific field here.
You need to provide node as an argument to the function. ag-grid calls this function with appropriate argument node.
Link: Example External filter
function doesExternalFilterPass(node) { // <- node as argument
console.log('$scope.filterval.ReleaseType : ' ,$scope.filterval.ReleaseType);
if($scope.filterval.ReleaseType == 'A'){return node.data.ReleaseType = 'A';}
if($scope.filterval.ReleaseType == 'B'){}
if($scope.filterval.ReleaseType == 'C'){}
if($scope.filterval.ReleaseType == 'D'){}
if($scope.filterval.ReleaseType == 'D'){}
}

pass decimal column value to populate results from a table using linq query

I have a product table with 20 column. There is a column 'Width' which is decimal data type. I have been calling particular width value in web api controller to populate results. As an example,width values are like below:
Width values
1 0.0015
2 1.0000
3 0.0063
4 1.0100
5 2.0000
6 2.0630
public HttpResponseMessage GetclassByWidthList(Decimal cWidth)
{
using (CrossReferenceTool1Entities cls1 = new CrossReferenceTool1Entities())
{
**var query = (from u in cls1.Products where (u.PrivateOnly == false && u.SelectionTool == true && u.ProductTypeID == 2 && (u.ProductFamilyID == 11 || u.ProductFamilyID == 12 || u.ProductFamilyID == 58 || u.ProductFamilyID == 59 || u.ProductFamilyID == 92) && **u.Width == cWidth**) select u.Class).Distinct().ToList()**;
HttpResponseMessage res;
res = Request.CreateResponse(HttpStatusCode.OK, query);
return res;
}
}
method is working fine. results are reflected if width value is "1.0000" OR "2.0000" but for other values of width it is not populating any result.
Please help me on above query where other decimal values with precision will populate result.
i have tried in browser to populate result:
1.
http://localhost:55481/api/KendoCascading/GetclassByWidthList/1 - result is poupulating
`http://localhost:55481/api/KendoCascading/GetclassByWidthList/1.01 - http-404 error showing
For debugging, your decimal data of Cwidth is being truncated, Please check
As per Ehasanul's suggestion i Just modified my method with putting extra "/" last of that route path
[Route("**api/KendoCascading/GetclassByWidthList/{cWidth:decimal}/**")]
public HttpResponseMessage GetclassByWidthList(decimal cWidth)
{
using (CrossReferenceTool1Entities cls1 = new CrossReferenceTool1Entities())
{
var query = (from u in cls1.Products where (u.PrivateOnly == false && u.SelectionTool == true && u.ProductTypeID == 2 && (u.ProductFamilyID == 11 || u.ProductFamilyID == 12 || u.ProductFamilyID == 58 || u.ProductFamilyID == 59 || u.ProductFamilyID == 92) && u.Width == cWidth) select u.Class).Distinct().ToList();
HttpResponseMessage res;
res = Request.CreateResponse(HttpStatusCode.OK, query);
return res;
}
}
call that url in my angular service http.get method with adding extra "/" in last.
this.getclsWidthList3 = function (cWidth) {
var res;
if (cWidth !== 0.0000) {
res = $http.get("/api/KendoCascading/GetclassByWidthList"+ "/"+ cWidth+"/");
return res;
}
};
and its working great.

How can I embed optional Where parameters in a Linq to EF statement?

Let's say we have a description field on my form with optional check boxes. The check boxes represent which fields to search when doing the lookup. Right now I have a matrix of look ups that call their unique version of where clause. It works but I think it smells a bit.
Here is an excerpt
// Look for part numbers decide how many fields to search and use that one.
// 0 0 X
if (!PartOpt[0] && !PartOpt[1] && PartOpt[2])
{
query = query.Where(p => (p.PartNumAlt2.Contains(partSearchRec.inventory.PartNum)));
}
// 0 X 0
if (!PartOpt[0] && PartOpt[1] && !PartOpt[2])
{
query = query.Where(p => (p.PartNumAlt.Contains(partSearchRec.inventory.PartNum)));
}
// 0 X X
if (!PartOpt[0] && PartOpt[1] && PartOpt[2])
{
query = query.Where(p => (p.PartNumAlt.Contains(partSearchRec.inventory.PartNum)
|| p.PartNumAlt2.Contains(partSearchRec.inventory.PartNum)));
}
// X 0 0
if (PartOpt[0] && !PartOpt[1] && !PartOpt[2])
{
query = query.Where(p => (p.PartNum.Contains(partSearchRec.inventory.PartNum)));
}
. . .
This goes on for a while and seems to be prone to coding errors. In each case we are looking for the same information in any of the selected fields. If I was doing this in SQL I could simply build up the WHERE clause as needed.
Once again I rubber ducked my way to an answer. Rather than throw the question away, here is what I came up with. Is it efficient?
if (partSearchRec.optPartNum || partSearchRec.optAltPartNum1 || partSearchRec.optAltPartNum2)
{
query = query.Where(p => (
(partSearchRec.optPartNum && p.PartNum.Contains(partSearchRec.inventory.PartNum))
|| (partSearchRec.optAltPartNum1 && p.PartNumAlt.Contains(partSearchRec.inventory.PartNum))
|| (partSearchRec.optAltPartNum2 && p.PartNumAlt2.Contains(partSearchRec.inventory.PartNum))));
}
Basically if any of the check boxes are set we will execute the query. Each line of the query will be processed only if the check box was checked. If the left side of an AND is false it doesn't process the right.
This is an aera that Delphi's with statement would be handy. I also learned that you can't use an array inside the LINQ statement.

Most concise way to assign a value from a variable only if it exists in CoffeeScript?

Anyone knows of a more concise/elegant way of achieving the following?
A = B if B?
Thanks.
EDIT:
I'm looking for a solution that references A and B once only. And would compile to
if (typeof B !== "undefined" && B !== null) { A = B; }
or something else similar.
To have this short helps have the following a bit more readable:
someObject[someAttribute] = (someOtherObject[someOtherAttribute] if someOtherObject[someOtherAttribute]?)
That is the motivation for my question.
You could say:
a = b ? a
For example, this:
a = 11
a = b ? a
console.log(a)
b = 23
a = b ? a
console.log(a)​
will give you 11 and 23 in the console (demo: http://jsfiddle.net/ambiguous/ngtEE/)
Maybe something like:
A=_ if (_=B)?
expanded:
if ((_ = B) != null) {
A = _;
}
This will overwrite A with what ever is in B, but only if it is not null, referencing both only once.
Not sure about Coffee Script but you can use the OR operator for this in regular javascript like so:
a = b || a

Using an existing IQueryable to create a new dynamic IQueryable

I have a query as follows:
var query = from x in context.Employees
where (x.Salary > 0 && x.DeptId == 5) || x.DeptId == 2
order by x.Surname
select x;
The above is the original query and returns let's say 1000 employee entities.
I would now like to use the first query to deconstruct it and recreate a new query that would look like this:
var query = from x in context.Employees
where ((x.Salary > 0 && x.DeptId == 5) || x.DeptId == 2) && (x,i) i % 10 == 0
order by x.Surname
select x.Surname;
This query would return 100 surnames.
The syntax is probably incorrect, but what I need to do is attach an additional where clause and modify the select to a single field.
I've been looking into the ExpressionVisitor but I'm not entirely sure how to create a new query based on an existing query.
Any guidance would be appreciated. Thanks you.
In an expression visitor you would override the method call. Check if the method is Queryable.Where, and if so, the methods second parameter is a quoted expression of type lambda expression. Fish it out and you can screw with it.
static void Main()
{
IQueryable<int> queryable = new List<int>(Enumerable.Range(0, 10)).AsQueryable();
IQueryable<string> queryable2 = queryable
.Where(integer => integer % 2 == 0)
.OrderBy(x => x)
.Select(x => x.ToString());
var expression = Rewrite(queryable2.Expression);
}
private static Expression Rewrite(Expression expression)
{
var visitor = new AddToWhere();
return visitor.Visit(expression);
}
class AddToWhere : ExpressionVisitor
{
protected override Expression VisitMethodCall(MethodCallExpression node)
{
ParameterExpression parameter;
LambdaExpression lambdaExpression;
if (node.Method.DeclaringType != typeof(Queryable) ||
node.Method.Name != "Where" ||
(lambdaExpression = ((UnaryExpression)node.Arguments[1]).Operand as LambdaExpression).Parameters.Count != 1 ||
(parameter = lambdaExpression.Parameters[0]).Type != typeof(int))
{
return base.VisitMethodCall(node);
}
return Expression.Call(
node.Object,
node.Method,
this.Visit(node.Arguments[0]),
Expression.Quote(
Expression.Lambda(
lambdaExpression.Type,
Expression.AndAlso(
lambdaExpression.Body,
Expression.Equal(
Expression.Modulo(
parameter,
Expression.Constant(
4
)
),
Expression.Constant(
0
)
)
),
lambdaExpression.Parameters
)
)
);
}
}
}