+1 vote
by (130 points)

I would like to build my own custom render function, as a test, I tried this code, but when I try it, the original function is used in the Passage instance. What is weird to me is that orender still exists on the instance as well and points to the correct function.

What would really be super is if I could actually get at the base render function, but it seems to exist in a scope that is only visible to the default Passage.render.

Passage.orender = Passage.render;
Passage.render = function(e) {
	console.log("Rendering " + e);
	Passage.orender(e);
};

Passage.prototype.orender = Passage.prototype.render;
Passage.render = function(e) {
	console.log("Rendering " + e);
	this.orender(e);
};

 

1 Answer

0 votes
by (159k points)

Your second example is close but if you look at the source code of the Passage module's render() function you will see that it actually returns a value, where as your monkey patched version doesn't.

The layer that is calling the render can't display anything if you don't give it something to display.

by (130 points)

Thanks. I just realized the main reason it isn't working as I expected (i.e. not replacing the function called by the Passage instance.

Passage.render = function(e) {
	console.log("Rendering " + e);
	this.orender(e);
};

should have been

Passage.prototype.render = function(e) {
	console.log("Rendering " + e);
	this.orender(e);
};

Is there any way to access the render function called by Passage.prototype.render? I don't think there is, but if I could customize that it would be ideal.

by (159k points)

I explained in my previous comment why your Passage.prototype.render example is not working, the code layer that calls the render(source) function expects it to return a (String) value because it is that calling code layer that does the actual rendering of that value.

If you look at the source code of the existing function you will see this.

by (2.5k points)

This should work:

var oldRender = Passage.prototype.render;

Passage.prototype.render = function() {
  console.log('Custom rendering');
  return oldRender.apply(this);
};

 

...