I am trying out AngularJS 1.5.7,  Restangular, UI-Router, with a Rails api.

I have defined a factory that allows me to use the following in a 
controller:

    this.jewels = JewelsResource.getAll()

However, the above for some reason does not assign to 'this.jewels'
unless I have

   console.log(this.jewels) 

...below it. Just seems interesting, and having looked at the documentation 
a couple times,
not really seeing where I am off base.

Here is my factory, where I define the above method:

angular.module('jewelsStore')
  .controller('JewelsCtrl', [ "JewelsResource", (JewelsResource) ->
    this.jewels = JewelsResource.getAll()
    console.log this.jewels
  ])

Here is the  complete jewels controller:

angular.module('jewelsStore')
  .factory('JewelsResource', ['Restangular', (Restangular) ->
      getAll: ->
        Restangular.all('jewels').getList().$object
      getOne:  (jewelsId) ->
        Restangular.one('jewels', jewelsId).get().$object
  ])

Here is my app declaration, config, states setup:

jewelsStore = angular.module('jewelsStore', ['restangular', 
'templates','ui.router', 'ng-rails-csrf'])
 
jewelsStore.config(['RestangularProvider', '$stateProvider', 
'$urlRouterProvider', '$locationProvider', (RestangularProvider, 
$stateProvider, $urlRouterProvider, $locationProvider)->
  RestangularProvider.setBaseUrl '/api'
  $stateProvider.state('home',
    url: '/'
    views:
      '':
        templateUrl: 'home/home.html'
        controller: 'MainCtrl'
      'jewels@home':
        templateUrl: 'jewels/jewels.html'
      'gallery@home':
        templateUrl: 'gallery/gallery.html'
        controller: 'JewelsCtrl'
        resolve:
          jewels: (JewelsResource) ->
            JewelsResource.getAll()
  )
  $stateProvider.state("jewels",
    url: "/jewels"
    templateUrl: 'jewels/jewels.html'
    controller: 'JewelsCtrl'
    resolve:
      jewels: (JewelsResource) ->
        return JewelsResource.getAll()
  )
  $locationProvider.html5Mode(true)
  $urlRouterProvider.otherwise '/'
])

Was my understanding that by using $object would enable 'promise unwrapping'
and this is all I would need.

So the console.log(this.jewels) is a mystery to me as to why that somehow 
makes
everything golden!

If I remove the line
   console.log(this.jewels)

this.jewels

is empty.

Any hints on this appreciated.

Thanks!
   Jet

plunker:   http://plnkr.co/edit/OuhI8Q1z3Jn8mfBMXVPa
     

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to