0
votes

Simple question , I want to query results on PIECES PAGES . Like what I did using Express + NodeJS + MongoDB + Mongoose is simply like this :

// GET /api/question/:id
router.get('/question/:id' , (req , res)=>{
    var id = req.params.id;

    if(!ObjectID.isValid(id))
    {
        return res.status(400).send();
    }

    Question.findById(id).then((question) => {
        if(!question){
            return res.status(400).send();
        }
        res.send(question);
    }).catch((err) => {
        res.status(400).send(err);
    });
});

Those codes are returning params of :id to search related _id (GET Method)

And then , I use browser side to get query using ajax :

function getQueryLink(q){
    return $.ajax({
            method: "GET",
            url: '/api/question/?q=' + encodeURIComponent(q),
            contentType: 'application/json',
            success: function (response) {
                var getListBlock = $("#questionsBlock");
                $("#questionsBlock").empty();    
                response.question.docs.forEach(function (questions) {
                    var id = questions._id;
                    getListBlock.append("<form id='" + id + "' class='questionBox'><input type='number' name='time' id='timeLittleBox' value='" + questions.time + "' onfocus='this.value=\"\"'><p style='font-size: 17px;color: #535353; display: inline-block; float:right; padding:3px;'>seconds</p><textarea class='list'>" + questions.questionString + "</textarea><input type='number' class='answer-box-edit' style='background-color: #eaad3a' id='trueAnswerBox' value='" + questions.answers[0] + "'><input type='number' class='answer-box-edit' id='falseAnswerBox1' value='" + questions.answers[1] + "'><input type='number' class='answer-box-edit' id='falseAnswerBox2' value='" + questions.answers[2] + "'><input type='number' class='answer-box-edit' id='falseAnswerBox3' value='" + questions.answers[3] + "'><div class='form-group'><label for='level' id='timeBoxLabelEdit'>Level of Question = <output class='rangeValue' id='rangevalue'>" + questions.level + "</output></div></label><input type='range' id='level' min='1' max='10' value='" + questions.level + "' oninput='rangevalue.value=value' onchange='rangevalue.value=value' /><br><button form='" + id + "' class='delete'>Delete</button><button form='" + id + "' class='update'>Update</button></form><hr>");
                });
                if (response.question.total >= 6) {
                    getListBlock.last().append("<div id='pagination' class='pagination'><a class='left-arrow' href='/'>❮ Previous</a><a class='right-arrow' href='/'>Next ❯</a></div>");
                }
                if (response.question.page == 1) {
                    $(".left-arrow").addClass("disabled");
                } else if (response.question.page == response.question.pages) {
                    $(".right-arrow").addClass("disabled");
                }
                $("a.left-arrow").on("click", function (e) {
                    e.preventDefault();
                    paginationLeft(response.question.pages, response.question.page);
                });
                $("a.right-arrow").on("click", function (e) {
                    e.preventDefault();
                    paginationRight(response.question.pages, response.question.page);
                });
                $('.questionBox').on('click', '.update', function (e) {
                    e.preventDefault();
                    var id = $(this).attr('form');
                    var questionString = $("form#" + id).find("textarea").val();
                    var answer1 = $("form#" + id).find("#trueAnswerBox").val();
                    var answer2 = $("form#" + id).find("#falseAnswerBox1").val();
                    var answer3 = $("form#" + id).find("#falseAnswerBox2").val();
                    var answer4 = $("form#" + id).find("#falseAnswerBox3").val();
                    var level = $("form#" + id).find("#level").val();
                    var time = $("form#" + id).find("#timeLittleBox").val();                    
                    var allAnswers = [answer1, answer2, answer3, answer4];
                    update(id, questionString, allAnswers, level ,time , q);
                });
                $('.questionBox').on('click', '.delete', function (e) {
                    e.preventDefault();
                    var id = $(this).attr('form');
                    var questionString = $("form#" + id).find("textarea").val();
                    deleteData(id, questionString , q);
                });
            }
        });
}

Now , in Apostrophe-CMS , I did not know where to start. Even to fetch params and send result back to API and able to use nunjucks to display with same req.data or req.piecesFilters . Let me put simple example of what I am currently doing for my own portfolio pieces. Here is my design for portfolio-pages : {} Finished Development in Github :

Portfolio Design

As you can see , I have my 'Search' Form in the design. But I want to search ONLY in Portfolio pieces (Query Portfolio Only). Please help me . I studied your code on self.indexPage . But still don't know how to return the query to that page only. Then I found addFilter method , which is somehow useful for custom search on each different pages. Help me :'(

And also , I want to search with Powerful regex . If let say I typed 'Blue' And also return 'Bluebird' . This would make powerful search query !

1
What is a portfolio? A piece? And what kind of page are you trying to add this functionality to? - Tom Boutell
FYI your task needs to take a callback and invoke that callback when it's done, otherwise it will never exit. - Tom Boutell

1 Answers

0
votes

Found an Answer

It turns out , I have to enable apostrophe-search in app.js . And make a form action in portfolio pages like /portfolio . And make my input name to search so that the overall URL would be localhost:3000/portfolio?search=some+search . Now it returns all the value in pieces which is great ! This is my form in indexAjax.html :

<form method="GET" id="portfolio-list" action="/portfolio">
<input type="text" name="search" placeholder="Search" data-search="on" /><input type="submit" value="Search" style="display:none" />
</form>

And my browser serialize data (Just in case) :

$("#portfolio-list").submit(function(){
    $.get('/portfolio', $("#portfolio-list").serialize(), function (result) {
        if(result.status === 'ok'){
            console.log("Form sent");
        }
    });
});