2

I am a newbie in coffeescript. The following file main.js is generated after I include my coffeescript file functions.coffee:

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict';
require('../../modules/common/frontend/functions');



},{"../../modules/common/frontend/functions":2}],2:[function(require,module,exports){
//Link selection in menu
function setCurrentlink(){
    var currenturl=window.location.href;
    var part=currenturl.match(/\n\/displaywizard\/(.*)$/g);
    alert(part);
}

...

},{}]},{},[1])
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5vZGVfbW9kdWxlcy9icm93c2VyaWZ5L25vZGVfbW9kdWxlcy9icm93c2VyLXBhY2sv...

I have been trying to call the function setCurrentlink from html file as:

<script type="text/javascript">setCurrentlink();</script>

I tried putting the link to the js file in head section, at the bottom of the page but I am getting an error saying 'Can't find variable setCurrentlink'. Please help me find my mistake or anything I missed.

functions.coffee

#@export functions

#Link selection in menu

setCurrentlink = ->
  currenturl = window.location.href
  part = currenturl.match(/\n\/displaywizard\/(.*)$/g)
  alert currenturl
  return
...
Chetana Kestikar
  • 570
  • 6
  • 23

1 Answers1

1

Your function is defined within a function, so it's scope is limited to that function. You would need your function defined globally for this to work. That said, you're likely going about this the wrong way. If I had more info about what you're trying to accomplish I can advise a bit further.

Shawn Erquhart
  • 1,820
  • 2
  • 17
  • 30
  • Also, it looks like you're using a packaging utility that's protecting you from publishing to the global scope, which, as I alluded to, is generally a good thing. – Shawn Erquhart Jun 09 '15 at 12:28
  • Thanks @Shawn. I am using a framework called I-tier which is supported by node.js and coffeescript. The paths of the required JS/coffee files are to be mentioned in core/frontend/main.coffee as `require /path/to/file` and then after using `gulp watch` , the file main.js is generated in assets. All I want to do is call the function defined in one of the pointed coffee or js files. How do I do it? – Chetana Kestikar Jun 09 '15 at 12:38
  • It's requiring your scripts and placing them within an immediately invoking function expression (iife), which is used to limit variable scope. If you're positive that you want this function globally available, you can add it to the Window object. But again, please don't, your approach is misguided. I don't know anything about I-Tier, but you're likely working against the framework. It seems you're referring to Groupon's Interaction Tier framework. If you have a link to the framework's home page that would help. – Shawn Erquhart Jun 09 '15 at 12:50
  • Yeah you are right.. I am working with Groupon's I-Tier framework. Unfortunately the framework docs need login credentials which I cannot provide. – Chetana Kestikar Jun 09 '15 at 13:09
  • Gotcha. I would look in those docs and find out how you're supposed to do whatever you're trying to do - pretty sure they don't want you operating on the global scope like this. – Shawn Erquhart Jun 09 '15 at 14:43