Skip to content
Nubgrammer
Menu
  • Home
  • Contact Me
Menu
State of mind

100DaysOfCode Day 3 – A State of Mind

Posted on August 19, 2020August 19, 2020 by Tyler Sells

Day three is officially over. I got a little bit done tonight, but I was also watching a riveting Gerard Butler movie. Not the best way to be productive but I’ve also worked on another project at my actual job for about 6 hours today, so I’m not going to beat myself up over it too bad. Plus, the more I do, the longer I have to make these posts.

Today’s update is all about states and how I use the Knockoutjs visible data-bind to display a given ‘page’.

Managing States

As you can see, my components are appearing to load one at a time when the associated button is pressed. However, thanks to Knockoustj, there is only one server call to do all of this AND all the components are on the page at all times… sort of. Let’s look at the dashboard component template:

dashboard.html

<div class="ngts-panel ngts-sidebar ngts-content">
    <div data-bind="component:{name:'dashboard-sidebar', params:states}"></div>
    <div>
        <div data-bind="visible:states.home">
            <h2>Home</h2>
        </div>
        <div data-bind="visible:states.photos">
            <div data-bind="component:'dashboard-photos'"></div>
        </div>
        <div data-bind="visible:states.albums">
            <div data-bind="component:'dashboard-albums'"></div>
        </div>
        <div data-bind="visible:states.posts">
            <div data-bind="component:'dashboard-posts'"></div>
        </div>
    </div>
</div>

Using the visible data-bind, I can control which components are visible using a variable. If that variable evaluates to true, we show it. Otherwise, it’s hidden. This is also great for allowing a user to switch back forth between each ‘page’ and never lose their place or form data.

To handle the variables responsible for showing a page, I’m using a states object. It looks like this:

dashboard.js

self.states = {};
self.states.home = ko.observable(true);
self.states.photos = ko.observable(false);
self.states.albums = ko.observable(false);
self.states.posts=ko.observable(false);
self.states.currentState = ko.observable(self.states.home);
self.states.changeState = function (state) {
    var currentState = self.states.currentState();
    currentState(false);
    state(true);
    self.states.currentState(state);
};

All of my states are defined as a knockout observable with a boolean value. My default state is home. I also have an object to keep track of the current state and a function that handles changing the current state.

What is an observable?

I’m glad you asked. Observables are what make the Knockoutjs world go round. Observables are designed to be updated and then tell everyone about it. So, if I want to update the state, I need the divs from dashboard.js to recheck the current state and act accordingly. There is also an observable array (ko.observableArray()) that we can use. It’s useful for binding a list of items to a select list and has other applications as well.

Because we can make our html elements change automatically, we have some real power with javascript and Knockoutjs.

State of the App

Now that we’ve defined the states of our dashboard, we need to be able to change them.

dashboard-sidebar.js

var koModel = function (params) {
    var self = this;
    self.title = "Dashboard Sidebar";
    self.states = params;
    self.buttons = [
        new link("Home","fas fa-home",self.states.home),
        new link("Photos", "fas fa-camera-retro", self.states.photos),
        new link("Albums", "fas fa-compact-disc", self.states.albums),
        new link("Posts", "far fa-comment-alt", self.states.posts)
    ];
    function link (text, icon,state) {
        var self = this;
        self.text = text;
        self.icon = icon;
        self.state = state;
        return self;
    };
};

In my dashboard-sidebar component, I’ve established some buttons with some display info and assign each one of them a state. I’m also pulling the params object into a new states object. This is a way to share information between the main dashboard component and the dashboard-sidebar component.

dashboard-sidebar.html

<div>
    <h2 data-bind="text:title"></h2>
    <ul data-bind="foreach: buttons">
        <li><button data-bind="click:function(){$component.states.changeState(state)}"><i data-bind="css:icon"></i><span data-bind="text:text"></span></button></li>
    </ul>
</div>

I use the foreach binding to loop through my buttons and create an unordered list containing each one. Each button also has a click binding that calls the changeState function of the relative states object (pulled from the dashboard component). Because I assigned different states when creating the buttons, it automagically knows which state to change just by passing in the state variable.

Passing the states object

To make it all work, we just have to make sure we pass the states object from the dashboard component into the dashboard-sidebar component, like so:

dashboard.js

<div data-bind="component:{name:'dashboard-sidebar', params:states}">

The component binding syntax changes just a little bit, but as you can see, the states object is assigned to the params property. The easiest way to make sure objects are being passed is to simply console.log(params) in the component you’re trying to pass them to.

Done for the day

That’s about it for today. I’m looking forward to getting into some more CSS to make my sidebar look good. I plan on getting very carried away with it.

Author: Tyler Sells

Github

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • More
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to email a link to a friend (Opens in new window) Email
  • Click to share on Reddit (Opens in new window) Reddit

Like this:

Like Loading...

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Follow me on Twitter

My Tweets

Github Repos

vtsells (Tyler Sells)

Tyler Sells

vtsells
http://www.nubgrammer.com
Joined on Jun 21, 2017
9 Public Repositories
100DaysOfCode
embers
MultiSelect
MVC-Project-Start
nubgrammer.com
PermIT
Spray
vtsells.github.io
Wizard
0 Public Gists

Categories

  • #100DaysOfCode (4)
  • ASP.NET (7)
  • ASP.NET Core (1)
  • ASP.NET MVC (3)
  • CSS (4)
  • General (13)
  • JS (3)
  • LESS (2)
  • Snippets (4)
  • Tools (4)
  • Tutorials (9)

Recent Posts

  • Creating a Knockout.js project on Codepen
  • 100DaysOfCode Day 3 – A State of Mind
  • 100DaysOfCode Day 2 – The Building Blocks
  • 100DaysOfCode Day 1 (Sort of cheated already)
  • Committing to #100DaysOfCode
© 2025 Nubgrammer | Powered by Superbs Personal Blog theme
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT
%d

    Privacy Policy