Why are my coffeescript functions not available from my HTML code ? - ruby-on-rails-3

I am using rails-backbone, coffeescript gems in my rails 3.2.6 project.
square = (x) -> x * x
alert square(5)
this is the blog.js.coffee script file it produces:
(function() { var square; square = function(x) {return x * x;}; alert(square(5));
I need to call the square() method in an other view file.
How can I call that?
Is there any thing wrong I am doing?

All your code in Coffeescript will be inside a self-invoking anonymous function.
To call it outside a file, just write:
window.square = (x) -> x * x
alert(square(5)) in an other function
The best you can do to not overuse window is a App object that will contain all your variables.
window.App={}
window.App.square= (x) -> x * x
and then alert(App.square(5))

Call it like a regular JavaScript function:
<script>
square(5)
</script>

Related

How to increment a variable in a Golang template? [duplicate]

How can you calculate something inside a html template of go?
For example:
{{ $length := len . }}
<p>The last index of this map is: {{ $length -1 }} </p>
Were the . is a map.
The code {{ $length -1 }} is not working, is there a way to achieve this?
You can't. Templates are not a scripting language. By design philosophy, complex logic should be outside of templates.
Either pass the calculated result as a parameter (preferred / easiest), or register custom functions which you can call during template execution, pass values to them and which may perform calculations and return any values (e.g. return param - 1).
For examples of registering and using custom functions, see:
Golang templates (and passing funcs to template)
How do I access object field by variable in template?
Iterate Go map get index.
The other answers are correct, you can't do it in the template themselves. However, here's a working example of how to use Funcs:
package main
import (
"fmt"
"html/template"
"os"
)
type MyMap map[string]string
func LastMapIndex(args ...interface{}) string {
if m, ok := args[0].(MyMap); ok && len(args) == 1 {
return fmt.Sprintf("%d", len(m) - 1)
}
return ""
}
func main() {
myMap := MyMap{}
myMap["foo"] = "bar"
t := template.New("template test")
t = t.Funcs(template.FuncMap{"LastMapIndex": LastMapIndex})
t = template.Must(t.Parse("Last map index: {{.|LastMapIndex}}\n"))
t.Execute(os.Stdout, myMap)
}
Playground: https://play.golang.org/p/YNchaHc5Spz
You can use a FuncMap like this. Once you define a function within a funcmap, you can use it in the HTML. In your case you could define a MapLength function or something similar that calculates the length of a given map and returns it for you. You can then call it in the template a bit like this:
<p>The last index of this map is: {{ .MapLength . }} </p>

I am designing a function in JS for scrolling dynamic page how can I pass document as an argument?

I am designing a JS function to achieve scrolling of dynamic page so I created a function in Scroll.feature as
#ignore
* def ScrollFunction()=
"""
function(document){
var height = document.body.scrollHeight
while(true){
window.scrollTo(0, document.body.scrollHeight)
var newHeight = document.body.scrollHeight
if (newHeight === height) {
break;
}
height = newHeight ;
}
}
"""
And From another feature file, I will call this function for scrolling, but how will I pass the document parameter to this function?
Sorry, you need to spend some time reading and understanding this: https://github.com/intuit/karate/tree/master/karate-core#karate-vs-the-browser
Even your understanding of Karate functions needs clarity: https://github.com/intuit/karate#multiple-functions-in-one-file
Now, document should always work as long as a driver has been initialized.
One hint is you can break up into pieces like this:
* def getHeight = function(){ return script("document.body.scrollHeight") }
And then you can use getHeight() in some other function. Also refer https://github.com/intuit/karate/tree/master/karate-core#function-composition
So please open a new question once you have tried again.

React-Native Map() data structure?

Does react-native not support javascript's Map()? The following code is returning an empty object:
const x = new Map()
x.set(1, 'a')
x.set(2, 'b')
console.log(x)
react-native only supports a subset of Javascript functionality as listed here. This may explain the reason for the unexpected behaviour.
To achieve this what you're requiring in your react-native app, you could use an object to map key/value pairs in a similar way like so:
const x = {};
x[1] = 'a';
x[2] = 'b';
console.log(x);

Using Global Function in Titanium

I am making Titanium mobile project where I want to make one global function which I can use throughout the application. For that I have created other .JS file where I have defined the function and I am including that .JS file where I need to use this function and I am successfully able to call the function.
But My question is :
Can I create new Window in that function? As I have added one Label and one MapView in that window but it is not showing, while at the start of function I have added alert('FunctionCalled'), it is showing me alert but not showing me the label I have added in the window.
So anybody can help me to find out whether we can open window through function. If yes then any sample example, so that I can find out what mistake I am making.
Thanks,
Rakesh Gondaliya
you approach CAN work but is not a best practice, you should create a global namespace, add the function to that namespace and then only include the file with the function once in app.js
// apps.js
var myApp = {};
Ti.include('global.js','ui.js');
myApp.ui.openMainWindow();
then we create a seperate file for our ui functions
//ui.js
(function(){
var ui = {};
ui.openMainWindow = function() {
// do open window stuff
// call global function
myApp.global.globalFunction1();
}
myApp.ui = ui;
})();
here is where we create our global functions, we will not have to include the file everywhere since we are adding it to our global namespace
//global.js
(function(){
var global = {};
global.globalFunction1 = function() {
// do super global stuff
}
myApp.global = global;
})();
this is a simple outline of how it can be implemented, I have a complete code listing on my blog
Yes you can create a new window or add a label or anything else. If you wanted to add a label to the current window then you would do:
var helloWorld = Ti.UI.createLabel({ text: "Hello World", height: "auto", width: 200 });
Ti.UI.currentWindow.add(helloWorld);
It won't matter where the code is executing because Ti.UI.currentWindow will be the active window regardless.

Access ExtJS grid's column model from other file

I have an ExtJS grid that has a button set up in it. The button triggers a function that's defined into other JS file that's included in the grid page. The function triggers ok but in that function I want to get the columns count like this:
grid.getColumnModel().getColumnCount()
The problem is that I get an error like: grid.getColumnModel is not a function.
In PHP I would make a "global $ext" and then access that function. How can I do this in Ext ? How can I access the grid from other file ? What needs to be defined ?
Thank you.
How did you define the grid object? Did you do it like this:
var grid = new Ext.grid.GridPanel(...);
If so, the grid object is not in global scope. Remove the "var" and see if it helps.
This looks like a scope issue. See variable scope in JavaScript.
Basically, you can do:
my_global_grid = ... // accessible in the current ~global~ context (document, window)
var my_local_grid = ... // accessible only in the function
window.my_window_global_grid = ... // accessible in the same window
You might also pass the grid object into your function as an argument:
function myFunction(arg1,arg2,grid){
...
var count = grid.getColumnModel().getColumnCount();
...
}