Skip to content

Instantly share code, notes, and snippets.

@schovi
Created March 9, 2013 21:38
Show Gist options
  • Save schovi/5125858 to your computer and use it in GitHub Desktop.
Save schovi/5125858 to your computer and use it in GitHub Desktop.
Simple example of using can.compute with template
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