Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fixed multilevel inheritance for controller elements and events #438

Merged
merged 1 commit into from

2 participants

Artem Litvinov Chris Engebretson
Artem Litvinov

There is an issue with the controller elements and events inheritance that was suggested by #430 improvement.
It's occurs then you have more that 2 classes in your inheritance tree. E.g.:

class A extends Spine.Controller
  elements:
    ...

class B extends A
  elements:
    ...

class C extends B
  elements:
    ...

c = new C()

In this case "c" will only contain elements and events declared in C and B classes, not A, unfortunately.

Current pull request fixes this issue by collecting the elements and events recursively.
All this stuff is also covered with updated specs.

@cengebretson I suggest merging it as soon as possible cause the merged before feature is not working properly and may be confusing.

Chris Engebretson cengebretson merged commit 390939e into from
Chris Engebretson

good catch, will get this in. thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 34 additions and 21 deletions.
  1. +3 −1 src/spine.coffee
  2. +31 −20 test/specs/controller.js
4 src/spine.coffee
View
@@ -456,9 +456,11 @@ class Controller extends Module
@events = @constructor.events unless @events
@elements = @constructor.elements unless @elements
- if parent_prototype = @constructor.__super__
+ context = @
+ while parent_prototype = context.constructor.__super__
@events = $.extend({}, parent_prototype.events, @events) if parent_prototype.events
@elements = $.extend({}, parent_prototype.elements, @elements) if parent_prototype.elements
+ context = parent_prototype
@delegateEvents(@events) if @events
@refreshElements() if @elements
51 test/specs/controller.js
View
@@ -194,36 +194,47 @@ describe("Controller", function(){
describe("inheritance", function() {
beforeEach(function() {
- element = $('<div/>').html('<div class="parent"></div><div class="child"></div>');
- Users.include({
- events: {"click .parent": "parentEventHandler"},
- elements: {".parent": "el1"},
- parentEventHandler: function() {}
+ element = $('<div/>').html('<div class="a-el"></div><div class="b-el"></div><div class="c-el"></div>');
+ A = Spine.Controller.sub({
+ events: {"click .a-el": "aEventHandler"},
+ elements: {".a-el": "elA"},
+ aEventHandler: function() {}
+ });
+
+ B = A.sub({
+ el: element,
+ events: {"click .b-el": "bEventHandler"},
+ elements: {".b-el": "elB"},
+ bEventHandler: function() {}
});
- ChildUser = Users.sub({
+ C = B.sub({
el: element,
- events: {"click .child": "childEventHandler"},
- elements: {".child": "el2"},
- childEventHandler: function() {}
+ events: {"click .c-el": "cEventHandler"},
+ elements: {".c-el": "elC"},
+ cEventHandler: function() {}
});
- childUser = new ChildUser();
+ c = new C();
});
- it("should inherit elements from parent controller", function(){
- expect(childUser.el1).toBeDefined();
- expect(childUser.el2).toBeDefined();
+ it("should inherit elements from parent controllers", function(){
+ expect(c.elA).toBeDefined();
+ expect(c.elB).toBeDefined();
+ expect(c.elC).toBeDefined();
});
- it("should inherit events from parent controller", function(){
- spyOn(childUser, 'parentEventHandler');
- spyOn(childUser, 'childEventHandler');
- childUser.el.find('.parent').click();
- childUser.el.find('.child').click();
+ it("should inherit events from parent controllers", function(){
+ spyOn(c, 'aEventHandler');
+ spyOn(c, 'bEventHandler');
+ spyOn(c, 'cEventHandler');
+ c.el.find('.a-el').click();
+ c.el.find('.b-el').click();
+ c.el.find('.c-el').click();
- expect(childUser.parentEventHandler).toHaveBeenCalled();
- expect(childUser.childEventHandler).toHaveBeenCalled();
+ expect(c.aEventHandler).toHaveBeenCalled();
+ expect(c.bEventHandler).toHaveBeenCalled();
+ expect(c.cEventHandler).toHaveBeenCalled();
});
});
});
Something went wrong with that request. Please try again.