-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Add a "replace" option to Backbone.Model.set #3253
Comments
Reset is an escape hatch, that allows you to easily do efficient rendering in bulk when you know that you need it. For your case, just use |
Can you set without making attribute changes dirty?
|
Calling |
I agree that this method is missing. I also thought about using A Model.prototype.reset = function(attrs, options) {
for (var key in this.attributes) {
if (key in attrs) continue;
attrs[key] = void 0;
}
return this.set(attrs, options);
}; |
What about: Model.prototype.reset = function(attrs, options) {
for (var key in this.attributes) {
this.unset(key, {silent:true});
}
return this.set(attrs, options);
}; |
No, this does not help. Imagine you a have a key Backbone.Model.prototype.reset = function(attrs, options) {
for (var key in this.attributes) {
this.unset(key, {silent:true});
}
return this.set(attrs, options);
};
var bar = new Backbone.Model();
bar.on('change', function(model) {
console.log('The model bar has been changed.');
});
bar.on('change:foo', function(model, foo) {
console.log('Foo has been changed to: ' + foo);
});
bar.set(foo, 'test');
// => The model bar has been changed.
// => Foo has been changed to: test
bar.reset({ foo2: 'test2' });
// => The model bar has been changed.
// Foo was resetted but no change event has been triggered. |
Cool, I get what you mean. I'd probably opt for using |
Pardon any ignorance I show, as I'm still relatively new to backbone.js, but the behavior being discussed sounds like
It looks as though Again, pardon any ignorance I am exhibiting in this comment. |
@kolorahl , What the OP wants to achieve is to clear the current model attributes and pass a new JSON which becomes the new attributes of the model. In this manner, we don't really want to hit the backend if already have the JSON. |
I somehow agree with @lupugabriel1 with his clear + set method. But I think this is one functionality that needs to be considered. Something like Backbone.Collection#reset |
- 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`.
The reason I needed this is because the world is changing. Backbone assumes Model#fetch() XHR is the primary method for loading data from the server, but we're doing a lot more stuff using websockets. When data gets pushed to the client, it's redundant to call .fetch and we need a decent way to side-load the data and still get an event hook to trigger. |
Why aren't you using |
@jridgewell because #set will make the attributes dirty. Let's try using set and see what happens:
Actual outcome:
Desired outcome:
Set is fine for when the state has changed out-of-sync with the server, but Model#reset is needed because there is no way to mark state as synchronize w/ the server. |
I ended up writing a different monkey patch for this feature:
This probably needs to unset missing items also. Not sure if Model#set(attributes) does that. |
As @lennerd pointed out, calling
|
I went ahead and created a little extension to add a working |
Agree - would be useful to have natively. I needed to just reset attributes without mangling the 'id' attribute, as clear() was doing. Here's the gist of it. |
@thesmart, Model#set does not unset missing attributes which is why I think Model#reset is necessary. It should be a requirement to give a reason when closing an issue. I'd like to know why @akre54 closed this. Backbone models are deliberately primitive and unopinionated, however, the lack of Model#reset expresses an opinion about how models are used. From http://backbonejs.org/#Getting-started
|
As far as I can tell, Backbone's data model is incompatible with REST On Tuesday, July 5, 2016, pgifford [email protected] wrote:
|
I needed a model It's better than a simple /**
* Clears the model's attributes and sets the default attributes.
* @param {Object} attributes to overwrite defaults
* @param {Object} options to pass with the "set" call.
* @return {Backbone.Model} this object, to chain function calls.
*/
reset: function(attributes, options) {
options = _.extend({ reset: true }, options);
// ensure default params
var defaults = _.result(this, 'defaults'),
attrs = _.defaults(_.extend({}, defaults, attributes || {}), defaults);
// apply
this._reset(attrs, options);
// triggers a custom event, namespaced to model in order
// to avoid collision with collection's native reset event
// when listening to a collection.
if (!options.silent) this.trigger('model:reset', this, options);
return this;
},
/**
* Private method to help wrap reset with a custom behavior in child
* classes.
* @param {Object} attributes to overwrite defaults
* @param {Object} options to pass with the "set" call.
*/
_reset: function(attrs, options) {
this.clear({ silent: true }).set(attrs, options);
}, It triggers change events ( |
See also #4101 |
i've tweaked the solution of lennerd #3253 (comment) a little bit.
|
I needed to update some of a Model's data that came in from the server. Currently, there are two options: call
Model.set
, or setModel.attributes
directly. I didn't want changes to be recorded, but I also couldn't usesilent
because I needed the respective views to update. So, I wrote a monkey patch:Wondered why
Backbone.Model
doesn't have a reset method likeBackbone.Collection
?The text was updated successfully, but these errors were encountered: