This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
How to write this javascript code in coffeeScript?
$('#id_tab a').click(function (e) {
$('ul.nav-tabs li.active').removeClass('active')
$(this).parent('li').addClass('active')
})
It works when i put it in my html.erb file
So I try this in the coffeeScript file:
$ ->
$('#id_tab a').click (e) ->
$('ul.nav-tabs li.active').removeClass 'active'
$(this).parent('li').addClass 'active'
And it doesn't work.
Please help!
There is a nice site called JS2Coffee which you can use to check your script. You'll see that your coffee script compiles to:
$(function() {
return $('#id_tab a').click(function(e) {
$('ul.nav-tabs li.active').removeClass('active');
return $(this).parent('li').addClass('active');
});
});
Instead, try this:
$ ->
$('#id_tab a').click (e) ->
$('ul.nav-tabs li.active').removeClass 'active'
$(this).parent('li').addClass 'active'
true
true
You can view this StackOveflow answer for more information, but Coffeescript always returns the last line.
Related
after several rounds of research, I found there is no clear answer about the situation like below:
I have a js file called 'AAA.js', and there is simple code in side like this:
var AAA = {
listenForMenuLayer: function () {
console.log("menu initiated");
$('.nav-menu').on('click', function() { console.log("menu clicked")});
}
init: function(){
this.listenForMenuLayer();
}
};
And in the dart, I wrote like below (using 'dart:js'):
js.context['AAA'].callMethod('init');
Then, when I run it, everything looks fine, the "menu initiated" shows properly, which means the 'listenForMenuLayer' is initiated, but when click on the '.nav-menu', there is nothing happened. (I check many times, there is no spelling error or else)
My question is: Can Dart accept this kind of initiating of external JS event? or we should re-write those JS events at all, please advise, many thanks.
Updates:
I found that if we write the js code like above, the jquery will not be initiated properly, which means all the features begin with '$' will not be functional.
guys, I update it to using 'package:js/js.dart';
#JS('AAA.init')
external void aInit();
then some where, just simply call after including:
aInit();
I'm using the twitter typeahead library. Version: 0.10.4
I have been able to bind events to the "opened", "selected" events but nothing happens when I bind the event "rendered", although it is in the documentation.
Has any of you guys come across this issue?
Here is the code I'm using:
typeAhead.on('typeahead:selected', function(e, suggestion) {
alert(0);return; // Shows the alert
})
typeAhead.on('typeahead:rendered', function() {
// Nothing happens
});
had the same issue, debugging I've found out this line
typeAhead.data().ttTypeahead.dropdown.datasets[0].onSync('rendered', function(){
console.log('rendered');
});
it's working for me and I aint found any better than this, without modifying typeahead libraries.
if you have more datasets, just change to a for loop.
I'm having troubles with this event too. I'm using version 0.11.1 and as far as I can see I think there's a kind of bug when passing arguments to the callback function:
if you use this handler:
function(obj, matches) {
console.log(matches);
}
you seem to get only one (the first one, of the several matched suggestions.
if you use this handler:
function(obj, match1, match2) {
console.log(match1);
console.log(match2);
}
you get two, and so on.
Actually all suggestions are passed as this handler prove:
function() {
console.log(arguments);
}
skipping the first slot, the remaining are the current suggestions, so I think this is a bug of the plugin.
I need to run a javascript function of the web page from ranorex.
So, I gave Add New Action -> User code and,
I wrote the following code in Recording1.UserCode.cs
public void greet()
{
WebDocument webDocument = "/doc";
webDocument.ExecuteScript("document.write('Hello World!');");
}
When I play the recording I got the following error in report:
"No element found for path '/doc' within 10s."
What is missing?
Or what is the right way to do it?
You question was answered in the following post.
http://www.ranorex.com/forum/ranorex-javascript-executescript-t4436.html
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have one g:select with an onchange event that fires a remoteFunction that is calling the request method as well, however, the callback JS function isn't been calling.
This is the g:select:
<g:select id="categories"
name="categories"
from="${Category.findAll('from Category where master is null')}"
noSelection="['':'- Selecione -']" optionKey="id" optionValue="description"
onchange="${remoteFunction(
controller: 'event',
action: 'subCategoriesJSON',
params:'\'id=\' + escape(this.value)',
onSuccess: 'updateSubs(data,textStatus)'
)}"
/>
The subCategoriesJSON is called as well, but there is no way to remoteFunction calls after that the updateSubs function. My page is rendering the scope of the function correctly on the head tag.
This is the function:
<script type="text/javascript">
$(document).ready(function() {
function updateSubs(data, textStatus) {
alert('call me!!!');
var subs = eval("(" + e.responseText + ")");
if (subs) {
$("span#saida").html(subs);
}
}
});
</script>
Any clue will be very welcome. Thanks a lot!
It looks like updateSubs is defined as a local function, which means it isn't accessible from the global scope. Basically move your updateSubs function out of the $(document).ready() - it doesn't need to be there really as there is no need for your code to wait until the dom is ready before defining that function.
That should solve your main problem, however you'll have to edit your code further as you are trying to access e.responseText when you have no access to a variable named e - this will just trip an error until it is fixed. I've commented the problem code for now.
Anyway, once updateSubs is in the global scope the onSuccess handler should be able to access it.
<script type="text/javascript">
function updateSubs(data, textStatus) {
alert('call me!!!');
//var subs = eval("(" + e.responseText + ")");
//if (subs) {
// $("span#saida").html(subs);
//}
}
// I'm not sure if you actually need this ready function..?
$(document).ready(function() {
// if you define a function in here it will only be accessible
// from within the scope of this function.
});
</script>
For further information about function scope, have a read about Nested functions and closures.
Is there another way to write this:
//javascript and jquery area
'<% if (Model.Fruit == MyEnum.Apple) { %>'
$("#PaymentType option").each(function(){
//blah
});
'<% } %>'
i'm not sure i like the quotes around the whole if statement
In your view:
var isApple = <% Model.Fruit == MyEnum.Apple %>;
In an included javascript file:
if(isApple) {
$("#PaymentType option").each(function(){
//blah
});
}
If the goal is to have VS continue to correctly check/auto-reformat javascript then you must use your trick or one similar to avoid the issue.
In one of the answers to the following related question there is text from Microsoft indicating that this is a known issue for which they do not yet have a good solution.
Visual Studio confused by server code inside javascript