diff --git a/README.md b/README.md index bafd58c..214b778 100644 --- a/README.md +++ b/README.md @@ -538,16 +538,22 @@ Possible options (when using `state.set()`): * `silent` {Boolean} - prevents triggering of any change events as a result of the set operation. * `unset` {Boolean} - `unset` the attributes keyed in the attributes object instead of setting them. -### unset `state.unset(attribute, [options])` +### unset `state.unset(attribute|attributes[], [options])` -Clear the named attribute from the state object. Fires a `"change"` event and a `"change:attributeName"` event unless `silent` is passed as an option. +Clear the named attribute or an array of named attributes from the state object. Fires a `"change"` event and a `"change:attributeName"` event unless `silent` is passed as an option. If the attribute being unset is `required` and has a `default` value as defined in either `props` or `session`, it will be set to that value, otherwise it will be `undefined`. ```javascript +// unset a single attribute person.unset('firstName') ``` +```javascript +// unset multiple attributes +person.unset(['firstName', 'lastName']) +``` + ### clear `state.clear([options])` Clear all the attributes from the state object, by calling the `unset` function for each attribute, with the options provided. diff --git a/ampersand-state.js b/ampersand-state.js index 86ce52e..dd2fb37 100644 --- a/ampersand-state.js +++ b/ampersand-state.js @@ -318,16 +318,19 @@ assign(Base.prototype, BBEvents, { return this.serialize(); }, - unset: function (attr, options) { - var def = this._definition[attr]; - var type = def.type; - var val; - if (def.required) { - val = result(def, 'default'); - return this.set(attr, val, options); - } else { - return this.set(attr, val, assign({}, options, {unset: true})); - } + unset: function (attrs, options) { + attrs = Array.isArray(attrs) ? attrs : [attrs]; + forEach(attrs, function (key) { + var def = this._definition[key]; + var type = def.type; + var val; + if (def.required) { + val = result(def, 'default'); + return this.set(key, val, options); + } else { + return this.set(key, val, assign({}, options, {unset: true})); + } + }, this); }, clear: function (options) { diff --git a/test/basics.js b/test/basics.js index c1e3b00..1ed2355 100644 --- a/test/basics.js +++ b/test/basics.js @@ -500,6 +500,19 @@ test("multiple unsets", function (t) { t.end(); }); +test("unset with array", function (t) { + var Model = State.extend({ + props: { + a: ['string', true, 'first'], + b: ['string', true, 'second'] + } + }); + var model = new Model({a: 'a', b: 'b'}); + model.unset(['a', 'b']); + t.equal(model.a, 'first'); + t.equal(model.b, 'second'); + t.end(); +}); test("unset and changedAttributes", function (t) { var Model = State.extend({