Skip to content

Commit

Permalink
Merge pull request #1131 from mucaho/init-order
Browse files Browse the repository at this point in the history
Setup event callbacks before init
  • Loading branch information
mucaho authored May 20, 2017
2 parents 72874ad + fc91c7e commit bd287ef
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,6 @@ Crafty.fn = Crafty.prototype = {
Object.defineProperty(this, propertyName, props[propertyName]);
}
}
// Call constructor function
if (comp && "init" in comp) {
comp.init.call(this);
}
// Bind events
if (comp && "events" in comp){
var auto = comp.events;
Expand All @@ -335,6 +331,10 @@ Crafty.fn = Crafty.prototype = {
this.bind(eventName, fn);
}
}
// Call constructor function
if (comp && "init" in comp) {
comp.init.call(this);
}
}

this.trigger("NewComponent", comps);
Expand Down
9 changes: 4 additions & 5 deletions src/core/systems.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,6 @@ Crafty.CraftySystem = (function() {
Object.defineProperty(this, propertyName, props[propertyName]);
}
}

// Run any instantiation code
if (typeof this.init === "function") {
this.init(name);
}
// If an events object is provided, bind the listed event handlers
if ("events" in template) {
var auto = template.events;
Expand All @@ -128,6 +123,10 @@ Crafty.CraftySystem = (function() {
this.bind(eventName, fn);
}
}
// Run any instantiation code
if (typeof this.init === "function") {
this.init(name);
}
};
})();

Expand Down
63 changes: 63 additions & 0 deletions tests/unit/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,69 @@
_.ok(propList.indexOf("_foo") === -1, "Property _foo is not enumerable");
});

test("component - order of handling special members", function(_) {
_.expect(5 * 5 - 1);

Crafty.c("MemberOrderTest", {
// 1st: basic prop should be added to entity
foo: 1,
// 2nd: properties should be defined on entity
properties: {
bar: {
get: function() {
if (!this.getCalled) {
_.strictEqual(this.foo, 1);
// can't check this.bar here - infinite recursion
_.strictEqual(this.baz, undefined);
_.strictEqual(this.quux, undefined);
_.strictEqual(this.quuz, undefined);
this.getCalled = true;
}
return 2;
}
}
},
// 3rd: events should be bound on entity
events: {
"CustomEvent": function() {
_.strictEqual(this.foo, 1);
_.strictEqual(this.bar, 2);
_.strictEqual(this.baz, undefined);
_.strictEqual(this.quux, undefined);
_.strictEqual(this.quuz, undefined);
this.baz = 3;
}
},
// 4th: init method should be called on entity
init: function() {
this.trigger("CustomEvent");

_.strictEqual(this.foo, 1);
_.strictEqual(this.bar, 2);
_.strictEqual(this.baz, 3);
_.strictEqual(this.quux, undefined);
_.strictEqual(this.quuz, undefined);
this.quux = 4;
},
// 5th: remove method should be called on entity
remove: function() {
_.strictEqual(this.foo, 1);
_.strictEqual(this.bar, 2);
_.strictEqual(this.baz, 3);
_.strictEqual(this.quux, 4);
_.strictEqual(this.quuz, undefined);
this.quuz = 5;
}
});
var e = Crafty.e().addComponent("MemberOrderTest").removeComponent("MemberOrderTest");

_.strictEqual(e.foo, 1);
_.strictEqual(e.bar, 2);
_.strictEqual(e.baz, 3);
_.strictEqual(e.quux, 4);
_.strictEqual(e.quuz, 5);
});

test("overwrite component definition", function(_) {
Crafty.c('MyCompDef', { a: 0 });
var e = Crafty.e('MyCompDef');
Expand Down
63 changes: 63 additions & 0 deletions tests/unit/core/systems.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,68 @@
_.strictEqual(Crafty.s('MySystemDef').b, 1);
});

test("order of handling special members", function(_) {
_.expect(5 * 5 - 1);

Crafty.s("MemberOrderTest", {
// 1st: basic prop should be added to entity
foo: 1,
// 2nd: properties should be defined on entity
properties: {
bar: {
get: function() {
if (!this.getCalled) {
_.strictEqual(this.foo, 1);
// can't check this.bar here - infinite recursion
_.strictEqual(this.baz, undefined);
_.strictEqual(this.quux, undefined);
_.strictEqual(this.quuz, undefined);
this.getCalled = true;
}
return 2;
}
}
},
// 3rd: events should be bound on entity
events: {
"CustomEvent": function() {
_.strictEqual(this.foo, 1);
_.strictEqual(this.bar, 2);
_.strictEqual(this.baz, undefined);
_.strictEqual(this.quux, undefined);
_.strictEqual(this.quuz, undefined);
this.baz = 3;
}
},
// 4th: init method should be called on entity
init: function() {
this.trigger("CustomEvent");

_.strictEqual(this.foo, 1);
_.strictEqual(this.bar, 2);
_.strictEqual(this.baz, 3);
_.strictEqual(this.quux, undefined);
_.strictEqual(this.quuz, undefined);
this.quux = 4;
},
// 5th: remove method should be called on entity
remove: function() {
_.strictEqual(this.foo, 1);
_.strictEqual(this.bar, 2);
_.strictEqual(this.baz, 3);
_.strictEqual(this.quux, 4);
_.strictEqual(this.quuz, undefined);
this.quuz = 5;
}
});
var s = Crafty.s("MemberOrderTest");
s.destroy();

_.strictEqual(s.foo, 1);
_.strictEqual(s.bar, 2);
_.strictEqual(s.baz, 3);
_.strictEqual(s.quux, 4);
_.strictEqual(s.quuz, 5);
});
})();

0 comments on commit bd287ef

Please sign in to comment.