-
Notifications
You must be signed in to change notification settings - Fork 11
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
Proposal for an improved structure of the API for version 1.1.x #5
Comments
So with this proposed approach, would |
The idea is that it is called with an object of which ID's have changed along with the value. This way it shouldn't matter if you are dealing with a single or multiple instances. As both the ID's and values are available you can always loop through all the ID's separately, or update them all in 1 go. |
Cool, that makes sense. |
I have implemented this in the One of the things that I'm currently looking at is how to properly test the ChangeSet that is passed to the callbacks. it('should execute the callback after updating watched properties on multiple models', function(done) {
var self = this;
var spyStatusParams = { ids: {}, values: {} };
spyStatusParams.ids[this.joe.id] = 'pending';
spyStatusParams.ids[this.bilbo.id] = 'pending';
spyStatusParams.values['pending'] = [this.joe.id.toString(), this.bilbo.id.toString()];
this.Person.updateAll(null, {status: 'pending', name: 'pending'})
.then(function(res) {
expect(self.spyAge).not.to.have.been.called;
expect(self.spyNickname).not.to.have.been.called;
expect(self.spyStatus).to.have.been.called;
expect(self.spyStatus).to.have.been.calledWith(spyStatusParams);
done();
})
.catch(done);
}); This test fails with a message like this as it does not invoke the callback with a
|
I gave this some thought and came up with a new structure that seems to provide easier access to the things we want.
In the definition of the mixin each property is followed by the name of the method that is triggered on change:
When the mixin executes
before save
it will monitor the properties specified above and will put them all in their own object which gives us the following structure:When the mixin executes
after save
it will execute the callback function specified in the definition with thechangedProperties
for that property.The
changed
parameter that is passed to the callback functions need to have a few helper methods to easily access the data:The callbacks that are specified in the example above could be implemented like this:
One of the advantages of this approach is that it is easy to handle changes on a per-property basis without having to add new logic for it. If you want to handle all properties using the same handler (as it is implemented in version 1.0.x) you can easily do that before.
In a future version it would be nice the possibility of adding the logic of this mixin programatically:
And even watch a specific property value:
The text was updated successfully, but these errors were encountered: