2014-06-25 37 views
1

我想在$ resources URLs中设置一个动态参数。

我发现this有用的文章,但遗憾的是它并没有解决我的问题。 $资源函数中仍未填充cid。

messageApp.factory('conversationService', function($http) { 
    var data = {cid: ''}; 
    $http({method: "GET", url: '/myApp/resources/conversationwizard'}, {cache: true}) 
      .success(function(d) { 
       data.cid = d; 
      }) 
      .error(function(data) { 
       alert("Could not initiate conversation" + data); 
      }); 
    return data; 
}); 

messageApp.factory('messagesService', function($resource) { 
    return $resource('/myApp/resources/message/all', {}, { 
     query: {method: 'GET', isArray: true} 
    }); 
}); 

messageApp.factory('messageEventPoller', function($http, $timeout, conversationService) { 
    var data = {data: ''}; 
    var poller = function() { 
     $http.get('/myApp/msgnotification?cid=' + conversationService.cid).then(function(r) { 
      data.data = r.data; 
      $timeout(poller, 1); 
     }); 
    }; 
    poller(); 
    return data; 
}); 

messageApp.factory('createMessageService', function($resource, conversationService) { 
    return $resource('/myApp/resources/message?cid=:cid', {cid: conversationService.cid}, { 
     create: {method: 'POST'} 
    }); 
}); 

messageApp.factory('messageService', function($resource, conversationService) { 
    return $resource('/myApp/resources/message/:id?cid=:cid', {cid: conversationService.cid}, { 
     show: {method: 'GET'}, 
     update: {method: 'PUT', params: {id: '@id'}}, 
     delete: {method: 'DELETE', params: {id: '@id'}} 
    }); 
}); 

任何指针将不胜感激。

回答

3

您遇到的问题是使用单独的异步 ajax请求从服务器获取conversationService.cid

当您配置时:$resource('/myApp/resources/message?cid=:cid', {cid: conversationService.cid},响应未到达尚未和conversationService.cid仍为空。

不幸的是,对此没有简单的解决方案。在这里我建议一些方法:

1)使用从jQuery的同步Ajax和async : false:(并不好,因为它阻止浏览器线程)

messageApp.factory('conversationService', function($http) { 
    var data = {cid: ''}; 

    $.ajax({ 
     url: '/myApp/resources/conversationwizard', 
     async : false 
     }) 
     .done(function(d) { 
      data.cid = d; 
     }) 
     .fail(function(){ 
      alert("Could not initiate conversation" + data); 
     }); 
    return data; 
}); 

2)装入'/myApp/resources/conversationwizard'成一个全局对象自举角之前(使用jQuery ajax)。并且在您的conversationService中,您可以分配data.cid = yourglobal;

3)用角承诺:

messageApp.factory('conversationService', function($http,$q) { 
    var deferred = $q.defer(); 

    var data = {cid: ''}; 
    $http({method: "GET", url: '/myApp/resources/conversationwizard'}, {cache: true}) 
      .success(function(d) { 
       data.cid = d; 
       deferred.resolve(data); 
      }) 
      .error(function(data) { 
       alert("Could not initiate conversation" + data); 
      }); 

    return deferred.promise; 
}); 

messageApp.factory('createMessageService', function($resource, conversationService) { 
    var deferred = $q.defer(); 

    conversationService.then(function (data){ 
     var createMessageService = $resource('/myApp/resources/message?cid=:cid', 
     {cid: data.cid}, 
     {create: {method: 'POST'} 
     }); 

     deferred.resolve(createMessageService); 
    }); 

    return deferred.promise; 
}); 

但这将使用的资源加上复杂的代码。例如

messageApp.controller("testCtrl",function (createMessageService){ 
    createMessageService.then(function(service){ 
     //use your service here 
    }); 
}); 
+0

好的,理解。不过,我不知道如何解决这个问题,我最近才刚开始使用Javascript和angularJS。你会如何解决这个问题? – dngfng

+0

也不应该缓存:真正解决这个问题? – dngfng

+0

@dngfng:恐怕我们没有解决这个问题的办法,因为我们无法将已解决的promise作为依赖项注入到'factory'中。解决方法可能是:在引导角度之前(使用jQuery ajax)将''/ myApp/resources/conversationwizard''加载到全局对象中。在你的'conversationService'中,你可以分配'data.cid = yourglobal;'。 –