Required to use "resolve" when using promises?


#1

I posted the below on SO; anyone here have thoughts/workarounds for this, since Ionic is integrated with ui-router ?

I’m using Ionic Framework, which is integrated with ui-router, and I’ve found that models that are associated with promises via my controller aren’t updated in the UI when, eg, a REST call via $http returns. Is this happening because I’m not using “resolve” in the state setup ? Is it required to use “resolve” for this to work ?

If “resolve” is required for this to work, then I find it horrible that my app-related code is spread among “resolve” and my controller; why would that be necessary ? What about cases where it’s not desirable to have the framework wait for “resolve” to finish ( ie, make a bunch of network calls ) before my controller executes ?

pk


#2

I’m told on SO that my problem lies elsewhere;

Below, summary and test are set via MyCtrl. I’m seeing “my test” display properly, but not “summary.name”. I’m seeing that MySvc.get()'s callback executes as expected, but summary.name’s updated value doesn’t appear on the UI.

Any suggestions on why summary.name’s updated value isn’t appearing on the UI ?

app.js:

...
.state('tab.edit', {
    url: '/edit',
    views: {
        'tab-dash': {
            templateUrl: 'templates/MyTemplate.html',
            controller: 'MyCtrl'
        }
    }
})
...

MyTemplate.html:

     <label class="item item-input item-stacked-label">
         <span class="input-label">Name</span>
         <textarea rows="8" placeholder="Nothing yet." ng-model="summary.name"></textarea>
     </label>
     <label class="item item-input item-stacked-label">
         <span class="input-label">TEST</span>
         <textarea rows="8" placeholder="Nothing yet." ng-model="test"></textarea>
     </label>

controllers.js:

...
.controller('MyCtrl', function($scope, MySvc) {
  console.log("MyCtrl: entered");
  $scope.summary = MySvc.get(0);
  $scope.test = "my test";
...

MySvc.get(0) returns ( and I see this callback execute and change ret.name ):

return $http.get(url).then(function(response) {
    console.log("MySvc callback: response.data = %o", response.data);
    console.log("MySvc callback: response.data.name = " + response.data.name);
    ret = new MySvc(response.data);
    console.log("MySvc callback: ret.name = " + ret.name);
    return ret;
});

#3

You don’t need to use resolve. $http takes care of that for you. However, you’re not using the promise that is returned by $http.get. You can’t just assign it to $scope.summary. By doing that, you are assigning the promise to that scope property.

You need to change your controller to this:

.controller('MyCtrl', function($scope, MySvc) {

  console.log("MyCtrl: entered");

  MySvc.get(0).then(
    function(results) {
      $scope.summary = results
    })
  $scope.test = "my test";

#4

Thanks; I was previously using $resource to construct my service, which was taking care of that; moved away from it while integrating w/ Tastypie.

pk