How to use the compiled property name ? There should be a goog.getPropertyName(...) simmilar to goog.getCssName(...) !

133 views
Skip to first unread message

Bernd Wessels

unread,
May 17, 2012, 8:08:45 PM5/17/12
to closure-lib...@googlegroups.com
Hi guys

Is there already a way to use the compiled property name, or would you be able to add this feature to the compiler please ?

Why is it useful :
Because at the moment, everybody who wants to implement dynamic binding to properties of a model, has to force the compiler not to minify the properties of the model.

This is really bad, especially since the models are usually also the objects that go forward and backward between server and client and unminified properties make each object sooo much bigger.
So having almost everything compiled and minified is definately the best solution. The Node.js WebServices can use the same Property Mapping File and even C# WebServices can easily use the Property Mapping File in a custom JavaScriptSerializer (I can provide the code to everybody who is interested in that).

What do we need :
...
MyModel.prototype.TheWayToLongPropertyName = 4711;
...
myView.bindProperty(myModel, goog.getPropertyName("TheWayToLongPropertyName"), function(oldValue, newValue){.....});

during compilation the compiler then replaces that to:

myView.bindProperty("dF");



A goog.getPropertyName function like that would help everybody who loves closure and wants the most advanced minification possible for everything (even data and model objects).

Is there a chance that we can get this in closure ?

Best regards
Bernd

John Lenz

unread,
May 17, 2012, 8:42:41 PM5/17/12
to closure-lib...@googlegroups.com
It currently looks like this:  

// Some code with type definitions
/** @construction */
function MyType(value) {this.someProp = value};

// Get the name of the property on some type.
var renamedPropertyName = goog.object.transpose(goog.reflect.object(MyType, {someProp: 0}))[0];



Where "MyType" is the type whose property you want the name of.  The is safe in the presence of type based property renaming passes:  http://code.google.com/p/closure-compiler/wiki/ExperimentalTypeBasedPropertyRenaming

Bernd Wessels

unread,
May 17, 2012, 10:28:22 PM5/17/12
to closure-lib...@googlegroups.com
Hi John

Thank you for the immediate answer. Great to know that there is already something hitting in the right direction.

Unfortunately this is not general enough to support all scenarios, especially for things like:

var something = { TooLongButSoBeautifulToRead: 4711, Lastname: 'Wessels' };
...
myView.bindProperty(something, goog.object.transpose(goog.reflect.object(something, {TooLongButSoBeautifulToRead: 0}))[0], function(...){...});

That doesn't work.

Hope you will add something that covers most of these cases ;)

For the time being I just wrote a small PostProcessor for my compiled output that replaces the property names where ever my code requires it. But that is obviously only a workaround.

Thanks, let us know when there is a solution.

Best regards
Bernd

John Lenz

unread,
May 18, 2012, 12:47:22 AM5/18/12
to closure-lib...@googlegroups.com
On Thu, May 17, 2012 at 7:28 PM, Bernd Wessels <wessel...@gmail.com> wrote:
Hi John

Thank you for the immediate answer. Great to know that there is already something hitting in the right direction.

Unfortunately this is not general enough to support all scenarios, especially for things like:

var something = { TooLongButSoBeautifulToRead: 4711, Lastname: 'Wessels' };
...
myView.bindProperty(something, goog.object.transpose(goog.reflect.object(something, {TooLongButSoBeautifulToRead: 0}))[0], function(...){...});

That doesn't work.

 
In what way does it not work?

Bernd Wessels

unread,
May 18, 2012, 12:57:22 AM5/18/12
to closure-lib...@googlegroups.com
The problem is that

var something = { TooLongButSoBeautifulToRead: 4711, Lastname: 'Wessels' }; 

is not a function and without annotation, it is just an anonymous (on the fly) object.

But

goog.reflect.object(something,  ...

requires a Function and probably even a function with proper annotation (not sure about that).

Cheers

Bernd

Nathan Wright

unread,
May 19, 2012, 10:04:29 PM5/19/12
to closure-lib...@googlegroups.com
You could try using Object as the constructor function argument, though I've never tried it myself.

It's my understanding that all properties of all objects will be renamed the same way unless you enable the experimental type based property renaming option of the compiler. Therefore, if I'm not mistaken, you should be able to get away with using *any* constructor function as the argument for goog.reflect.object — not that I'd recommend that.

A much better approach is to avoid using an ad-hoc object literal. Create your own type like this:

/** @constructor */
function Something(a, b) {
   this.TooLongButSoBeautifulToRead = a;
   this.LastName = b;
};

var PROP_NAME = goog.object.transpose(goog.reflect.object(Something, {TooLongButSoBeautifulToRead: 0}))[0];

var thing = new Something('A', 'B');
alert(thing[PROP_NAME]);

Also you'll probably want to set PROP_NAME only once, when the JS file is loaded. Calling transpose every time is rather inefficient.

Bernd Wessels

unread,
May 20, 2012, 6:04:49 PM5/20/12
to closure-lib...@googlegroups.com
Hi Nathan and John

Thank you both for the answers. You are probably right. After thinking about how to avoid dynamic anonymous objects and tidying up some of my concepts, it seems like a good way to go.

I only have to find a way to beautify "goog.object.transpose(goog.reflect.object(Something, {TooLongButSoBeautifulToRead: 0}))[0];" ;)

It is rather long and as you mentioned, nothing you should do to often.

Anyway, thank you, great to know that there is an active community out there.

Cheers
Bernd

John Lenz

unread,
May 21, 2012, 12:00:13 PM5/21/12
to closure-lib...@googlegroups.com
I've wanted to add a "getPropertyName(type, name)" primitive but it is tricky as it needs to be treated as a property name "escape" so a lot of optimization passes would need checks added and test cases.
Reply all
Reply to author
Forward
0 new messages