Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <link href='https://fonts.googleapis.com/css?family=Linden+Hill' rel='stylesheet' type='text/css'>
  
<header class="hero-unit">
  <h1>Player profile</h1>
</header>

<section id="player-profile">
  <div class="sidebar">
    <h2>
      <span data-bind="text: fullName"></span> <small class="bad-status" data-bind="text: status"></small>
    </h2>
    <div class="avatar">
      <img src="http://www.barbarianmeetscoding.com/images/barbarian-meets-knockout/barbarian-avatar.png">  
    </div>
    <btn class="btn btn-large btn-block" data-bind="click: changeName">Change Player Name Via Code</btn>
    <btn class="btn btn-large btn-block btn-primary" data-bind="click: saveChanges, visible: canSave">Save</btn>
    <label>Dispose all computeds</label><input type='checkbox' data-bind="checked: disposeAll"/>
  </div>
  <div class="inventory">
    <h2>Inventory <small data-bind="text: inventoryWeight"></small></h2>
    <!-- ko foreach : inventoryItems -->
      <div class="inventoryItem">
        <p data-bind="text: name"></p>
        <img data-bind="attr: {src: image}"/>
        <p data-bind="text: weight"></p>
        <btn class="btn btn-small" data-bind="click: $parent.dropFromInventory">drop</btn>
      </div>
    <!-- /ko -->
  </div>
</section>


<footer>
  <section id="debugging" class="clearfix">
    <h4>View model dump</h4>
    <div data-bind="dump: $data"></div>
  </section>
</footer>

              
            
!

CSS

              
                body{
  text-align: center;
  font-family: 'Linden Hill', serif;
}

header{
  min-width: 650px;
  margin: 0px;
}

footer{
  margin-top: 550px;
}

section#player-profile{
  max-width: 60%;
  min-width: 650px;
  margin: 0 auto;
}

section#debugging{
  max-width: 60%;
  min-width: 650px;
  margin: 0 auto;
}

/*** Avatar ***/

div.sidebar{
  max-height: 300px;
  max-width: 300px;
  float: left;
}


div.avatar{
  -webkit-border-radius: 150px;
  -moz-border-radius: 150px;
  border-radius: 150px;
  border: 2px solid #beb2b2;
  margin: 20px;
}


div.avatar img{
  -webkit-border-radius: 150px;
  -moz-border-radius: 150px;
  border-radius: 150px;
}

/*** Inventory ***/

div.inventory{
  max-width: 50%;
  margin-left: 50%;
}

div.inventory .inventoryItem{
  float: left;
  width: 25%;
  height: 25%;
  margin: 2%;
}

div.inventory .inventoryItem img{
  width: 100%;
}

/*** Misc ***/

.bad-status{
  color: red;
}
              
            
!

JS

              
                // Dump custom binding
// From John Papa in Essential Knockout and Javascript Tips (Pluralsight)
// I tried to find a gist of it already from him but no luck, so here it is for anyone else who may want it
 
// Usage in HTML
// <div data-bind="dump: $data"></div>
// <div data-bind="dump: $root.results"></div> (if results was an observable/observableArray)
 
ko.bindingHandlers.dump = {
    init: function (element, valueAccessor, allBindingsAccessor, viewmodel, bindingContext) {
        var context = valueAccessor();
        var allBindings = allBindingsAccessor();
        var pre = document.createElement('pre');
 
        element.appendChild(pre);
 
        var dumpJSON = ko.computed({
            read: function () {
                var en = allBindings.enabled === undefined || allBindings.enabled;
                return en ? ko.toJSON(context, null, 2) : '';
            },
            disposeWhenNodeIsRemoved: element
        });
 
        ko.applyBindingsToNode(pre,
          {
              text: dumpJSON,
              visible: dumpJSON
          }
          );
        return { controlsDescendentBindings: true };
    }
};

// Stubs for services
var app = {
  playerRepository: {
    save: function(){
      var ajaxCall = $.Deferred();
      function resolveAjaxCall() {
        ajaxCall.resolve();
      };
      setTimeout(resolveAjaxCall, 1000);
      return ajaxCall;
    }
  }
}

// Model
var player = {
  name: "Kull",
  title: "The Conqueror", 
  avatarImage : "http://www.barbarianmeetscoding.com/images/barbarian-meets-knockout/barbarian-avatar.png",
  inventory: [
    { 
      name: "rusty broadsword",
      image: "http://www.barbarianmeetscoding.com/images/barbarian-meets-knockout/broadsword.png",
      weight: "10 st"
    },
    { 
      name: "magic potion",
      image: "http://www.barbarianmeetscoding.com/images/barbarian-meets-knockout/potion.png",
      weight: "1 st"
    },
    { 
      name: "hunter axe",
      image: "http://www.barbarianmeetscoding.com/images/barbarian-meets-knockout/axe.png",
      weight: "20 st"
    },
    { 
      name: "assassin's bone knife",
      image: "http://www.barbarianmeetscoding.com/images/barbarian-meets-knockout/assassins-knife.png",
      weight: "3 st"
    }
  ]
};

// ViewModel
var PlayerViewModel = function(){
    var self = this;
    self.name = ko.observable(player.name);
    self.title = ko.observable(player.title);
  self.fullName = ko.computed(function(){
        return self.name() + ', ' + self.title();
    });
    
    self.inventoryItems = ko.observableArray(player.inventory);
    self.inventoryWeight = ko.computed(function(){
      //calculate weight
      var totalWeight = 0;
      ko.utils.arrayForEach(self.inventoryItems(), function(item) {
        var value = parseInt(item.weight);
        if (!isNaN(value)) {
            totalWeight += value;
        }
      });
      return totalWeight + " stones";
    });
    self.status = ko.computed({ 
      read: function(){
        var totalWeight = parseInt(self.inventoryWeight());
        if (!isNaN(totalWeight) && totalWeight > 20)
          return "(overstrained)";
        return "";
      },
      disposeWhen: function(){
        return self.disposeAll;
      }});
    self.dropFromInventory = function(item){
      self.inventoryItems.remove(item);
      self.hasChanged(true);
    };
    self.changeName = function(){
      self.name("John Doe");
    };
    // change trancking system
    self.hasChanged = ko.observable(false);  
    self.isSaving = ko.observable(false);
    self.canSave = ko.computed(function(){
      return self.hasChanged() && !self.isSaving();
    });
    self.name.subscribe(function(newValue){
       self.hasChanged(true)
    });
    self.saveChanges = function(){
      self.isSaving(true);
      app.playerRepository.save(self).done(function(){
        self.hasChanged(false);
        self.isSaving(false);
        console.log('saved!');
      });
    };
    
    self.disposeAll = false;
    // To dispose explicitely
    self.dispose = function(){
      self.fullName.dispose();
      self.status.dispose();
      self.inventoryWeight.dispose();
    };
};
    
var playerViewModel = new PlayerViewModel();

// Run knockout!
ko.applyBindings(playerViewModel);
    


              
            
!
999px

Console