dataFilter/dataTransform not work by using coffeescript

You must Login before you can answer or comment on any questions.

I just found when i use coffeescript, dataFilter/dataTransform does not work:

index.xml

<Alloy>
    <Collection src="product"/>
    <Window id="window">
        <TableView id="table" dataCollection="product" dataFilter="doFilter">
            <Require src="row"/>
        </TableView>
    </Window>
</Alloy>
index.coffee
$.getView().open()
Alloy.Collections.product.fetch()
 
doFilter = (collection) ->
    Ti.API.info 'go to filter products'
    return collection.models
and the generated javascript is:
var doFilter;
$.getView().open();
 
Alloy.Collections.product.fetch();
 
function doFilter(collection) {
  Ti.API.info('go to filter products');
  return collection.models;
};
you will never see 'go to filter products' is printed.

But it works when i modify the javascript as below:

var doFilter;
$.getView().open();
 
Alloy.Collections.product.fetch();
 
function doFilter(collection) {
  Ti.API.info('go to filter products');
  return collection.models;
};
Will this problem be solved?

— asked 2 years ago by Yong Gu
0 Comments

4 Answers

Accepted Answer

The translations we don't have control of is why we don't officially support coffeescript. Had coffeescript not created the undefined variable instance var doFilter at the top of your code, your function doFilter() would actually be in scope and you would not have to move the fetch to the bottom. So without coffeescript, this code would work:

// remove the unneeded variable reference
// var doFilter;
 
$.getView().open();
 
// this will work now
Alloy.Collections.product.fetch();
 
// this function will now be usable by the previous fetch
function doFilter(collection) {
  Ti.API.info('go to filter products');
  return collection.models;
};

And here is the error log:

[ERROR] Error triggering 'click' event: TypeError: 'undefined' is not a function (evaluating 'doFilter(Alloy.Collections.product)')

it is my fault, the solution is move Alloy.Collections.product.fetch() to the bottom.

Looks like a CoffeeScript Bug that has been fixed in the current CoffeeScript version. When I run the above CoffeeScript code through the converter, the function now gets defined as

doFilter = function(collection) {
for which the var declaration actually makes sense.

Your Answer

Think you can help? Login to answer this question!