0

How do I export the method of an object to call externally in the HTML page.

The JavaScript code:

my.namespace.FeedbackController = Class.create();
Object.extend(my.namespace.FeedbackController.prototype, {
 initialize:function(fid, fitem){
  this.fid = fid,
  this.feedback_item = fitem;
 },
 link_click : function(fstate) {
  alert(fstate);
 }
});
goog.exportSymbol('my.namespace.FeedbackController', my.namespace.FeedbackController);
goog.exportProperty(my.namespace.FeedbackController, 'link_click', my.namespace.FeedbackController.link_click);

On the page there are multiple items that people give feedback on. The HTML Code:

<script type="text/javascript">
  feedback_handlers = new Array();
  feedback_handlers[16] = new my.namespace.FeedbackController(16, 'useful');
</script>
<a href="javascript:feedback_handlers['16'].link_click('useful');">Useful</a>

When I click on the link 'Useful' I get TypeError: feedback_handlers['16'].link_click is not a function.

Am I missing something?

Chris
  • 816
  • 4
  • 11
  • 23

2 Answers2

2

link_click is a prototyped method.

Try:

goog.exportProperty(my.namespace.FeedbackController.prototype, 'link_click', my.namespace.FeedbackController.prototype.link_click);

See https://code.google.com/p/closure-library/source/browse/closure/goog/base.js#1538

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
TWL
  • 216
  • 1
  • 4
1

The way in which you extend your function prototype Object.extend is not recognized by Closure-compiler. You'll need to use direct prototype assignments:

my.namespace.FeedbackController.prototype.initialize = function() ...

If you compile with verbose warnings, you should get a warning about undefined properties.

Chad Killingsworth
  • 14,360
  • 2
  • 34
  • 57