刚刚学习 ember.js,并开始使用 ember-app-kit 构建应用程序.
我正在使用提供的 api-stub 来 stub api,但我决定读取外部 json 文件以用作“夹具数据”。做出此决定的原因 - 我将使用相同的 json 文件来测试我的 ember 应用程序以及 Rails api 后端。 Here's要点以及我的 json 文件示例。
所以我的 api-stub/routes.js
目前看起来像这样:
有效实现
var path = require('path');
var fs = require('fs');
// going to sibling directory
var fixtureDir = path.resolve(__dirname, '../..', 'lightfixtures')
module.exports = function (server) {
// Create an API namespace, so that the root does not
// have to be repeated for each end point.
server.namespace('/api', function () {
var locationsProtocol = JSON.parse(fs.readFileSync(fixtureDir + '/locations.json'));
var roomsProtocol = JSON.parse(fs.readFileSync(fixtureDir + '/rooms.json'));
// stub location list request
server.get(locationsProtocol[0].request.url, function (req, res) {
res.send(locationsProtocol[0].response.data);
});
// stub individual location request
server.get(locationsProtocol[1].request.url, function (req, res) {
res.send(locationsProtocol[1].response.data);
});
// stub room list request
server.get(roomsProtocol[0].request.url, function (req, res) {
res.send(roomsProtocol[0].response.data);
});
// stub individual room request
server.get(roomsProtocol[1].request.url, function (req, res) {
res.send(roomsProtocol[1].response.data);
});
});
};
因此,在读取文件并解析其中的部分内容以 stub 适当的请求时工作正常 - 但看起来非常多余。我想迭代解析的 json 并为所有元素生成 stub ,以使其干燥并轻松添加更多 stub ,如下所示:
期望的实现
...
var locationsProtocol = JSON.parse(fs.readFileSync(fixtureDir + '/locations.json'));
var roomsProtocol = JSON.parse(fs.readFileSync(fixtureDir + '/rooms.json'));
function respondToEach(json) {
for (var i = 0; i < json.length; i++) {
server.get(json[i].request.url, function (req, res) {
res.send(json[i].response.data);
});
}
}
respondToEach(locationsProtocol);
respondToEach(roomsProtocol);
...
但是失败了! Ember 抛出一个默认错误,并在网络 Pane 中声称它收到内部服务器错误 (500),响应部分显示“TypeError: Cannot read property 'response' of undefined”(指示的行是这个 - res.发送(json[i].response.data);
我很困惑 - 我认为工作实现和“所需”实现应该产生完全相同的输出 - 但事实并非如此。有人可以帮助我理解我在哪里失败了(即重构版本有何不同),以及我该如何解决这个问题?
最佳答案
您遇到了臭名昭著的 javascript 循环闭合问题:) 在每个匿名函数内 i
最终将成为 json
的 length
> 评估时。
这样的东西应该有效
function respondToEach(json) {
for (var i = 0; i < json.length; i++) {
(function(idx){
server.get(json[idx].request.url, function (req, res) {
res.send(json[idx].response.data);
});
})(i);
}
}
或者你可以定义另一个函数来解决闭包问题
function realResponse(item){
server.get(item.request.url, function (req, res) {
res.send(item.response.data);
});
}
function respondToEach(json) {
for (var i = 0; i < json.length; i++) {
realResponse(json[i]);
}
}
关于unit-testing - ember-app-kit - 使用外部文件中的数据 stub api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21396281/