undefine is not a function jquery auto complete - autocomplete

When trying to handle the data returned from autocomplete result set this always happens.
Any clue on this issue?
$("#search").autocomplete(suggest_url,{
max:100,
delay:10,
selectFirst: false
}).result(function(event, data, formatted)
{
do_search(true);
});
Thanks

Such error happens only if you are trying to call a function, but it is not a function.
x = 123;
x(); // produces «number is not a function
y = undefined;
y(); // produces exactly «TypeError: undefined is not a function»
«Uncaught type error» indicates that exception caused inside the lambla-style function. Thus, I hope, it is something wrong with do_searh identifier. Try to alert(do_searh) before calling it.
>>> UPDATE
This questions refers to another:
jQuery UI Autocomplete .result is not a function woes

Related

Error when trying to get single item from element.all in Protractor

I have the following test code (a simplified example):
e = element(by.id('element-id'));
it('description', function(){
e.all(by.tagName('my-directive')).then(function(items){
expect(items.count()).toEqual(3); //error
expect(items.length).toEqual(3); //ok
expect(items.get(0).getAttribute('my-attr')).toEqual('1'); //error
});
});
This is the HTML:
<div id="element-id">
<my-directive my-attr="1"></my-directive>
<my-directive my-attr="0"></my-directive>
<my-directive my-attr="0"></my-directive>
</div>
When I run this test I get the following error:
TypeError: undefined is not a function
I stripped it down and found out that the error is from the get() function and the count() function. I have read about the functions in the Protractor API and used them the same way as the example on the site, so I don't understand why it does not work for me.
Does anyone know what I'm doing wrong?
I have also tried this (included 'element'):
e.element.all(by.tagName('my-directive')).then(function(items){...})
But that gave an error event without the get() function.
items in this case is just an ordinary array. You get
TypeError: undefined is not a function
because there isn't any function on the Array.prototype called count.
count() and get() can only be called on ElementFinderArrays, which is a Protractor specific object: http://angular.github.io/protractor/#/api?view=ElementArrayFinder
If you want to get the length and one of the elements' attribute, this will work:
e.all(by.tagName('my-directive')).then(function(items){
expect(items.length).toEqual(3);
expect(items[0].getAttribute('my-attr')).toEqual('1');
});
or like this:
expect(e.all(by.tagName('my-directive')).count()).toEqual(3);
expect(e.all(by.tagName('my-directive')).get(0).getAttribute('my-attr')).toEqual(1);

How to define a default argument value for a method in as2?

look at this code :
function a2j(trusted:Boolean=true):String
{
...
}
compiler will not accept this code in flash actionscript 2.
It looks like AS2 doesn't force you to supply the all the arguments that a function declares. At the bottom of this help page, they state that arguments you do not supply are undefined ... and that any extra arguments you supply are ignored.
Also, the answer to this question shows that you can use the arguments keyword (an Array) to work with the parameters that are passed into the function.
So for a default value, as in your example above, you could do something like this:
function methodThatHasADefault(value:Boolean):void
{
if (arguments.length == 0)
value = true;
// do something
}

Correct way to return function value instead of binding in Coffeescript

I can't seem to find a concise answer to this question. What is the correct coffeescriptic way to return the value from _otherInstanceMethod when calling #_instanceMethod instead of the function binding itself?
x = _instanceMethod: () ->
#_otherInstanceMethod key: 'value'
Edit (thanks commenters)
This returns:
x = function () {
[...] # function body omitted
});
Instead of
x = 'some value returned by _otherInstanceMethod'
I would like the value to be returned instead of the function binding to _otherInstanceMethod
Being totally new to Coffeescript, this was my fault. I was calling the instance method like:
#_instanceMethod
instead of
#_instanceMethod()
Sorry for the trouble, voting to delete
In CoffeeScript #something translated into this.something regardless of the underlying variable type. This means you can use # only in conjuction with properties, with methods you still ought to use good old this.

