Skip to content

Commit

Permalink
fix target display object when destroying mediator. #7
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Dec 18, 2016
1 parent 4227922 commit 8de2451
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 10 deletions.
3 changes: 3 additions & 0 deletions example/config/MyConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {

import { CircleMediator } from "../view/CircleMediator";
import { CircleView } from "../view/CircleView";
import { ChildMediator } from "../view/ChildMediator";
import { ChildView } from "../view/ChildView";
import { IMediatorMap } from "../../src/index";

@injectable()
Expand All @@ -20,6 +22,7 @@ export class MyConfig implements IConfig {

configure () {
this.mediatorMap.map(CircleView).toMediator(CircleMediator);
this.mediatorMap.map(ChildView).toMediator(ChildMediator);
}

}
23 changes: 23 additions & 0 deletions example/view/ChildMediator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { inject } from "robotlegs";
import { ChildView } from "./ChildView"
import { Mediator } from "../../src/index";

export class ChildMediator extends Mediator<ChildView> {

initialize()
{
console.log("ChildMediator initialized!");
this.view.interactive = true;
this.addViewListener("click", this.onClick, this);
}

onClick (e) {
this.view.parent.removeChild(this.view);
}

destroy () {
console.log("ChildMediator destroyed!");
}

}

15 changes: 15 additions & 0 deletions example/view/ChildView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Container, Graphics } from "pixi.js";

export class ChildView extends Container {

constructor () {
super();

let g = new Graphics();
g.beginFill(0xff00ff);
g.drawCircle(Math.random() * 800, Math.random() * 600, 50);

this.addChild(g);
}

}
3 changes: 2 additions & 1 deletion example/view/CircleMediator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { inject } from "robotlegs";
import { CircleView } from "./CircleView"
import { Mediator } from "../../src/index";
import { ChildView } from "./ChildView";

export class CircleMediator extends Mediator<CircleView> {

Expand All @@ -12,7 +13,7 @@ export class CircleMediator extends Mediator<CircleView> {
}

onClick (e) {
console.log("clicked!");
this.view.parent.addChild(new ChildView());
}

destroy () {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "robotlegs-pixi",
"version": "1.0.0-alpha.12",
"version": "1.0.0-alpha.13",
"description": "PIXI View Integration with RobotlegsJS",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -83,7 +83,7 @@
"ts-node": "^1.7.0",
"tslint": "^3.15.1",
"typedoc": "^0.5.1",
"typescript": "^2.2.0-dev.20161111",
"typescript": "^2.1.4",
"typescript-formatter": "^4.0.0",
"webpack": "^2.1.0-beta.25",
"webpack-dev-server": "^2.1.0-beta.10"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ export class MediatorManager {
var displayObject: DisplayObject = <DisplayObject>item;

// Watch Display Object for removal
if (displayObject && mapping.autoRemoveEnabled)
displayObject.on('removed', this.onRemovedFromStage, this);
// displayObject.addEventListener(Event.REMOVED_FROM_STAGE, this.onRemovedFromStage);
if (displayObject && mapping.autoRemoveEnabled) {
item._onRemovedFromStage = this.onRemovedFromStage.bind(this, item);
displayObject.on('removed', item._onRemovedFromStage, this);
}

// Synchronize with item life-cycle
this.initializeMediator(mediator, item);
Expand All @@ -63,8 +64,7 @@ export class MediatorManager {
*/
public removeMediator(mediator: any, item: any, mapping: IMediatorMapping): void {
if (item instanceof DisplayObject)
(<DisplayObject>item).off('removed', this.onRemovedFromStage);
// displayObject.removeEventListener(Event.REMOVED_FROM_STAGE, this.onRemovedFromStage);
(<DisplayObject>item).off('removed', (<any>item)._onRemovedFromStage);

this.destroyMediator(mediator);
}
Expand All @@ -73,8 +73,8 @@ export class MediatorManager {
/* Private Functions */
/*============================================================================*/

private onRemovedFromStage(event: any): void {
this._factory.removeMediators(event);
private onRemovedFromStage(displayObject: any, fromContainer: any): void {
this._factory.removeMediators(displayObject);
}

private initializeMediator(mediator: any, mediatedItem: any): void {
Expand Down

0 comments on commit 8de2451

Please sign in to comment.