【发布时间】:2013-08-20 18:08:21
【问题描述】:
我是 Angular 和 Deployd 的新手,想知道如何一起使用它们。
我发现 Deployd 网站中的示例很好,但它只消耗其余 API 数据,我想了解如何在 AngularJS 中将 Deployd 作为服务。例如,使所有客户端 API 与集合的最新数据保持同步,并在已部署的集合事件中可用。
我想出了下面的例子,我们可以看到我正在使用 $resource 来使用其余的 api,但是在控制器“MyCtrl”中,我正在调用 dpd,我想用它来利用http://docs.deployd.com/docs/collections/notifying-clients.md等功能
我真的很想看看一些例子,或者任何与此相关的建议!
感谢收看 :)
angular.module('questions', ['ngResource'])
.factory('Deployd', function(dpd){
return dpd;
})
.factory('EntriesService', function($resource){
return $resource('/entries', {});
})
.controller('MainCtrl', ['$scope', 'EntriesService', function($scope, EntriesService) {
$scope.title = "Q&A Module";
$scope.entries = [];
EntriesService.query(function(response){
$scope.entries = response;
});
$scope.addMessage = function() {
$scope.entries.push({
author: "myAuthor",
message: $scope.message
});
EntriesService.save({
author: "myAuthor",
message: $scope.message
});
};
dpd.comments.get(function(comments, error) {
comments.forEach(function(comment) {
console.log(comment);
});
});
}]);
【问题讨论】:
-
我从未使用过 Deployd,但一般来说,处理对服务器的调用的东西最好封装在服务中,并将任何 DOM 操作代码封装到指令中,如下所示:briantford.com/blog/angular-d3.html
-
感谢@shaunhusain 分享此链接!我会花时间去理解它,但我也会通过指令。例如,我想将 Deployd 封装在服务中的原因是,我想利用这些功能让我实时通知所有正在查看应用程序的客户端等。我基本上可以,只需抛出“ drd" 可以在全球范围内使用,但我认为也许我不应该这样做,应该吗?!
标签: javascript angularjs dependency-injection deployd