2

I am creating an ionic application and in particular scenario when an employee is selected from a list(generated from a JSON data array), his detail is to be shown from here

var employees = [{"id": 325, "firstName": "James", "lastName": "King", "managerId": 0, "managerName": "", "reports": 4, "title": "President and CEO", "department": "Corporate", "cellPhone": "617-000-0001", "officePhone": "781-000-0001", "email": "[email protected]", "city": "Boston, MA", "pic": "James_King.jpg", "twitterId": "@fakejking", "blog": "http://coenraets.org"}.....

All i need is to find the index of ID to show the complete object.

     findById: function(employeeId) {
                var deferred = $q.defer();

                var searchValue = {'id': employeeId };
                index = -1;
                _.each(employees, function(data, idx) { 
                if (_.isEqual(data, searchValue)) {
                index = idx;
                return;
                }
                });

                var employee = employees[index];
                deferred.resolve(employee);
                return deferred.promise;
            },

for that i am using this function which is not taking debugger inside the _.each function

kindly tell me where m i wrong.

3 Answers 3

2

It's not going to work because expression _.isEqual(employees[xxx], {id: 325}) will never return a match.

Instead use _.find method

findById: function(employeeId) {
    var employee = _.find(employees, {id: employeeId});
    return $q.when(employee);
}
Sign up to request clarification or add additional context in comments.

3 Comments

I didn't know $q.when(). What is that doing?
$q promisifies the value. You should use it instead of deferred object.
Ah, that is helpful. My solution is a little laborious. Thanks.
0

I think you can simplify your function a little, because you are using lodash:

findById: funtion(employeeId) {
    return _.findWhere(employees, {id : employeeId});
}

Futhermore I do not see any reason to use promises here, but to go with that notion, you could try this:

findById: funtion(employeeId) {
    var deferred = $q.defer();

    $timeout(function() {
        var employee = _.findWhere(employees, {id : employeeId});

        if (found !== undefined) {
            deferred.resolve(employee);
        } else {
            deferred.reject();
    }, 0);

    return deferred.promise;
}

You need to return the promise deferred.promise before you resolve() or reject(), so you can wait for that in your callee.

Hope that helps!

Comments

0

I don't know if your really need the index. To work on a solution, which returns the employee at that moment, when you find the data, would be better and shorter. Than you should also jump out of the loop.

If your debugger is not jumping inside _.each maybe your employees array is empty. You could try something like this:

       findById: function(employeeId) {
            var deferred = $q.defer();  
            // deferred should be inside the scope of _.each
            // because it is defined here

            var searchValue = {'id': employeeId };
            //index = -1;
            // check your data here
            // for example console.log(employees.length)
            // or make a breakpoint here and check employees
            _.each(employees, function(data, idx) { 
            // make a console.log(idx) here to check your data
            if (_.isEqual(data, searchValue)) {
              deferred.resolve(data);
              return deferred.promise;;
            }
            });

        },

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.