Is there an event listener for when a feature layer's definition expression is udpated?

2064
4
Jump to solution
05-09-2017 06:14 PM
DanielPascual
New Contributor II

I'd like to get the count of features, without making another call the the server, after the definition expression of a feature layer is changed. I'm using JSAPI 4.2

Tags (4)
0 Kudos
1 Solution

Accepted Solutions
DanielPascual
New Contributor II

After reading through the docs more finally came up with a solution:

        service.getFeatureLayerViewCount = function (featureLayer) {
            var deferredFeatures = $q.defer();

            var layerViewPromise = _view.whenLayerView(featureLayer);
            layerViewPromise.then(function (layerView) {
                var handle = layerView.watch("updating", function (newVal, oldVal, prop, target) {
                    if (!newVal) {
                        // console.log(prop + " CHANGED FROM " + oldVal + " TO " + newVal);
                        layerView.queryFeatureCount().then(function (response) {
                            // console.log(response);
                            deferredFeatures.resolve(response);
                        }, function (error) {
                            console.error(error);
                            deferredFeatures.reject(error);
                        });
                        handle.remove();
                    }
                });
            });

            return deferredFeatures.promise;
        }

I'm using the angular-esri libraries, but the idea is the same. Call that after the definitionExpression is set. I don't think you can even get a count from the featurelayer, had to get it from the featurelayerview. I just had to save that extra call to the server.. Thanks!

View solution in original post

0 Kudos
4 Replies
RobertScheitlin__GISP
MVP Emeritus

Daniel,

   There is not. But a work around is to attach a listen for the update-end right before you use setDefinitionExpression and then remove the update-end event listener inside the handler function.

var updateEndHandlerEvt = featureLayer.on('update-end', updateEndHandler);

featureLayer.setDefinitionExpression("blah blah");

function updateEndHandler(){
  updateEndHandlerEvt.remove();
  //now do something since the definition expression is applied
}
DanielPascual
New Contributor II

Sorry, I should have been more clear in my main post. I'm on 4.2, I only put the version in the hashtag section. I'll update the main question.

I don't see that even in 4.2, I do see a "layerview-create" on the FeatureLayer, but I already tried that and it didn't hit it.

Any other ideas? Thanks!

DJ

0 Kudos
DanielPascual
New Contributor II

After reading through the docs more finally came up with a solution:

        service.getFeatureLayerViewCount = function (featureLayer) {
            var deferredFeatures = $q.defer();

            var layerViewPromise = _view.whenLayerView(featureLayer);
            layerViewPromise.then(function (layerView) {
                var handle = layerView.watch("updating", function (newVal, oldVal, prop, target) {
                    if (!newVal) {
                        // console.log(prop + " CHANGED FROM " + oldVal + " TO " + newVal);
                        layerView.queryFeatureCount().then(function (response) {
                            // console.log(response);
                            deferredFeatures.resolve(response);
                        }, function (error) {
                            console.error(error);
                            deferredFeatures.reject(error);
                        });
                        handle.remove();
                    }
                });
            });

            return deferredFeatures.promise;
        }

I'm using the angular-esri libraries, but the idea is the same. Call that after the definitionExpression is set. I don't think you can even get a count from the featurelayer, had to get it from the featurelayerview. I just had to save that extra call to the server.. Thanks!

0 Kudos
ArjunDongre
Occasional Contributor

I have this exact issue, but it doesn't seem that the event handler for "on-set-definition-expression-complete" has been added. Any other ideas for a simple implementation? It seems the definition expression will break out the query into batches. I need to know when they are all done resolving.

0 Kudos