Created
March 9, 2013 21:38
-
-
Save schovi/5125858 to your computer and use it in GitHub Desktop.
Simple example of using can.compute with template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Person = can.Observe({ | |
fullName: can.compute(function(fullName) { | |
if(typeof fullName === "undefined") { | |
return this.attr('firstName') + " " + this.attr("lastName") | |
} else { | |
var parts = fullName.split(/\s+/) | |
this.attr('lastName', parts.pop()) | |
this.attr('firstName', parts.pop()) | |
} | |
}) | |
}) | |
person = new Person({ | |
firstName: "John", | |
lastName: "Doe" | |
}) | |
person.fullName() // -> John Doe | |
var tmpl = can.view.ejs("<span><%= this.person.fullName() %></span>") | |
$(document.body).html(tmpl({person: person})) // You will see "John Doe" | |
person.attr('firstName', "David") // Now you see "David Doe" | |
person.fullName("Marry Poppins") // Now you see "Marry Poppins" :) | |
person.attr('lastName') // -> Poppins |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment