0

I am trying to create an application page that allows users to watch and comment on YouTube videos imported through the YT API. In order to do so I have created a page template within a child theme that features a list of the videos, a YT player instance and custom player controls.

What I want to achieve is whenever the user selects a video from the list the video should load and, through a $resource call, populate a factory with the comments that go along with the video.

I have created a custom table that, amongst others, contains a column for the video_id. For the factory the furthest I have come up with:

angular.module("app")
    .factory("commentRepository",
        function($resource) {
            return $resource("/wp-content/themes/zerif-lite-child/inc/get_comments.php");
        });

In my controller I add a dependency for the factory and assign it to a $scope varaible.

get_comments.php:

<?php
require_once($_SERVER["DOCUMENT_ROOT"]."/wp-load.php");
function get_comments_by_id()
{
    if (!is_user_logged_in()) {
        echo json_encode("Not Authorised");
    } else {
        global $wpdb;
        $result = $wpdb->get_results("SELECT * FROM analyser_posts", OBJECT);
        echo json_encode($result);
    }
}
get_comments_by_id();

However, this returns the following error. If I browse directly to the php file I see a nice JSON array of the data. How do I get the data correctly into the factory?

0

Guess I should have read the error page more carefully, because it contained the answer in plain English. In the factory request is should simply match the request to the response signature (an array in my case), thus becoming:

angular.module("app").controller("MainController", ["$scope", "commentRepository", function ($scope, commentRepository) {
    commentRepository.query(function(data) {
        $scope.comments = data;
    });
}]);

Instead of using the get method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.