Skip to content

Commit

Permalink
Merge pull request #152 from pgilad/patch-3
Browse files Browse the repository at this point in the history
Add ability to unset an array. Closes #45
  • Loading branch information
Michael Garvin committed Mar 31, 2015
2 parents 1b22c3a + 43c506f commit b24d776
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 13 additions & 10 deletions ampersand-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions test/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit b24d776

Please sign in to comment.