angularjs $resource将查询资源列表转换为对象数组
angularjs $resource converting query Resource list to array of objects
我正在尝试从$resource查询的返回中创建对象数组,如以下SO问题所示:链接。但是,我不断得到相同的资源和其他元素列表。 我有一个 plunk:这里(您必须打开开发人员控制台才能看到输出。
var app = angular.module('plunker', ['ngResource']);
app.factory('NameResource', function($resource) {
var url = 'data.json';
var res = $resource(url, null, {
query: {
method: 'GET',
isArray: true,
transformResponse: function(data, headersGetter) {
var items = angular.fromJson(data);
var models = [];
angular.forEach(items, function(item) {
models.push(item);
});
console.log("models: ", models);
return models;
}
}
});
return res;
});
app.controller('MainCtrl', function($scope, NameResource) {
$scope.names = NameResource.query();
console.log('Inside controller: ', $scope.names);
setTimeout(function(){console.log('after some time names is:', $scope.names)}, 3000);
});
我做错了什么? 还是我误解了什么。 还有两者有什么区别?对我来说,它似乎非常相似。 什么时候会导致问题?
Resource.query
返回一个具有两个属性的数组(因为您创建的 isArray
标志),$promise
这是一个承诺,当解析时,它将"复制"所有响应值到从返回的数组Resource.query
神奇地更新视图和$resolved
这是一个标志,告诉$promise
是否已解析, 为了回答您的问题,实际上发生了一些额外的转换,从转换返回的数据实际上将经过另一个转换(无法禁用),这是每个对象转换为Resource
实例的地方。
所以这就是你期望发生的事情:
promise
.then(function (rawData) {
// this is where your transformation is happening
// e.g. transformResponse is called with rawData
// you return your transformed data
})
.then(function (transformedData) {
// raw data has gone through 1 transformation
// you have to decide what to do with the data, like copying it to
// some variable for example $scope.names
})
但Resource
正在执行以下操作:
promise
.then(function (rawData) {
// this is where your transformation is happening
})
.then(function (transformedData) {
// Resource is adding this handler and doing the
// 'copy' to array operation here for you,
// but it will actually create a Resource instance
// in the process with each item of your array!
})
.then(function (transformedDataV2) {
// raw data has gone through 2 transformations here!
})
额外的转换是魔术发生的地方,也是创建Resource
实例的地方,如果我们看一下源代码,这些是处理这种转换的行,我将在这里复制它们:
if (action.isArray) {
value.length = 0;
forEach(data, function(item) {
if (typeof item === "object") {
value.push(new Resource(item));
} else {
// Valid JSON values may be string literals, and these should not be converted
// into objects. These items will not have access to the Resource prototype
// methods, but unfortunately there
value.push(item);
}
});
}
data
是第一次转换返回的数据,如上所示,它将通过typeof item === 'Object'
检查,因此value
Resource.query
返回的数组将更新为新的Resource
项(而不是项)。你担心这个奇怪的Resource
对象,让我们分析一下Resource
构造函数:
function Resource(value) {
shallowClearAndCopy(value || {}, this);
}
它只是将对象的每个属性value
复制到this
(this
是新的 Resource 实例),所以现在我们处理的是Resource
对象而不是纯数组对象
它会导致问题吗?
我相信它会的,如果你定义的转换函数稍微复杂一点,比如让每个对象实际上是其他东西的实例,其__proto__
有一些方法,例如Person
对象而不是普通对象,那么Person.prototype
中定义的方法对整个操作的结果不可见,因为每个对象都不是Person
实例,而是Resource
实例!(在此 plunkr 中看到此错误,请务必阅读注释并查看由于未定义方法而在控制台中引发的错误)
- 如何在Javascript中将JSon对象转换为数组
- 使用JS将数组转换为json对象
- 如何使用json将对象列表从java转换为javascript
- 在Javascript中转换对象数组
- 如何将TypeScript对象转换为普通对象
- JavaScript代码问题:我正在将对象转换为数组
- Javascript如何转换对象数组
- 任何转换对象*值*的Javascript或Coffeescript“映射”函数
- 转换对象数组
- 转换对象或数组值的最佳方式
- 使用lodash转换对象的键和值的最优雅的方法是什么?
- Javascript/html5 2d Canvas Context -获取相对于Canvas的X, Y(相对于转换对象
- javascript,转换或强制转换对象
- 根据对象的不同,在转换对象时考虑旋转
- 在类中转换对象
- 转换对象数组的结构
- 用于在浏览器中转换对象(拖动、缩放、旋转和扭曲)的Javascript框架
- Javascript:通过lodash、下划线或corejavascript转换对象响应数组
- 如何转换对象数组
- 使用Lodash转换对象数组