jeffsebring / angular-wp-api Goto Github PK
View Code? Open in Web Editor NEWAn AngularJS Client for WP-API
An AngularJS Client for WP-API
Looking at the way queries are constructed with this library, I feel that the param1
, param2
etc convention feels a bit arbitrary. Query levels (or directories, or what have you) will always start at the root (param1) and go until you don't specify another key; I'm not aware of a situation where you'd have a query for param1
and param3
without the intermediate param2
.
Given this linearity, have you considered using an array for structuring these queries? As an example,
wpAPIResource.get( {
param1: 'users',
param2: wpAPIData.user_id
} );
would become
wpAPIResource.get([ 'users', wpAPIData.user_id ]);
// or
wpAPIResource.get([
'users',
wpAPIData.user_id
]);
This may avoid some confusion around the param1, param2 naming convention by flat-out removing it: You're now specifying a sequential number of URL parts that are assembled to create a final query.
Alternatively, you could introduce a more verbose query syntax in the manner of an ORM library like knex. That may be overkill for the time being!
I'm attempting to use this API for some dynamic post population and am running into an issue in configuring it.
JS:
var dbModule = angular.module('database', ['wp.api']);
dbModule.controller('ResultsController', function(){
wpAPIResource.query({
param1: 'posts'
});
});
PHP:
add_theme_support('angular-wp-api', array('angular', 'angular-route', 'angular-resource'));
It throws this error every time:
ReferenceError: wpAPIResource is not defined
at new <anonymous> (db.js?ver=4.2.2:4)
at Object.invoke (angular.js?ver=4.2.2:4219)
at $get.extend.instance (angular.js?ver=4.2.2:8525)
at angular.js?ver=4.2.2:7771
at forEach (angular.js?ver=4.2.2:334)
at nodeLinkFn (angular.js?ver=4.2.2:7770)
at compositeLinkFn (angular.js?ver=4.2.2:7149)
at compositeLinkFn (angular.js?ver=4.2.2:7152)
at publicLinkFn (angular.js?ver=4.2.2:7028)
at angular.js?ver=4.2.2:1460
Dependencies are set properly as far as I can tell and WP-API is enabled. I am running this on a multi-site, could that be causing issues?
Thank you
The plugin does nothing if WP-API is not installed.
Add a dashboard message for administrators when constant JSON_API_VERSION
is not defined.
Hi,
I'm trying to utilize angular-wp-api, and have some issue with 'wpAPIData'.
I read your answer to @joao-parana, but still not fully understand the way of solution.
I already added within my wordpress json-rest-api/plugin.php the code
you specified as is,
but not sure where to add the theme support for 'angular-wp-api' ??
then on running angular app the ctrl recognized 'wpAPIResource' but not 'wpAPIData' obj, receiving this error:
ReferenceError: wpAPIData is not defined
.controller('SliderCtrl',function($scope, $timeout, $ionicSlideBoxDelegate, $http, wpAPIResource, wpAPIData) { ... }
where is the mistake?
Hi Jeff,
this might not be an issue but I'm unable to use filters in my param.
This works in my controller..
$scope.newsletters = NewslettersFactory.query({
param1: 'posts'}, function(data) {
console.log("Got all", data);
});
But I'd like to specify posts?filter[category_name]='news'
How is this possible?
On https://github.com/jeffsebring/angular-wp-api#examples you comments that
** If the user is not logged in, the request will recieve a 403 Forbidden response.
but I'm not be able to find information in README how can I pass username and password in request.
Can you explain this. Tanks.
OK exactly this issue.. http://stackoverflow.com/questions/24833940/wp-api-custom-post-type-with-custom-taxonomy
I've solved this once but my given solution now fails me..
Please send me in the right direction, anyone help....
I don't know how to resolve this issue.
wpAPIResource is injected successfully but wpAPIData not !
My controller:
// Wordpress Resource Support
app.controller('WPRemoteFetchCtrl',
function ($scope, wpAPIResource, wpAPIData) {
$scope.wordpress = {};
$scope.wordpress.root = wpAPIResource.get();
$scope.wordpress.post1 = wpAPIResource.get( {
param1: 'posts',
param2: 1
});
$scope.wordpress.user = wpAPIResource.get( {
param1: 'users',
param2: wpAPIData.user_id
});
});
Any Ideas ?
I've enabled the "AngularJS WordPress API" plugin but the page is not loading the 'angular-wp-api.min.js' javascript file.
What is going wrong?
Note:
When I run wpAPIResource.get(); I get the error "Can't find variable: wpAPIResource"
Noticed the last commit was 2 years ago. Is this still under development at all? Saw you had mentioned a v2 in some comments and wondered if I should wait before using in a project.
How can i edit / create custom metas?
and is it possible to edit MULTIPLE metas at once?
Hello @jeffsebring how do go about passing query parameters to the /post endpoint, like so:
/post?type=&context=edit
the syntax is driving me mad...
The current approach with ngResource forces you to use the ugly :param1/:param2
approach. I think it would be better to use Restangular instead. With Restangular you could create a service like this:
module.factory('wpAPIResource', function(Restangular) {
return Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.setBaseUrl(wpAPIData.base);
RestangularConfigurer.setDefaultHeaders({ 'X-WP-Nonce': wpAPIData.nonce });
});
});
And then use it like this (example taken from Restangular README):
// Restangular returns promises
wpAPIResource.all('users').getList() // GET: /users
.then(function(users) {
// returns a list of users
$scope.user = users[0]; // first Restangular obj in list: { id: 123 }
})
// Later in the code...
// Restangular objects are self-aware and know how to make their own RESTful requests
$scope.user.getList('cars'); // GET: /users/123/cars
// You can also use your own custom methods on Restangular objects
$scope.user.sendMessage(); // POST: /users/123/sendMessage
// Chain methods together to easily build complex requests
$scope.user.one('messages', 123).one('from', 123).getList('unread');
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.