Open mati-a opened 5 years ago
Agreed that this would be a big nice-have.
Yes I'm agree 🙂
+1 from me. Only thing that has kept me looking for other libraries currently.
meta is a trivial but useful field in the JSON:API specification. It's essential to fulfill the requirements of some major libraries, for example, Vuetify server-served datatables. I could include it easily overriding the get method in the interface like this.
import _ from "underscore";
import ActiveResource from "active-resource";
import MyCollectionResponse from "./MyCollectionResponse";
class MyCustomInterface extends ActiveResource.Interfaces.JsonApi {
get = function(url, queryParams = {}) {
var queryParams =
arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _this, data;
data = {};
if (queryParams["filter"] != null) {
data["filter"] = this.buildFilters(queryParams["filter"]);
}
if (queryParams["fields"] != null) {
data["fields"] = this.buildSparseFieldset(
queryParams["fields"],
queryParams
);
}
if (queryParams["include"] != null) {
data["include"] = this.buildIncludeTree(queryParams["include"]);
}
if (queryParams["sort"] != null) {
data["sort"] = this.buildSortList(queryParams["sort"]);
}
if (queryParams["page"] != null) {
data["page"] = queryParams["page"];
}
if (queryParams["limit"] != null) {
data["limit"] = queryParams["limit"];
}
if (queryParams["offset"] != null) {
data["offset"] = queryParams["offset"];
}
_this = this;
return this.request(url, "GET", data).then(
function(response) {
var built;
built = MyCollectionResponse.build(_.flatten([response.data])).map(
function(object) {
object = _this.buildResource(object, response.included, {});
object.assignResourceRelatedQueryParams(queryParams);
return object;
}
);
built.links(response.links);
built.meta(response.meta);
console.log(built.meta());
if (_.isArray(response.data)) {
return built;
} else {
return built.first();
}
},
function(errors) {
return Promise.reject(
_this.parameterErrors(errors.response.data["errors"])
);
}
);
};
}
export default MyCustomInterface;
I've coded my own CollectionResponse child class to include the meta field.
import _ from "underscore";
import ActiveResource from "active-resource";
class MyCollectionResponse extends ActiveResource.CollectionResponse {
meta(data = {}) {
if (!_.isEmpty(data) || !this.__meta) this.__meta = data;
return this.__meta;
}
}
export default MyCollectionResponse;
I don't use CoffeScript so my code is plain JavaScript. It could be great if @nicklandgrebe could include this enhancement.
Thanks for the library!
Is there a way to access to meta field? Im searching in the code but couldnt find any.