Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/knockout/2.2.1/knockout-min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <input type="text" data-bind="value: familly.person">
  <p data-bind="text: kodog.greeting()"></p>
  <pre data-bind="text: ko.toJSON($data, null, 2)"></pre>
  <button data-bind="click: function()  {familly.alert();}" >log person!</button>
  </body>
</html>
 
/*
// Plain JS, works as expected
// ###########################
var Animal = function() {
  var self = this;
  
  self.hello = function() {
     return 'The dog says';
  };
  
  self.greeting = function() {
    return self.hello() + '!!';
  };
};
var dog = new Animal();
dog.hello = function() {
  return 'Woff';
};
console.log(dog.greeting());
*/
// Knockout
// ########
var KOAnimal = function() {
  var self = {};
  
  self.hello = ko.computed(function() {
    return 'KO Not implemented';
  });
  
  self.greeting = ko.computed({
    read: function() {
        return self.hello() + '!!';
    },
    deferEvaluation: true
  });
  return self;
  
};
var familly = {
  person: ko.observable("Sanna"),
  alert: function() {
    console.log(kodog.greeting());
  }
};
var kodog = new KOAnimal();
kodog.hello = ko.computed(function() {
  return 'Hello ' + familly.person();
});
console.log(kodog.greeting());
familly.person('Eva');
console.log(kodog.greeting());
ko.applyBindings({familly: familly, kodog: kodog});
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
victor@cottin.sepro
0viewers