Lines Matching refs:model

1 YUI.add('model-test', function (Y) {
20 'destroy() should destroy the model instance': function () {
21 var model = new Y.Model();
23 model.sync = function () {
24 Assert.fail('sync should not be called unless the model is being deleted');
27 Assert.isFalse(model.get('destroyed'));
28 Assert.areSame(model, model.destroy(), 'destroy() should be chainable');
29 Assert.isTrue(model.get('destroyed'));
34 model = new Y.Model();
41 model.destroy(mock.callback);
47 model = new Y.Model();
54 model.destroy({}, mock.callback);
58 'destroy() should delete the model if the `remove` option is truthy': function () {
61 model = new Y.Model();
68 model.sync = function (action, options, callback) {
79 model.destroy({remove: true}, mock.callback);
83 'destroy() should remove the model from all lists': function () {
84 var model = new Y.Model(),
89 listOne.add(model);
90 listTwo.add(model);
91 listThree.add(model);
93 Assert.areSame(1, listOne.size(), 'model should be added to list one');
94 Assert.areSame(1, listTwo.size(), 'model should be added to list two');
95 Assert.areSame(1, listThree.size(), 'model should be added to list three');
97 model.destroy();
99 Assert.areSame(0, listOne.size(), 'model should be removed from list one');
100 Assert.areSame(0, listTwo.size(), 'model should be removed from list two');
101 Assert.areSame(0, listThree.size(), 'model should be removed from list three');
125 var model = new this.TestModel({foo: 'foo'});
126 Assert.areSame('foo', model.get('foo'));
132 model = new Y.Model({
141 Assert.areSame('foo', model.get('foo'), 'ad-hoc foo attribute should be set');
142 Assert.areSame('bar', model.get('bar.a'), 'ad-hoc bar attribute should be set');
143 Assert.areSame('baz', model.get('baz')[0], 'ad-hoc baz attribute should be set');
144 Assert.isNull(model.get('quux'), 'ad-hoc quux attribute should be set');
145 Assert.areSame(0, model.get('zero'), 'ad-hoc zero attribute should be set');
146 Assert.areSame(created, model.get('created'), 'ad-hoc created attribute should be set');
148 ObjectAssert.ownsKeys(['foo', 'bar', 'baz', 'quux', 'zero', 'created'], model.getAttrs(), 'ad-hoc attributes should be returned by getAttrs()');
152 var model;
158 model = new this.TestModel({customId: 'foo'});
159 Assert.areSame('foo', model.get('customId'));
160 Assert.areSame('foo', model.get('id'));
162 model = new this.TestModel({customId: 'foo'});
163 Assert.areSame('foo', model.get('id'));
164 Assert.areSame('foo', model.get('customId'));
166 model = new this.TestModel({id: 'foo'});
167 Assert.areSame('foo', model.get('customId'));
168 Assert.areSame('foo', model.get('id'));
170 model = new this.TestModel({id: 'foo'});
171 Assert.areSame('foo', model.get('id'));
172 Assert.areSame('foo', model.get('customId'));
176 var model = new this.TestModel();
177 Assert.isTrue(model._isYUIModel);
182 model = new this.TestModel();
184 model.on('change', function (e) {
191 model.set('id', 'foo');
197 var model = new this.TestModel();
199 Assert.isObject(model.changed);
200 ObjectAssert.ownsNoKeys(model.changed);
202 model.set('foo', 'foo');
203 Assert.areSame('foo', model.changed.foo);
205 model.setAttrs({foo: 'bar', bar: 'baz'});
206 ObjectAssert.areEqual({foo: 'bar', bar: 'baz'}, model.changed);
208 model.save();
209 ObjectAssert.ownsNoKeys(model.changed);
211 model.set('foo', 'foo');
212 model.load();
213 ObjectAssert.ownsNoKeys(model.changed);
217 var model = new Y.Model();
219 Assert.isString(model.get('clientId'));
220 Assert.isTrue(!!model.get('clientId'));
224 var model = new this.TestModel();
226 Assert.isObject(model.lastChange);
227 ObjectAssert.ownsNoKeys(model.lastChange);
229 model.set('foo', 'foo');
230 Assert.areSame(1, Y.Object.size(model.lastChange));
231 ObjectAssert.ownsKeys(['newVal', 'prevVal', 'src'], model.lastChange.foo);
232 Assert.areSame('', model.lastChange.foo.prevVal);
233 Assert.areSame('foo', model.lastChange.foo.newVal);
234 Assert.isNull(model.lastChange.foo.src);
236 model.set('bar', 'bar', {src: 'test'});
237 Assert.areSame(1, Y.Object.size(model.lastChange));
238 Assert.areSame('test', model.lastChange.bar.src);
240 model.set('foo', 'bar', {silent: true});
241 Assert.areSame(1, Y.Object.size(model.lastChange));
242 Assert.areSame('bar', model.lastChange.foo.newVal);
245 '`lists` property should be an array of ModelList instances that contain this model': function () {
247 model = new this.TestModel(),
250 new Y.ModelList({model: this.TestModel}),
251 new Y.ModelList({model: this.TestModel})
254 Assert.isArray(model.lists);
263 lists[0].add(model);
264 lists[1].add(model);
266 ArrayAssert.itemsAreSame(lists, model.lists);
268 model.set('foo', 'foo');
292 var model = new this.TestModel(),
293 firstId = model.generateClientId(),
294 secondId = model.generateClientId();
303 model = new this.TestModel({foo: value});
305 Assert.areSame(Y.Escape.html(value), model.getAsHTML('foo'));
310 model = new this.TestModel({foo: value});
312 Assert.areSame(encodeURIComponent(value), model.getAsURL('foo'));
315 'isModified() should return true if the model is new': function () {
316 var model = new this.TestModel();
317 Assert.isTrue(model.isModified());
319 model = new this.TestModel({id: 'foo'});
320 Assert.isFalse(model.isModified());
323 'isModified() should return true if the model has changed since it was last saved': function () {
324 var model = new this.TestModel({id: 'foo'});
325 Assert.isFalse(model.isModified());
327 model.set('foo', 'bar');
328 Assert.isTrue(model.isModified());
330 model.save();
331 Assert.isFalse(model.isModified());
334 'isNew() should return true if the model is new': function () {
335 var model = new this.TestModel();
336 Assert.isTrue(model.isNew());
338 model = new this.TestModel({id: 'foo'});
339 Assert.isFalse(model.isNew());
341 model = new this.TestModel({id: 0});
342 Assert.isFalse(model.isNew());
347 model = new this.TestModel(),
350 model.sync = function (action, options, callback) {
360 model.load(opts);
365 var model = new this.TestModel();
367 model.set('foo', 'bar');
368 Assert.areSame(1, Y.Object.size(model.changed));
370 model.load();
371 Assert.areSame(0, Y.Object.size(model.changed));
376 model = new this.TestModel();
378 Assert.areSame(model, model.load());
379 Assert.areSame(model, model.load({}));
381 Assert.areSame(model, model.load(function (err) {
386 Assert.areSame(model, model.load({}, function () {
394 var model = new this.TestModel(),
395 response = model.parse('{"foo": "bar"}');
402 var model = new this.TestModel(),
406 Assert.areSame(array, model.parse(array));
407 Assert.areSame(object, model.parse(object));
412 model = new this.TestModel(),
415 model.sync = function (action, options, callback) {
422 // Give the model an id so it will no longer be new.
426 model.save(opts);
428 Assert.areSame('foo', model.get('id'), "model id should be updated after save");
430 model.sync = function (action) {
435 model.save();
441 var model = new this.TestModel();
443 model.set('foo', 'bar');
444 Assert.areSame(1, Y.Object.size(model.changed));
446 model.save();
447 Assert.areSame(0, Y.Object.size(model.changed));
452 model = new this.TestModel();
454 Assert.areSame(model, model.save());
455 Assert.areSame(model, model.save({}));
457 Assert.areSame(model, model.save(function (err) {
462 Assert.areSame(model, model.save({}, function () {
470 var model = new this.TestModel();
472 Assert.areSame('', model.get('foo'));
473 Assert.areSame(model, model.set('foo', 'bar'), 'set() should be chainable');
474 Assert.areSame('bar', model.get('foo'));
478 var model = new this.TestModel();
480 Assert.areSame('', model.get('foo'));
481 Assert.areSame('', model.get('bar'));
482 Assert.areSame(model, model.setAttrs({foo: 'foo', bar: 'bar'}), 'setAttrs() should be chainable');
483 Assert.areSame('foo', model.get('foo'));
484 Assert.areSame('bar', model.get('bar'));
489 model = new this.TestModel();
491 model.sync(function (err) {
499 "toJSON() should return a copy of the model's attributes, minus excluded ones": function () {
501 model = new this.TestModel(attrs),
504 json = model.toJSON();
522 model = new CustomTestModel(attrs);
523 json = model.toJSON();
530 'undo() should revert the previous change to the model': function () {
532 model = new this.TestModel(attrs);
534 ObjectAssert.areEqual(attrs, model.toJSON());
536 model.setAttrs({foo: 'moo', bar: 'quux'});
537 ObjectAssert.areEqual({id: 'id', foo: 'moo', bar: 'quux'}, model.toJSON());
539 Assert.areSame(model, model.undo(), 'undo() should be chainable');
540 ObjectAssert.areEqual(attrs, model.toJSON());
544 var model = new this.TestModel({id: 'id', foo: 'foo', bar: 'bar'});
546 model.setAttrs({foo: 'moo', bar: 'quux'});
548 model.undo(['foo']);
549 ObjectAssert.areEqual({id: 'id', foo: 'foo', bar: 'quux'}, model.toJSON());
554 model = new this.TestModel({id: 'id', foo: 'foo', bar: 'bar'});
556 model.setAttrs({foo: 'moo', bar: 'quux'});
558 model.on('change', function (e) {
563 model.undo(null, {src: 'test'});
568 var model = new this.TestModel();
570 model.on('change', function () {
574 model.undo();
579 model = new this.TestModel();
581 model.validate = function (attrs, callback) {
583 Y.ObjectAssert.areEqual(model.toJSON(), attrs);
587 model.set('foo', 'bar');
588 model.set('foo', 'baz');
589 model.save();
597 model = new this.TestModel(),
600 model.validate = function (attrs, callback) {
605 model.sync = function () {
609 model.on('error', function (e) {
615 model.save(function (err, res) {
629 model = new this.TestModel();
631 model.on('error', function (e) {
636 model.on('save', function (e) {
640 model.validate = function (attrs) {
646 model.set('foo', 'bar');
647 model.save();
651 model.set('foo', 'baz');
652 model.save();
678 model = new this.TestModel();
680 model.on('change', function (e) {
695 model.setAttrs({
704 var model = new this.TestModel();
706 model.on('change', function (e) {
710 model.set('foo', 'bar', {silent: true});
711 model.setAttrs({bar: 'baz'}, {silent: true});
716 model = new this.TestModel();
718 model.on('change', function (e) {
725 model.setAttrs({
730 model.set('foo', 'bar', {
740 model = new this.TestModel();
742 model.validate = function (hash, callback) {
746 model.on('error', function (e) {
755 model.set('foo', 'bar');
756 model.save();
763 model = new this.TestModel();
765 model.on('error', function (e) {
773 model.parse('moo');
780 model = new this.TestModel();
782 model.on('error', function (e) {
791 model.sync = function (action, options, callback) {
795 model.load();
802 model = new this.TestModel();
804 model.on('error', function (e) {
813 model.sync = function (action, options, callback) {
817 model.save();
824 model = new this.TestModel();
826 model.on('load', function (e) {
833 Assert.areSame('bar', model.get('foo'), 'load event should fire after attribute changes are applied');
836 model.sync = function (action, options, callback) {
840 model.load(function () {
849 model = new this.TestModel();
851 model.on('save', function (e) {
858 Assert.areSame('bar', model.get('foo'), 'save event should fire after attribute changes are applied');
861 model.sync = function (action, options, callback) {
865 model.save(function () {
876 requires: ['model', 'model-list', 'test']