var Note = new Model('Note', function () { this.attr('id!', 'number'); this.attr('title', 'string', 'nonempty'); this.attr('text', 'string'); }); var note = new Note({ id: 123, title: "Hello World" }); note.data.id note.data.title note.data.text note.data.id = note.data.title = note.data.text = note.data = {…} note.get(attrName[, attrName, …]) return an object with the values ​​of the requested attributes.note.get() return a copy of all the data.note.data() is the same as note.get() .note.set(attrName, value) and note.set({…}) do not note.set({…}) change event.note.hasChanged tells you if the entity data has changed since it was last saved.note.isNew says whether entity data has been saved at all at least once.note.isPersisted says whether recent changes have been saved.note.bind(eventName, handler) "hangs" the handler. By the way, you can hang the handler of any event not only on a separate entity, but also on all entities of a class ( Note.bind ).note.isValid says whether the current model data is valid.note.errors , returns data errors, if any.note.revert() rolls back unsaved changes and “shoots” the revert event.note._persist() , which also “shoots” the persist event.note.save() method must save data with the help of agax. Note.prototype.save = function () { var note = this; return $.ajax({ type: 'PUT', url: '/notes/'+note.data.id, data: note.data(), dataType: 'json' }).done(function (json) { note._persist(); }); } note.bind('persist', function () { $('h1', 'div#note'+this.data.id).html(this.data.title); }); note.data = { id: 123, title: "abc", text: "" } if (note.hasChanged && note.isValid) { // Django-style note.save(); } this.attr('title', 'string', 'nonempty') First we specify the name of the attribute, then its validators. Validators, when the time comes, in the declared order will check the value of the attribute. function validateMinLength(value, minLength) { if (value.length < minLength) return 'tooshort'; } Model.registerValidator('array', function (value) { if (Object.prototype.toString.call(v) === '[object Array]') return 'wrongtype'; }); Model.registerValidator('minLength', validateMinLength); number , string , boolean , nonnull , nonempty and in . They, just like in our example, can be connected to attributes by name. this.attr('title', 'string', 'nonempty', [ 'minLength', 6 ]); this.attr('title', 'string', 'nonempty', [ 'minLength', 6 ], function (title) { if (title[0] !== title[0].toUpperCase()) return 'downcase'; }); note.isValid can say true or false , and note.errors can return an object with errors, if any. note.data = { id: 'abc', title: '', text: 3 }; // note.isValid // false note.errors // { id: 'wrongtype', title: 'empty', text: 'wrongtype' } initialize , change , persist and revert . As mentioned above, you can hang the handler on a specific entity ( note.bind ), but you can hang on a class ( Note.bind ), so that the handler will be common to all entities.initialize “fired” once when an entity is created: var note = new Note({…}) . So hanging the initialize handler only makes sense to the class.change triggered every time an attribute value changes. There are, however, setters who do not pull change (this was discussed above).perist - when the entity receives a signal that the changes were successfully saved.revert - when the application logic ordered to undo the unsaved changes (using the note.revert() method). var ALLOWED_LANGUAGES = ['en', 'ua', 'ru']; var Note = new Model('Note', function () { this.attr('id!', 'number'); this.attr('title', 'string', 'nonempty'); this.attr('lang', 'string', 'nonempty', [ 'in', ALLOWED_LANGUAGES]); this.attr('text', 'string'); }); Note.bind('initialize', function () { if (!this.data.lang) { this.set('lang', 'en'); // change — ! } }); note.bind('change', function (changes) { if (changes.title) $('h1', 'div#note'+this.data.id).html(changes.title); }); Source: https://habr.com/ru/post/178811/
All Articles