Skip to content

Instantly share code, notes, and snippets.

@andyyou
Created May 10, 2013 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyyou/5552972 to your computer and use it in GitHub Desktop.
Save andyyou/5552972 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<title>Hello Backbone Model</title>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
<script>
(function($){
Man = Backbone.Model.extend({
initialize: function()
{
console.log("Hey, you create man.");
},
defaults:
{
name: 'Andy',
age: '27'
}
});
var man = new Man();
console.log(man.get('age'));
Woman = Backbone.Model.extend({
initialize: function(){
console.log('Hi, you create a woman');
}
});
var woman = new Woman();
woman.set({name: 'Rabby', age: '27'});
console.log(woman.get('name'));
Child = Backbone.Model.extend({
initialize: function()
{
console.log('Hey, you bron a child');
},
defaults:
{
name: 'Ken',
age: '1'
},
about: function()
{
return 'Wow, I am ' + this.get('name') + ',and I am ' + this.get('age') + ' years old';
}
});
var child = new Child();
console.log(child.about());
Person = Backbone.Model.extend({
initialize: function()
{
console.log('WOW, you create a person');
this.bind('change:name', function(){
var name = this.get('name');
console.log('Change event trigger and Name: ' + name);
});
},
defaults:
{
name: 'Cruz',
age: '18'
}
});
var person = new Person();
console.log(person.get('name'));
person.set({name: 'Cool'});
Dog = Backbone.Model.extend({
initialize: function()
{
console.log('Dog bron.');
this.on('change:name', function(){
var name = this.get('name');
console.log('Dog change name: ' + name);
});
this.on('error', function(model, error)
{
console.log(error);
});
},
defaults:
{
name: 'White',
age: '10'
},
validate: function(attr)
{
if(attr.name == '')
{
return "Empty error";
}
}
});
var dog = new Dog();
dog.save({name:''});
})(jQuery);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment