0

I'm trying to create a controller which waits for a promise who's deferred is resolved in a directive. But all I get is a silent failure, preventing both my controller and directive from executing or doing anything.

app.coffee

angular.module('myApp', [
    'ngRoute'
    'myApp.filters'
    'myApp.services'
    'myApp.directives'
    'myApp.controllers'
]).config [ '$routeProvider', ($routeProvider) ->

        $routeProvider.when '/',
            template: '<div></div>'
            controller: 'MyCtrl1'
            resolve:
                CM: ['CM', (CM) ->
                        CM
                    ]
        return
])

controllers.coffee

angular.module('myApp.controllers', [])
.controller('MyCtrl1', ['$scope', 'CM', ($scope, CM) ->
    # do stuff with a resolved CM
])

directives.coffee

angular.module('myApp.directives', [])
.directive('myDirv', ['CMd', (CMd) ->
    (scope, elm, attrs) ->
        CMd.CMObject.applyToDiv elm
        CMd.deferred.resolve CMd.CMObject
        scope.apply() # I've tried it with and without scope.apply()
        return
])

services.coffee

angular.module('myApp.services', [])
.service('CMd', ['$q', ($q) ->
    @CMObject = CMObject
    @deferred = $q.defer()
    return
])
.factory('CM', ['CMd', (CMd) ->
    CMd.deferred.promise 
])

Why does this cause both my controller and my directive to silently fail? How can I get my controller to wait for a resolve correctly?

zakdances
  • 22,285
  • 32
  • 102
  • 173
  • I noticed that I was mistaken you appear the be resolving against the deferred here `CMd.deferred.resolve`. I am not familiar with coffee script syntax. Is there anyway you could post a plunker example. I may be able to provide more help. – Jonathan Palumbo Oct 23 '13 at 18:17
  • @JonathanPalumbo Not sure how I'd do that without including much more code (such as `CMObject`), etc. But [here's a pastebin written in JS](http://pastebin.com/vd7VaT9W). It's relatively straight-forward code. – zakdances Oct 23 '13 at 18:27
  • I'm not entirely sure but to resolve some promise in your route from a directive does not seem like something that could be done. The directive does not technically even exist at that point because it has not navigated to the route yet. – Jonathan Palumbo Oct 23 '13 at 19:30
  • @JonathanPalumbo So directives aren't executed until after the controller executes? – zakdances Oct 23 '13 at 20:12
  • @JonathanPalumbo Ahhh, I see what you mean. The resolve option is attached to the routeProvider – zakdances Oct 23 '13 at 20:15

0 Answers0