Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clear should set attributes to defaults #3395

Closed

Conversation

samjoch
Copy link

@samjoch samjoch commented Nov 25, 2014

When i defined defaults attributes, on clear those values are not retrieved.

@samjoch samjoch force-pushed the clear-should-set-to-defaults branch 2 times, most recently from a62f03a to 2a17d0c Compare November 25, 2014 02:09
@akre54
Copy link
Collaborator

akre54 commented Nov 26, 2014

I think most often when you clear the model you want it cleared. You can always reset the initial state after.

var attrs = {};
for (var key in this.attributes) attrs[key] = void 0;
return this.set(attrs, _.extend({}, options, {unset: true}));
var attrs = _.defaults({}, attrs, _.result(this, 'defaults'));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is attrs coming from? typo?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh thanks!

@lukeasrodgers
Copy link
Contributor

Agreed with @akre54 on the semantics of clear. If this behaviour is desired, I think it should be a new method, with a different name.

@samjoch
Copy link
Author

samjoch commented Nov 26, 2014

Are we talking about a reset() method in Model ? because i also agreed with @akre54.
I can rename this one and propose something, what do you think ?

@platinumazure
Copy link
Contributor

This functionality can be achieved pretty easily like so:

model.clear().set(_.result(model, "defaults"));

To avoid a double-flash in your view, you could add a custom option to the clear call and have the view callbacks not perform the update if it's happening during this "clear-then-defaults" call.

@akre54
Copy link
Collaborator

akre54 commented Feb 20, 2015

I think simpler wins out here. Thanks anyways.

@akre54 akre54 closed this Feb 20, 2015
jridgewell added a commit to jridgewell/backbone that referenced this pull request May 24, 2015
- Adds `{ reset: true }` flag to `#set` that deletes any attributes
that are not specified
- Adds ‘reset’ event mirroring `Collection`’s.

Fixes jashkenas#3253, jashkenas#3395.

I could really use help naming the `validateCombined` option. I need to
validate only the passed in `attrs` (both `#clear` and `#reset` specify
_exactly_ what the attributes will be), not the merged `attrs`
`attributes`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants