embedded coffeescript in a coffeekup view in zappa - node.js

Is there a way to pass variable values (just for reading, obviously) from the app to the embedded coffeescript in a coffeekup view (in zappa), such as:
.... #render 'index', myVar:"something"
#view index: ->
coffeescript ->
$(document).ready ->
I need to get to myVar here...
It seems really slick if it would work... (as an alternative to embedded javascript)
Update: Here are a few attempts. I'm just trying to use the string in my embedded script as an id.
coffeescript ->
$(document).ready ->
id = myVar
coffeescript ->
$(document).ready ->
id = "#{myVar}"
coffeescript ->
$(document).ready ->
id = #myVar
coffeescript ->
$(document).ready ->
id = "#{#myVar}"
I've written some quick javascript in a script -> function (since it's just a string, I can interpolate the variables easily), so I've worked around it, but the coffeescript way would be cleaner. It makes sense that the scope would be lost on the browser side--I was just trying to figure out how to do it.

I guess by "script -> function", you mean something like:
doctype 5
html ->
body ->
h1 "Hello world."
script "myvar = \"#{#myvar}\""
coffeescript ->
console.log myvar
At least that's a hackish workaround. It could be done with objects as well, typically using JSON or similar for serialization.
But I would not categorize this as a "clean coffeescript way", so I guess the question still stands!
Just to elaborate a bit; The problem here is that we're executing coffeescript code in two contexts; one on the server which generates the html to send to the client, and the other on the client itself through in the context of the coffeescript -> construct.
The server side knows about locals and similar, so for the code that runs on the server, it's simple to evaluate the coffeescript based template and replace template variables with values and similar.
Not so on the client. First of all the client knows nothing about locals / template variables, it only sees whatever those contained when the page got rendered server side. And doing simple template expansion inside the coffeescript construct would not work either, as coffeescript would not know if you refer to a local variable on the client or a template variable on the server. In theory there could be special sequences that signal "expand template variable inside coffeescript code" as well, but then we're just - again - creating a new template language on top of coffeecup. It could be made to look like regular coffeescript of course, e.g. serverLocal(SOMEREF), where SOMEREF would be replaced with the corresponding value.
In theory, Coffeecup could support a construct that shared all (or a selection of) locals also as variables to be accessed client side, using a "hack" similar to the script hack I showed above, and it would probably be not too different, except using JSON or similar for supporting more kinds of data than simple strings.

Related

How do I find what files use a specific function or file in a Node project, using VSCode?

I'm working with a Node project using VSCode. I'd like to be able to follow the tree upwards from functions/files at a lower level. That is, if I have an exported function doSomething in file dosomething.js, I'd like to see what code calls this function, and/or see what code requires this file.
// index.js
const { doSomething } = require('./tricky-stuff')
// tricky-stuff.js
const doSomething = function() {}
module.exports = {doSomething}
If I'm browsing tricky-stuff.js, is there a built-in feature to VSCode that allows me to see usages of either the function doSomething or where the file tricky-stuff.js is required/imported, so that it would show me the file index.js in this case (as well as perhaps other files where tricky-stuff is used)? Is there any extension? I seem to recall that WebStorm could do this, but I can't remember. (I know I could search for strings, but that seems inelegant).
I think it largely depends on how much VScode knows about your code, via the TypeScript language service or JSDoc comments etc...
Typically you can right click on a function and select the peek all references or find all references options:
In this case, it will show you all the places where that function is called or referenced.

Interpolating values in HTML attributes - Pug (Jade)

I am trying to construct an anchor tag with a dynamic href attribute in Jade.
I did go through the docs and some SO questions but they didn't help me. This is what I tried.
a(href= "http://www.imdb.com/title/#{movie.imdb_id}") Know more
But it renders
http://www.imdb.com/title/#{movie.imdb_id}
rather than
http://www.imdb.com/title/tt1234567
However this works
a(href= "http://www.imdb.com/title/" + movie.imdb_id) Know more
and this too.
- var url = "http://www.imdb.com/title/" + movie.imdb_id;
a(href= url) Know more
What's wrong with the first version?
Interpolation is only available in text.
You need to use JS string concatenation for attributes:
a(href="http://www.imdb.com/title/" + movie.imdb_id) Know more
If you JavaScript runtime supports ES2015 template string, you can also use them (notice the backticks):
a(href=`http://www.imdb.com/title/${movie.imdb_id}`) Know more
Reference
the pug variable declaration doesnt work in this case using #{...}
the right syntax goes this way,
a(attributes) Know more
a(href="http://www.imdb.com/title/"+ movie.imdb_id) Know more
the attributes is an expression so it renders correcly, or you could use ES5 template literals with back quotes to render the variable along side the text which becomes
a(href=`http://www.imdb.com/title/${movie.imdb_id}`) Know more
note that when using back quotes with template literals your variable expression are enclosed in parenthesis and a leading $ sign, that is ${..expression..}
When you quote it simply tells pug "this is a string". That's basic JS. Interpolation works with #{'#{interpolation}'} too! is an example which renders "Interpolation works with #{interpolation} too!"
I don't have any knowledge about pug(jade)
But my guess is "a(your code)" is already a signal to pug(jade) that it is in the controller's scope already.. and "{variable}" is also an indicator that you are accessing controller's scope. so
a(href= "http://www.imdb.com/title/#{movie.imdb_id}") Know more
for "{}" inside a() is no longer an indicator that your are trying to access controller's scope because you're already in the controller's scope.. so "{}" inside a() is just a string, {movie.imdb_id} is part of the link string.
So in order for the framework to identity that movie.imdb_id is a variable, you should separate it from the actual string.
NOTE: This is just a guess..I'm using angular

Passing variable from jade to ng-init not working

I'm trying to pass an object from jade to ng-init in angular
This: doesn't work:
ng-init='tables=!{JSON.stringify(tables)}'
This: expands but,
ng-init='tables=#{JSON.stringify(tables)}'
the output is unescaped and filled with "s
ng-init="tables={"12":{"id":....
and the view isn't updated in either of the cases. This article implies that first one should work, but like I said, it doesn't even expand,
ng-init='tables=!{JSON.stringify(tables)}'
in source code shows up exactly the same in the HTML source
ng-init='tables=!{JSON.stringify(tables)}'
Actually, the #{...} approach seems to work fine.
It is probably the way console.log prints attributes' values that confused you.
ng-init="tables=#{JSON.stringify(tables)}"
Take a look at this short demo.
In what use-case you want to pass data directly from Jade to angular? I think you could to this job in controller like this :
$scope.init = function () {
// init stuff
}
...and in your view :
ng-init = init()

NodeJS: Keeping library files DRY

I've recently started working on a non-trivial project in CoffeeScript and I'm struggling with how best to deal with registering exports etc. I'm writing it in a very 'pythonesque' manner, with individual files effectively being 'modules' of related classes and functions. What I'm looking for is the best way to define classes and functions locally AND in exports/window with as little repetition as possible.
At the moment, I'm using the following in every file, to save writing exports.X = X for everything in the file:
class module
# All classes/functions to be included in exports should be defined with `#`
# E.g.
class #DatClass
exports[name] = item for own name, item of module
I've also looked at the possibility of using a function (say, publish) that puts the passed class in exports/window depending on its name:
publish = (f) ->
throw new Error 'publish only works with named functions' unless f.name?
((exports ? window).namespace ?= {})[f.name] = f
publish class A
# A is now available in the local scope and in `exports.namespace`
# or `window.namespace`
This, however, does not work with functions as, as far as I know, they cannot be 'named' in CoffeeScript (e.g. f.name is always '') and so publish cannot determine the correct name.
Is there any method that works like publish but works with functions? Or any alternative ways of handling this?
It's an ugly hack but you can use the following :
class module.exports
class #foo
#bar = 3
And then :
require(...).foo.bar // 3
The old
(function (exports) {
// my code
exports.someLib = ...
})(typeof exports === "undefined" ? window : exports);
Is a neat trick that should do what you want.
If writing that wrapper boilerplate is a pain then automate it with a build script.
What I'm looking for is the best way to define classes and functions locally AND in exports/window with as little repetition as possible.
It's impossible to do something like
exports.x = var x = ...;
without writing x twice in JavaScript (without resorting to black magicks, i.e. eval), and the same goes for CoffeeScript. Bummer, I know, but that's how it is.
My advice would be to not get too hung up on it; that kind of repetition is common. But do ask yourself: "Do I really need to export this function or variable and make it locally available?" Cleanly decoupled code doesn't usually work that way.
There's an exception to the "no named functions" rule: classes. This works: http://jsfiddle.net/PxBgn/
exported = (clas) ->
console.log clas.name
window[clas.name] = clas
...
exported class Snake extends Animal
move: ->
alert "Slithering..."
super 5

LuaLanes Unable to pass global variables between functions (in a single lane)

hope you're having a good day.
I have been programming a IRC chatbot in Lua the past few days, and finally I want to start implementing multiple server support into it.
So far, I have created the irc "object" to manage each server, and all that happy stuff - Now, I am having problems with threading.
Lua, as you probably know, doesn't have thread support built-in. LuaLanes and LuaThreads seemed like the closest to what I wanted to use, in terms of libraries. What made me choose LuaLanes is because LuaThreads requires a modified lua core. Plus the "lindas" feature of LuaLanes caught my eye (for later use in plugins)
Anyway, the issue that I'm having is when you generate a lane using function middleman (for example), which then calls another function sqr (for example). if you generate the function like lanes.gen({globals = _G}, middleman), middleman will have access to everything in your current global scope, which is exactly what I want. The problem is, if middleman calls sqr, and sqr uses something from the global scope, it throws an error, almost as if _G suddenly became empty.
Here's some example code I managed to throw together, using my example above:
require 'lanes'
function sqr()
print(5*5)
end
function middleman()
sqr()
end
gen = lanes.gen({globals = _G}, middleman)
print(gen()[1])
Produces an error saying:
tc#box:~$ lua lanestrouble.lua
lua: lanestrouble.lua:4: attempt to call global 'print' (a nil value)
stack traceback:
[C]: in function 'error'
./lanes.lua:190: in function <./lanes.lua:136>
lanestrouble.lua:13: in main chunk
[C]: ?
threading.c 399: pthread_cond_destroy(ref) failed, 16 EBUSY
Aborted
tc#box:~$
(By the way, I'm using linux)
However, if you change line 11 from gen = lanes.gen({globals = _G}, middleman) to gen = lanes.gen({globals = _G}, sqr), it works fine.
I've checked, and the same thing happens if you pass "*" (or any other option for the "libs_str" parameter) to load the default libraries.
I really wish there was something like Java's threading library for Lua, that's how I originally learned to use threads. (I know, not the most ideal environment I suppose)
Thanks, I appreciate help a lot. Especially since this has completely halted my IRC bot development! :(
Making sqr local does the trick. Since it becomes an upvalue of middleman, it is copied to the new lane.
require 'lanes'
local function sqr()
print(5*5)
end
function middleman()
sqr()
end
gen = lanes.gen({globals = _G}, middleman)
print(gen()[1])
But definitely something strange is going on. I mean, even when passing explictly "sqr" and "print" to the new lane, it won't see "print" (when called by "sqr"). It seems to me that something is not working right when serializing functions between lanes. You should contact its maintainer.

Resources