Last active
December 16, 2015 14:11
-
-
Save ddoronin/0725bcd2e48a8355c037 to your computer and use it in GitHub Desktop.
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
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
</head> | |
<body> | |
<div id="log"></div> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script> | |
<script src="http://underscorejs.org/underscore.js"></script> | |
<script src="http://backbonejs.org/backbone.js"></script> | |
<script> | |
function log(text){ | |
//$('#log').append($('<p>').text(text)); | |
} | |
var User = Backbone.Model.extend({ | |
defaults: { | |
firstName: '', | |
secondName: '', | |
fullName: '' | |
}, | |
initialize: function(){ | |
var self = this; | |
function getFullname(){ | |
var firstName = self.get('firstName'); | |
var secondName = self.get('secondName'); | |
return [firstName, secondName].join(' '); | |
} | |
function updateFullname(){ | |
self.set('fullName', getFullname(), {silent: true}); | |
} | |
updateFullname(); | |
this.on('change', function(){ | |
console.log('change'); | |
updateFullname(); | |
}); | |
}, | |
destroy: function(){ | |
console.log('destroy'); | |
this.off(); | |
} | |
}); | |
setInterval(function(){ | |
_.forEach(new Array(1000), function(){ | |
var user = new User({ | |
firstName: 'Ivan', | |
secondName: 'Ivanov' | |
}); | |
log(user.get('fullName')); | |
user.set('secondName', 'Sidorov'); | |
log(user.get('fullName')); | |
// what if I don't need a user object any more? would it be collected by GC? | |
user.destroy(); | |
}); | |
}, 1000); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment