承诺成功不起作用

时间:2014-11-25 03:16:20

标签: angularjs coffeescript promise

我是棱角分明的新手并且为了结识而尝试了一些有趣的东西。我似乎无法承诺工作。更新操作正常。我没有在控制台上打印任何东西。

app.factory "Profile", [ 
    "$resource"
    "$q"
    ($resource, $q) ->
        Profile = ->
            @service = $resource("/users/profiles/:id.json", {id: @id}, 'update': {method: 'PATCH', params: {id: '@id'}, headers: {'Content-Type': 'application/json'}})
            return
        Profile::show = (userId) ->
            @service.get(id: userId)
        Profile::update = (updatedProfileObject) ->
            deferred = $q.defer()
            @service.update(id: updatedProfileObject.id, profile: updatedProfileObject).success = (response) ->
                deferred.resolve
                    message: "great success borat"
                return
            deferred.promise
        return new Profile
]


app.controller "MainCtrl", [
    "$scope"
    "$routeParams"
    "Profile"
    "$route"
    "$rootScope"
    "$q"
    ($scope,$routeParams,Profile,$route,$rootScope,$q) -> 
        $rootScope.$on "$routeChangeSuccess", ->
            $scope.profile = Profile.show($routeParams.id)
            return
        $scope.range = (min, max, step) ->
        step = (if (step is `undefined`) then 1 else step)
        input = []
        i = min
        while i <= max
            input.push i
            i += step
        input
        $scope.update = ->
            promise = Profile.update($scope.profile)
            promise.then = (response) ->
                console.log(response.message)
]

更新了修复控制器更新方法和工厂更新方法语法的代码,现在仍在使用。

更新的代码:

app.factory "Profile", [ 
    "$resource"
    "$q"
    ($resource, $q) ->
        Profile = ->
            @service = $resource("/users/profiles/:id.json", {id: @id}, 'update': {method: 'PATCH', params: {id: '@id'}, headers: {'Content-Type': 'application/json'}})
            return
        Profile::show = (userId) ->
            @service.get(id: userId)
        Profile::update = (updatedProfileObject) ->
            deferred = $q.defer()
            @service.update(id: updatedProfileObject.id, profile: updatedProfileObject).$promise.then (value) ->
                deferred.resolve();
            deferred.promise
        return new Profile
]


app.controller "MainCtrl", [
    "$scope"
    "$routeParams"
    "Profile"
    "$route"
    "$rootScope"
    "$q"
    ($scope,$routeParams,Profile,$route,$rootScope,$q) -> 
        $rootScope.$on "$routeChangeSuccess", ->
            $scope.profile = Profile.show($routeParams.id)
            return
        $scope.range = (min, max, step) ->
        step = (if (step is `undefined`) then 1 else step)
        input = []
        i = min
        while i <= max
            input.push i
            i += step
        input
        $scope.update = ->
            Profile.update($scope.profile).then (value) -> 
                console.log("lol") 

1 个答案:

答案 0 :(得分:0)

删除&#34; =&#34;在最后一行之前的那一行签名,然后进行回调

promise.then (response) ->
                console.log(response.message)