From 3695311ddbb1c4508c8dcab53ab01fdb2836de83 Mon Sep 17 00:00:00 2001 From: Andrew Hacking Date: Fri, 23 May 2014 14:42:56 +1000 Subject: [PATCH] Add attribute level save/update control --- packages/ember-model/lib/model.js | 13 ++++- packages/ember-model/tests/model_test.js | 61 ++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/packages/ember-model/lib/model.js b/packages/ember-model/lib/model.js index 6276b6f..c22ac75 100644 --- a/packages/ember-model/lib/model.js +++ b/packages/ember-model/lib/model.js @@ -176,10 +176,16 @@ Ember.Model = Ember.Object.extend(Ember.Evented, { attributes = this.constructor.getAttributes(), relationships = this.constructor.getRelationships(), properties = attributes ? this.getProperties(attributes) : {}, - rootKey = get(this.constructor, 'rootKey'); + rootKey = get(this.constructor, 'rootKey'), + isNew = get(this, 'isNew'); for (key in properties) { meta = this.constructor.metaForProperty(key); + if (meta.options) { + if (meta.options.save === false || (meta.options.update === false && ! isNew)) { + continue; + } + } if (meta.type && meta.type.serialize) { json[this.dataKey(key)] = meta.type.serialize(properties[key]); } else if (meta.type && Ember.Model.dataTypes[meta.type]) { @@ -195,6 +201,11 @@ Ember.Model = Ember.Object.extend(Ember.Evented, { for(var i = 0; i < relationships.length; i++) { key = relationships[i]; meta = this.constructor.metaForProperty(key); + if (meta.options) { + if (meta.options.save === false || (meta.options.update === false && ! isNew)) { + continue; + } + } relationshipKey = meta.options.key || key; if (meta.kind === 'belongsTo') { diff --git a/packages/ember-model/tests/model_test.js b/packages/ember-model/tests/model_test.js index d971e53..b9aabc3 100644 --- a/packages/ember-model/tests/model_test.js +++ b/packages/ember-model/tests/model_test.js @@ -493,6 +493,67 @@ test("Model#create() works as expected", function() { stop(); }); +test("attribute 'save' option works as expected", function() { + expect(6); + + var SaveableModel = Model.extend({ + dontSave: Ember.attr('string', {save: false}), + canSave: Ember.attr('string', {save: true}) + }); + + var record = SaveableModel.create({name: 'Yehuda', dontSave: 'BAD', canSave: 'OK'}), + json; + + SaveableModel.adapter = Ember.FixtureAdapter.create(); + SaveableModel.FIXTURES = []; + json = record.toJSON(); + ok(! json.dontSave, "The JSON omits non-saveable attributes on create"); + equal(json.canSave, 'OK', "The JSON includes explicitly savable attributes on create"); + equal(json.name, 'Yehuda', "The JSON includes implicitly savable attributes on create"); + record.save().then(function() { + start(); + record.set('dontSave', 'Bar'); + record.set('canSave', 'OK'); + record.save().then(function() { + start(); + json = record.toJSON(); + ok(! json.dontSave, "The JSON omits non-saveable attributes on update"); + equal(json.canSave, 'OK', "The JSON includes explicitly saveable attributes on update"); + equal(json.name, 'Yehuda', "The JSON includes implicitly saveable attributes on update"); + }); + stop(); + }); + stop(); +}); + +test("attribute 'update' option works as expected", function() { + expect(6); + + var UpdateableModel = Model.extend({ + dontUpdate: Ember.attr('string', {update: false}), + canUpdate: Ember.attr('string', {save: true}) + }); + + UpdateableModel.adapter = Ember.FixtureAdapter.create(); + UpdateableModel.FIXTURES = []; + + var record = UpdateableModel.create({name: 'Yehuda', dontUpdate: 'CREATE', canUpdate: 'OK'}), + json; + + json = record.toJSON(); + equal(json.dontUpdate, 'CREATE', "The JSON includes non-updateable attributes on create."); + equal(json.canUpdate, 'OK', "The JSON includes explicitly updateable attributes on create"); + equal(json.name, 'Yehuda', "The JSON includes implicitly updateable attributes on create"); + record.save().then(function(record2) { + start(); + json = record.toJSON(); + ok(! json.dontUpdate, "The JSON omits non-updateable attributes on update."); + equal(json.canUpdate, 'OK', "The JSON includes explicitly updateable attributes on update"); + equal(json.name, 'Yehuda', "The JSON includes implicitly updateable attributes on update"); + }); + stop(); +}); + test(".getAttributes() returns the model's attributes", function() { var attr = Ember.attr, BaseModel = Ember.Model.extend({