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

Delegated change events are called too early and too many times #8

Open
snurra opened this issue Jun 26, 2015 · 0 comments
Open

Delegated change events are called too early and too many times #8

snurra opened this issue Jun 26, 2015 · 0 comments

Comments

@snurra
Copy link
Contributor

snurra commented Jun 26, 2015

Normal change events are triggered after all changes needed from a call to Backbone.Model.set has been made. However, delegated change events are triggered after each change made to the related attribute.

Example:

var Company = Backbone.RelationalModel.extend({
  relations: {
    employees: Backbone.Collection.extend({})
  }
});
var aCompany = new Company();
// Set 1
aCompany.set("name", "Kinship ltd");
// Set 2
aCompany.set("employees", [{ name: "Martin" }, { name: "Gustaf" }]);
// Set 3
aCompany.set({ name: "Kinship.com", employees: [{ name: "Henric"}, { name: "Mateusz" }]});

With current functionality:
Set 1 triggers 1 change:name event (after all attributes have been changed).
Set 2 triggers 2 change:employees events (one after each added employee).
Set 3 triggers 4 change:employees events (one after each removed and added employee) and 1 change:name event (after all attributes have been changed).

My suggestion is this:
Set 2 triggers 1 change:employees event (after all attributes have been changed).
Set 3 triggers 1 change:employees event and 1 change:name event (after all attributes have been changed).

Also, make sure that Set 2 triggers a change event after all related attributes have changed. (Not sure if this is currently done or not.)

This will bring delegated changes more in line with how normal changes behave (one change event for each changed attribute). Also, the model will be in a completely changed state instead of partial when the change event is triggered, just as with a normal change event.

Related to #7

snurra added a commit that referenced this issue Jun 27, 2015
Fixes #8
Fixes #7 (as an
automatic side-effect, doing it otherwise would be complicated)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant