javascript - Meteor.call() format js object into correct rest query -
i trying api call wordpress rest api. working call console looks this:
http://dev.thomastraum.com/wp-json/posts?type=tt_works&filter[work_categories]= all&filter[posts_per_page]=1
a "handwritten" working call meteor looks this:
return meteor.http.call("get", settings.wpdomain + "/wp-json/posts", {params: {'type':'tt_works','filter[work_categories]':'all','filter[posts_per_page]':'1'}});
now question is, how can pass javascript objects meteor call , end being in right format. me above calls should represented this:
archivequery = { type:'tt_works', filter:{ work_categories:'all', posts_per_page:1 } };
but if pass
return meteor.http.call("get", settings.wpdomain + "/wp-json/posts", {params:archivequery);
it returns me posts type parameter of tt_works
, ignores rest of query. tried ejson.stringify(archivequery)
formats query differently, {}
.
the query format need doesn't common one. think easiest solution format object yourself:
var fillquery = function(query, prefix, object) { _.each(object, function(val, key) { var k = (prefix) ? prefix + '[' + key + ']' : key; if(_.isobject(val)) { fillquery(query, k, val); } else { query[k] = '' + val; } }); return query; }; var objecttoquery = function(object) { return fillquery({}, null, object); };
see here in action.
Comments
Post a Comment