Skip to content

Instantly share code, notes, and snippets.

@benpickles
Created April 2, 2010 20:35
Show Gist options
  • Save benpickles/353675 to your computer and use it in GitHub Desktop.
Save benpickles/353675 to your computer and use it in GitHub Desktop.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<script src="http://github.com/documentcloud/underscore/raw/master/underscore-min.js"></script>
<script src="http://github.com/benpickles/js-model/raw/master/dist/js-model-0.8.1.min.js"></script>
<script>
var Post = Model('post', {}, {
validate: function() {
if (this.attr("title") != "Bar") {
this.errors.add("title", "should be Bar")
}
}
})
var post = new Post()
post.attr("title", "Foo")
console.log('expects false: ', post.valid())
console.log('expects 1: ', post.errors.size())
console.log('expects ["should be Bar"]: ', post.errors.on("title"))
console.log('expects { title: ["should be Bar"] }: ', post.errors.all())
post.attr("title", "Bar")
console.log('expects true: ', post.valid())
console.log('expects 0: ', post.errors.size())
console.log('expects []: ', post.errors.on("title"))
console.log('expects {}: ', post.errors.all())
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment