Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a promise inside an object literal

In angular-translate version 2.0 the $translate service no longer returns the actual translation but a promise. I can see that is a good idea because there could be some asynchronous loading going on. But it confuses me how to use the service properly in my case, because I used the $translate service inside an object literal, like this

$scope.myDefs = [
      ...
      {
            field: 'supplier',
            displayName: $translate('Supplier'),
            cellTemplate: "<div class=\"ngCellText\">...</div>"
      },
      ...
      {
            field: 'supplierSize',
            displayName: $translate('Size'),
            width: 100,
            cellClass: "center"
      }
      ...
];

Question: How do I use a promise inside an object literal?

It is supposed to (according to the documentation) be used like this:

$translate('HEADLINE').then(function (headline) {
    $scope.headline = headline;
});
like image 817
swenedo Avatar asked Feb 23 '14 22:02

swenedo


3 Answers

If you know that there's no asynchronous stuff going on, you can use $translate.instant() which behaves exactly like $translate() in 1.x.

like image 184
Pascal Precht Avatar answered Nov 12 '22 12:11

Pascal Precht


You'd need to have a direct reference. Or a helper function that has closure over the reference. Like:

$scope.myDefs = [
    ...
    createArrayObject({
        field: 'supplier',
        displayName: $translate('Supplier'),
        cellTemplate: "<div class=\"ngCellText\">...</div>"        
    }),
    createArrayObject(.....

]

and elsewhere

function createArrayObject(obj){
    obj.displayName.then(function(data){
       obj.displayName = data;
    });
    return obj;  
}

Update

as Brian suggested below, Its always a good idea to write generic code you can use all over.

var forEach = angular.forEach,
    isFunction = angular.isFunction;

function resolveProperties(obj){
    forEach(obj,function(val,key){
        if(isFunction(val.then)){
            val.then(function(data){
                obj[key] = data;
            });
        }
    });
}

So you can use it like...

[
    resolveProperties({
        myPropertyToResolve: promiseReturningFunction() 
    }),
    ....   
]
like image 26
calebboyd Avatar answered Nov 12 '22 13:11

calebboyd


If you re using ui-grid, solution is to add headerCellFilter: 'translate' to columnsDefs (means myDefs) and displayName must have translation key.

Here is,

$scope.myDefs = [
      {
            field: 'supplier',
            displayName: "Supplier",
            cellTemplate: "<div class=\"ngCellText\">...</div>",
            headerCellFilter: 'translate'
      },
      {
            field: 'supplierSize',
            displayName: "Size",
            width: 100,
            cellClass: "center",
            headerCellFilter: 'translate'
      }
];
like image 35
mrtduzgun Avatar answered Nov 12 '22 11:11

mrtduzgun