Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/lodash/4/lodash.min.js"></script>
<meta name="description" content="This bin is an example of a even better foreach loop">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.3.0/knockout-min.js"></script>
  <div data-bind="foreach: {data: things, as: 'thing'}">
    <div data-bind="text: thing.data"></div>
  </div>
  <button data-bind="click: moreThings">Do it!</button>
</body>
</html>
 
// noprotect
var ajaxResult = [];
_.times(10 * 1000, function (i) {
  ajaxResult.push(i);
});
function DataModel(data) {
  var self = this;
  self.data = data;
}
function ViewModel () {
  var self = this;
  self.things = ko.observableArray([new DataModel(1),new DataModel(2)]);
  
  self.moreThings = function () {
    var tempThings = self.things();
    _.forEach(ajaxResult, function (i) {
      tempThings.push(new DataModel(i + 2));
    });
    self.things(tempThings);
  }
}
ko.applyBindings(new ViewModel());
Output 300px

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

Dismiss x
public
Bin info
jlaustillpro
0viewers