Unable to modify Coffeescript's global variables in a JQuery listener

Today I was migrating some of my javascript code into coffeescript and got stuck in something really silly but even though I didn't know how to make it work.
I wanted to update the value of a global variable when a click event was triggered, have a look at the code below to see one of my guesses
Here's the code
#activeObject = null
# Some other code
$ ->
$('#header').click ->
if !headerSelected
showMenu '#header-menu', event
else
#activeObject = "#header"
showMenu '#menu-style-header', event
Unfortunately even though the click event was triggered the variable was not getting updated.
I came up with a work around. I created a function that set the value of the variable and called it instead of the assignment and this time it worked.
I just wanted to know why I wasn't able to do it the other way. For me it was a simple operation and it seemed silly to define a new function just for this.
Your problem is that # (AKA this) inside the click handler isn't the same as it is outside so this:
#activeObject = null
and this:
#activeObject = "#header"
are referring to two different activeObjects. You should be able to bind everything with => to get the right this:
$ =>
$('#header').click =>
#...
or better (IMHO), just refer to window.activeObject directly in both places so that it is obvious to everyone that you're referring to a global variable:
window.activeObject = null
$ ->
$('#header').click ->
if !headerSelected
showMenu '#header-menu', event
else
window.activeObject = "#header"
showMenu '#menu-style-header', event
Alternatively, you could stop using globals altogether in favor of, perhaps, a data attribute:
$ ->
$('#header').data 'activeObject', null
$('#header').click ->
if !headerSelected
showMenu '#header-menu', event
else
$(#).data 'activeObject', '#header'
showMenu '#menu-style-header', event
I think the confusion is about the usage of #, which is basically just a shortcut for this.
If you compile your code and see what CoffeeScript compiler it produces, the confusion becomes clear
this.activeObject = null;
$(function() {
return $('#header').click(function() {
if (!headerSelected) {
return showMenu('#header-menu', event);
} else {
this.activeObject = "#header";
return showMenu('#menu-style-header', event);
}
});
});
if activeObject is global you whould reference to it
window.activeObject = null
and
window.activeObject = "#header";
in both occurences in this code, cause one might be tempted to use it without window in second occurence, but that will cause a new local variable to be implecitly defined.
Generally when starting with CoffeeScript, its usefull to try small snipets like this in
http://coffeescript.org/ on the Try Now Tab

; expected but <place your favourite keyword here> found

I'm trying to write a class for a scala project and I get this error in multiple places with keywords such as class, def, while.
It happens in places like this:
var continue = true
while (continue) {
[..]
}
And I'm sure the error is not there since when I isolate that code in another class it doesn't give me any error.
Could you please give me a rule of thumb for such errors? Where should I find them? are there some common syntactic errors elsewhere when this happens?
It sounds like you're using reserved keywords as variable names. "Continue", for instance, is a Java keyword.
You probably don't have parentheses or braces matched somewhere, and the compiler can't tell until it hits a structure that looks like the one you showed.
The other possibility is that Scala sometimes has trouble distinguishing between the end of a statement with a new one on the next line, and a multi-line statement. In that case, just drop the ; at the end of the first line and see if the compiler's happy. (This doesn't seem like it fits your case, as Scala should be able to tell that nothing should come after true, and that you're done assigning a variable.)
Can you let us know what this code is inside? Scala expects "expressions" i.e. things that resolve to a particular value/type. In the case of "var continue = true", this does not evaluate to a value, so it cannot be at the end of an expression (i.e. inside an if-expression or match-expression or function block).
i.e.
def foo() = {
var continue = true
while (continue) {
[..]
}
}
This is a problem, as the function block is an expression and needs to have an (ignored?) return value, i.e.
def foo() = {
var continue = true
while (continue) {
[..]
}
()
}
() => a value representing the "Unit" type.
I get this error when I forget to put an = sign after a function definition:
def function(val: String):Boolean {
// Some stuff
}