Eungene's Imaginations...
[Backbone.js] Model 기초 예제 소스 본문
<script> var Book = Backbone.Model.extend({ initialize: function () {
this.on("change", function () { // 로그를 남기는 여러가지 방법 console.log('Model Changed'); if (this.hasChanged('name')) { console.log('The name has changed'); } if (this.hasChanged('author')) { console.log('The author has changed'); } console.log('Changed attributes: ' + JSON.stringify(this.changed)); });
}, defaults: { name: 'Book Title', author: 'No One' } }); var thisBook = new Book({name : 'Beginning Backbone', author: 'Chris Jerico'});
console.log('선언부 : ' + JSON.stringify(thisBook));
// name attribute의 내용을 바꿈(재정의) thisBook.set('name', '바꼈지롱~'); // year attribute가 없을 경우에는 새로 추가, // 기존에 year attribute가 있을 경우에는 재정의 thisBook.set('year', 2030); console.log('year has is ' + thisBook.has('year')); console.log(thisBook.attributes); // year attribute를 삭제. thisBook.unset('year'); console.log(thisBook.attributes); // 해당 attribute가 존재하는 경우에는 true를 반환하고, // 없으면 false를 반환. var hasYear = thisBook.has('year'); var hasName = thisBook.has('name'); console.log('year has is ' + hasYear); console.log('name has is ' + hasName);// 모델 객체 복사 var cloneBook = thisBook.clone(); console.log(cloneBook.attributes);</script>
설명은 따로 추가하지 않겠습니다.
주석 참고 하세요~