KDC Menu

Wednesday, March 08, 2017

Vuex - Core Concepts (w/ Vue.js 2)

The learning is endless, but exciting.



The following 5-lesson series is an awesome primer on Vuex (Vue state management).





Monday, March 06, 2017

Code Logic for the Keep Track "Show All" Page

The link and code provided below is extracted from code I wrote for a Controller class used on my Laravel site, Keep Track.
IIRC my biggest achievement while creating Keep Track was on the item listing page. Sorting (and one level of sub-sorting) on separate/related tables, for me, was tricky. But I was able to figure out how to write a couple custom callbacks which accounted for a couple different sorting scenarios.
Laravel provides no advanced documentation for custom sorting (especially with eager loads). However, the underlying Laravel (sort) code, the PHP docs, and Google/Stack Overflow all helped a lot.
In the end, persistence prevailed over 2 or 3 challenges. Just, please don't ask me to do it again. Just kidding!... These challenges are the most rewarding.
This one section of my Controller code is provided on my GitHub 'snippets' page. The sorting particulars (shown partially below) are the code blocks at lines 167 and 183.

$itemListBase = $itemListBase->with($queryMapModelText)->get()
    ->sort(function($a, $b) use ($queryText, $orderDir, $orderCol) {

        $queryTextTitle = 'item_title';

        $queryTableName = explode(".", $queryText)[0];
        $queryFieldName = explode(".", $queryText)[1];

        if ($orderCol == "rating") { // Ratings are numeric; Don't compare as strings
            $testCase = -1;
        } else {
            $testCase = "";
        }

        $queryItem1 = is_null($a->$queryTableName) ? $testCase : (is_null($a->$queryTableName->$queryFieldName) ? $testCase : $a->$queryTableName->$queryFieldName);
        $queryItem2 = is_null($b->$queryTableName) ? $testCase : (is_null($b->$queryTableName->$queryFieldName) ? $testCase : $b->$queryTableName->$queryFieldName);

        if ($orderCol == "rating") {
            $sortResultVal = ($queryItem1 < $queryItem2) ? -1 : (($queryItem1 > $queryItem2) ? 1 : 0);
        } else {
            $sortResultVal = strcmp($queryItem1, $queryItem2);
        }

        if ($sortResultVal === 0) {
            $sortResultVal = strtolower($a->$queryTextTitle . "z") > strtolower($b->$queryTextTitle . "z") ? 1 : -1;
        } else {
            $sortResultVal = ($orderDir == 'asc') ? $sortResultVal : -1 * $sortResultVal;
        }
        return $sortResultVal;
    });