Angular.js与浏览器内RAML解析器集成

时间:2014-03-28 14:22:13

标签: angularjs rest raml

我在各种服务中使用了Angular中的$ http模块,但是想要将它与Mulesofts RAML解析器集成。这样可以让我不必更新角度的某些部分,以符合RAML规范。

有人这样做过吗?有哪些必要的步骤?

如果这是不可能的,我认为只需将$ resource模块用于RAML中描述的REST资源就不会太多工作。

1 个答案:

答案 0 :(得分:1)

将任何库集成到AngularJS中是完全可能的。

在你的情况下甚至更简单 - RAML模块使用$q.when将Angular将使用的承诺返回到它自己的承诺中,它会免费为您处理整个摘要周期集成问题。

RAML解析器的用法如下:

//example from the documentation
RAML.Parser.loadFile('http://localhost:9001/myAPI.raml').then( function(data) {
  console.log(data)
}, function(error) {
  console.log('Error parsing: ' + error);
});

你需要做的就是使用Angular很好地使用$q.when进行包装,它将整合到摘要周期中:

$.when(RAML.Parser.loadFile('http://localhost:9001/myAPI.raml')).then( function(data) {
  $scope.someProp = data;
}).catch(function(error) {
  throw error; //Angular promises log when you use throw instead of $q.reject
});

当然,对于干净的Angular代码 - 您应该将其提取到服务中。

关键是RAML模块和Angular都使用promise的Promises / A +投诉实现,所以非常简单地集成这两者。 RAML使用Q,而Angular使用名为$ q的降格版本。