0

I am trying to populate my model from backend(with label and messages) before my contoller get loads. My method is working fine it connects with backend and gets the data but when I am viewing that variable in controller it is coming as undefined. My variable is "Model"

This is my route file

mainApp
    .config(["$routeProvider", function ($routeProvider) {
        .when(AngularRoutesFactory.AIMSAdmin.SearchBookings, {
              templateUrl: aimsAdminViewBase + "Bookings/SearchBookings.html",
              controller: "SearchPerioperativeBookingController",
              resolve: {
                  "Model": function (BookingFactory) {
                      return BookingFactory.GetSearchModel();
                  }
              },
              requireAIMSAuthorizeUser: true
          })
          .otherwise({
              redirectTo: AngularRoutesFactory.MainApp.BaseUrl
          });
    }]);

My Factory is

mainApp.factory("BookingFactory", ["$location", "MainFactory",
function ($location, MainFactory) {
 bookingsFactory.GetSearchModel = function () {
        bookingsFactory.MainFactory.QueryAPI(apiEndpoint + "GetSearchModel", "GET", function (response) {
            bookingsFactory.SearchBookingCriteria = response;
            return bookingsFactory.SearchBookingCriteria;
        }, null, null, bookingsFactory.LangInfo.Message_GettingBookingModel);
    }
 return bookingsFactory;


}]);

And this is my controller

mainApp.controller("SearchBookingController", ["$scope", "BookingFactory", "$rootScope", "$location"
, function ($scope, BookingFactory, $rootScope, $location, Model) {
    $scope.bbb = Model;

}]);

2 Answers 2

1

Edit:

Try handling it this way:

mainApp.config(["$routeProvider", "$q", function ($routeProvider, $q) {
    .when(AngularRoutesFactory.AIMSAdmin.SearchBookings, {
          templateUrl: aimsAdminViewBase + "Bookings/SearchBookings.html",
          controller: "SearchPerioperativeBookingController",
          resolve: {
              Model: function (BookingFactory, $q) {

                  var deferred = $q.defer();
                  BookingFactory.GetSearchModel().then(
                            function (data) {
                                deferred.resolve(data);
                            }, function () {
                                deferred.reject();
                            }
                        );
                  return deferred.promise;
              }
          },
          requireAIMSAuthorizeUser: true
      })
      .otherwise({
          redirectTo: AngularRoutesFactory.MainApp.BaseUrl
      });
}]);
Sign up to request clarification or add additional context in comments.

8 Comments

It is still undefined..:(
Hmm, what about BookingFactory.GetSearchModel(); is it a promise?
yes it is cause I am passing my function to main factory which executes that function in then which i think returns promise
@AtulChaudhary You need to make sure that you return that promise from the resolve. Since you call several functions in the resolve, each of those functions needs to return the promise up to and including the function that is used in the resolve.
Does that mean I have to use $q method?
|
0

Took guidance from @Fedaykin and came up with following working solution. Please let me know if it is wrong

I just changed my factory method and resolve function by applying $q.defer method and got it working

Changed my factory GetSearchModel method with following code

bookingsFactory.GetSearchModel = function () {
        bookingsFactory.MainFactory.QueryAPI(apiEndpoint + "GetSearchModel", "GET", function (response) {
            deferred.resolve(response);
        }, null, null, bookingsFactory.LangInfo.Message_GettingBookingModel);
        return deferred.promise;
    }

What I did in route file

var bookingModel= function ($q, BookingFactory) {
    var deferred = $q.defer();
    BookingFactory.GetSearchModel().then(
              function (data) {
                  deferred.resolve(data);
              }, function () {
                  deferred.reject();
              }
          );
    return deferred.promise;
};
bookingModel.$inject = ["$q", "BookingFactory"];

Then in resolve all I did

 .when(AngularRoutesFactory.AIMSAdmin.SearchBookings, {
              templateUrl: aimsAdminViewBase + "Bookings/SearchBookings.html",
              controller: "SearchBookingController",
              resolve: {
                  "Model": bookingModel
              },
              requireAIMSAuthorizeUser: true
          })

And in controller voila I got the value

mainApp.controller("SearchBookingController", ["$scope", "InitializeMainFactory", "$rootScope", "$location", "Model"
, function ($scope, InitializeMainFactory, $rootScope, $location, Model) {

    $scope.Model = Model;

}]);

2 Comments

thats it! the only difference between this and the one I posted is that you put the function into the bookingModel variable, if you paste that in the resolve part you'll get the same result :D
you are absolutely right. I just did that to support minification and I think angular guys says that It is good a practice.

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.