-
Notifications
You must be signed in to change notification settings - Fork 568
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
Previous Attributes after save() call #326
Comments
Hmm, I don't know, I guess it depends on how you're trying to use "previous"... presumably you could do this: var previous;
var model = new Models.Site({id: 1});
return model.fetch().then(function() {
previous = this.get('name')
return model.save({
name: 'updatedknexjs.org'
});
})
.then(function (u) {
equal(previous, 'knexjs.org');
equal(model.get('name'), 'updatedknexjs.org');
model.set('name', 'knexjs.org');
return model.save();
}); or this: var previousAttrs;
var model = new Models.Site({id: 1});
return model.fetch().then(function() {
model.set(someValues)
previousAttrs = this.previousAttributes();
return model.save();
})
.then(function (u) {
// compare previousAttrs here
equal(model.get('name'), 'updatedknexjs.org');
return model.save();
}); would either of these work? |
The use case is for TryGhost/Ghost#1563, we need to try and detect when a Post's status is changed from I'm thinking of listening to the I'll try to report back if that works. |
In the |
I ended up adding the following to our base models and it seems to provide the functionality I'm looking for: saving: function (newObj, attr, options) {
// Store the previous attributes so we can tell what was updated later
this._updatedAttributes = newObj.previousAttributes();
},
// Get attributes that have been updated (values before a .save() call)
updatedAttributes: function () {
return this._updatedAttributes || {};
},
// Get a specific updated attribute value
updated: function (attr) {
return this.updatedAttributes()[attr];
} I couldn't get the options.previousAttributes to work for me, but this approach seems to be working. |
Cool. |
This is a bug right? I'm having the same problem, I do:
And I get "updated", is not supposed to return "jdoe"? |
Not working , i have tried using all the hooks , saving ,saved , updating , updated and previousAttributes are blank in every case |
Do you have a test case so we can help you? |
Yes UserModel bookshelf.model("Users",{
initialize: function(){
this.on("creating", this._beforeCreate, this);
this.on("created", this._afterCreate, this);
this.on("updating", this._beforeUpdate, this);
this.on("updated", this._afterUpdate, this);
},
_beforeUpdate: function(model,attrs,options){
console.log('model>>>>>',model);
console.log('options>>>>>',options);
},
_afterUpdate: function(model,response,options){
console.log('after:model>>>>>',model);
console.log('after:options>>>>>',options);
}
}); Now i will paste the console output from my unit tests Old email in my database was Update Query
Here |
I confirmed a while back that previousAttributes has a bug. I don't know if that claim is still valid. I would recommend you to workaround the problem by saving your attributes before your actions and make use of them later as it they were extracted from previous attributes.
|
+1 After model is saved I'm expecting that previous attributes would be exactly previous attributes, but I got the same data that I set in update request |
Can this be opened as a bug? I'm experiencing this in 0.8.2. |
@aef- There's a lot going on here, could you open a new issue showing exact use case with expected and actual ouput. |
I'm trying to get the previous attributes after calling save() and having a hard time. Something like this is what I'd expect:
Is there some other mechanism I can check after a save() call to get what attributes changed?
The text was updated successfully, but these errors were encountered: