Eungene's Imaginations...
[Backbone.js] Model attribute validate(모델 속성 조건부 정의) 본문
Programming/Backbone.js
[Backbone.js] Model attribute validate(모델 속성 조건부 정의)
Eungene's 2015. 10. 5. 10:29728x90
반응형
<script> var Book = Backbone.Model.extend({ initialize: function () { this.on("invalid", function (model, error) { console.log("**Validation Error : " + error + "**"); }); this.on("change", function (a) { // 로그를 남기는 여러가지 방법 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', year: 2000 }, // 개발자가 임의로 만든 함수 printDetails: function () { console.log('printDetails 함수 진입'); }, // attribute에 조건을 걸어둠. validate: function (attrs) { if (attrs.year < 2000) { // year attribute가 2000이하면 실행 return 'Year must be after 2000'; } if (!attrs.name) { // name attribute를 지울 경우 실행 return 'A name must be provided'; } } }); var thisBook = new Book({name : 'Beginning Backbone', author: 'Chris Jerico'}); console.log('선언부 : ' + JSON.stringify(thisBook)); // check if model is valid console.log('Is model valid: ' + thisBook.isValid()); // break the validation rules by not using the validate flag thisBook.set('year', 1998); //check if the model is valid console.log('Is model valid: ' + thisBook.isValid()); // 이 절에서 //thisBook.set('year', 1999, {validate: true}); //console.log('Check year change : ' + thisBook.get('year')); //thisBook.unset('name', {validate: true}); //console.log('Check if name was removed ' + thisBook.get('name')); //console.log(thisBook.attributes); console.log('마지막 단'); </script>
반응형
Comments