Chris
Chris

Reputation: 814

Export property of an object

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?

Upvotes: 0

Views: 1816

Answers (2)

TWL
TWL

Reputation: 216

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

Upvotes: 2

Chad Killingsworth
Chad Killingsworth

Reputation: 14411

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.

Upvotes: 1

Related Questions