Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Custom ViewModel event bindings">
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
 <style>
   my-input {
     display: inline-block;
     
   }
   my-input div {
     height: 18px;
     width: 140px;
     background-color: white;
     border: solid 1px black;
   }
  </style>
  
<div id='out'></div>
<script id="my-input-template"
        type="text/stache">
 <div 
   ($keydown)='updateValue(%event)'
   tabindex='0'>{{value}}</div> 
</script>
  
<script id="app" type="text/stache">
<person-page>
  <person-edit (saved)="addMessage('Saved')" {person}="person">
    <form ($submit)="save(%event)">
      <h3>Name: {{person.name}}</h3>
      <p> Name: <input {($value)}="person.name"/>
        <input type='submit' value="save"/>
      </p>
    </form>
  </person-edit>
  <ul>
  {{#each messages}}
    <li>{{.}}</li>
  {{/each}}
  </ul>
</person-page>
</script>
  
  
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.js"></script>
<script src="//canjs.com/release/2.3.2/can.jquery.js"></script>
<script src="//canjs.com/release/2.3.2/can.stache.js"></script>
</body>
</html>
 
can.Component.extend({
  tag: "person-page",
  viewModel: {
    person: {name: "Justin"},
    messages: [],
    addMessage: function(message){
      console.log("addMessage");
      var messages = this.attr("messages")
      messages.unshift(message);
      
      setTimeout(function(){
        messages.pop();
      },3000);
    }
  }
});
can.Component.extend({
  tag: "person-edit",
  viewModel: {
    save: function(event){
      event.preventDefault();
      var self = this;
      // fake saving
      setTimeout(function(){
        can.trigger(self,"saved");
      },500);
    }
  }
})
$("#out").html( can.view("app",{}) );
Output

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

Dismiss x