Skip to content

Instantly share code, notes, and snippets.

@the5fire
Created May 24, 2013 07:03
Show Gist options
  • Save the5fire/5641772 to your computer and use it in GitHub Desktop.
Save the5fire/5641772 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>the5fire-backbone-model</title>
</head>
<body>
</body>
<script src="http://backbonejs.org/test/vendor/jquery.js"></script>
<script src="http://backbonejs.org/test/vendor/underscore.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/backbone.js/1.0.0/backbone-min.js"></script>
<script>
(function ($) {
Man = Backbone.Model.extend({
initialize: function(){
alert('Hey, you create me!');
//初始化时绑定监听
this.bind("change:name",function(){
var name = this.get("name");
alert("你改变了name属性为:" + name);
});
this.bind("error",function(model,error){
alert(error);
});
},
defaults: {
name:'张三',
age: '38'
},
validate:function(attributes, options){
alert(attributes);
if(attributes.name == '') {
return "name不能为空!";
}
},
aboutMe: function(){
return '我叫' + this.get('name') + ',今年' + this.get('age') + '岁';
}
});
var man = new Man;
man.set({name:''}); //根据验证规则,弹出错误提示。
})(jQuery);
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment