This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from quarterto/mount-lifecycle-event
Add mount lifecycle method/event and unmount event
- Loading branch information
Showing
9 changed files
with
251 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,16 @@ | ||
## Interactions | ||
## Interacting with the DOM | ||
|
||
Not all modules need interaction or logic, but when they do FruitMachine has everything you need. | ||
|
||
#### Setting up | ||
Sometimes, modules need to interact with the DOM, for example to register event handlers or set up a non-fruitmachine component. The `mount` lifecycle method is called whenever a module is associated with a new DOM element, allowing you to perform setup that requires the DOM: | ||
|
||
```js | ||
var Apple = fruitmachine.define({ | ||
name: 'apple', | ||
template: function(){ return '<button>Click Me</button>'; }, | ||
setup: function() { | ||
var self = this; | ||
this.button = this.el.querySelector('tear me down'); | ||
this.onButtonClick = function() { | ||
alert('clicked'); | ||
}; | ||
|
||
this.button.addEventListener('click', this.onButtonClick); | ||
} | ||
}); | ||
|
||
var apple = new Apple(); | ||
|
||
apple | ||
.render() | ||
.inject(document.body) | ||
.setup(); /* 1 */ | ||
``` | ||
|
||
1. *The button is now active* | ||
|
||
#### Tearing down ([example](http://ftlabs.github.io/fruitmachine/example/interactions)) | ||
template: function() { return '<button>Click me</button>' }, | ||
|
||
```js | ||
var Apple = fruitmachine.define({ | ||
name: 'apple', | ||
template: function(){ return '<button>Click Me</button>'; }, | ||
setup: function() { | ||
var self = this; | ||
this.button = this.el.querySelector('tear me down'); | ||
this.onButtonClick = function() { | ||
alert('tearing down'); | ||
self.teardown(); /* 1 */ | ||
}; | ||
|
||
this.button.addEventListener('click', this.onButtonClick); | ||
mount: function() { | ||
this.el.addEventListener('click', function() { | ||
alert('clicked'); | ||
}); | ||
}, | ||
teardown: function() { | ||
this.button.removeEventListener('click', this.onButtonClick); | ||
} | ||
}); | ||
|
||
var apple = new Apple(); | ||
|
||
apple | ||
.render() | ||
.inject(document.body) | ||
.setup(); /* 1 */ | ||
``` | ||
|
||
1. *Teardown is called when the button is clicked, removing the event listener* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
var assert = buster.assertions.assert; | ||
|
||
buster.testCase('View#mount()', { | ||
setUp: helpers.createView, | ||
|
||
"Should give a view an element": function() { | ||
var el = document.createElement('div'); | ||
this.view.mount(el); | ||
|
||
assert.equals(this.view.el, el); | ||
}, | ||
|
||
"Should be called when the view is rendered": function() { | ||
var mount = this.spy(this.view, 'mount'); | ||
this.view.render(); | ||
assert.called(mount); | ||
}, | ||
|
||
"Should be called on a child when its parent is rendered": function() { | ||
var mount = this.spy(this.view.module('apple'), 'mount'); | ||
this.view.render(); | ||
assert.called(mount); | ||
}, | ||
|
||
"Should be called on a child when its parent is rerendered": function() { | ||
var mount = this.spy(this.view.module('apple'), 'mount'); | ||
this.view.render(); | ||
this.view.render(); | ||
assert.calledTwice(mount); | ||
}, | ||
|
||
"Should call custom mount logic": function() { | ||
var mount = this.spy(); | ||
|
||
var Module = fruitmachine.define({ | ||
name: 'module', | ||
template: function() { | ||
return 'hello'; | ||
}, | ||
|
||
mount: mount | ||
}); | ||
|
||
var m = new Module(); | ||
m.render(); | ||
|
||
assert.called(mount); | ||
}, | ||
|
||
|
||
"Should be a good place to attach event handlers that don't get trashed on parent rerender": function() { | ||
var handler = this.spy(); | ||
|
||
var Module = fruitmachine.define({ | ||
name: 'module', | ||
tag: 'button', | ||
template: function() { | ||
return 'hello'; | ||
}, | ||
|
||
mount: function() { | ||
this.el.addEventListener('click', handler); | ||
} | ||
}); | ||
|
||
var m = new Module(); | ||
|
||
var layout = new Layout({ | ||
children: { | ||
1: m | ||
} | ||
}); | ||
|
||
layout.render(); | ||
m.el.click(); | ||
|
||
assert.called(handler); | ||
|
||
layout.render(); | ||
m.el.click(); | ||
|
||
assert.calledTwice(handler); | ||
}, | ||
|
||
"before mount and mount events should be fired": function() { | ||
var beforeMountSpy = this.spy(); | ||
var mountSpy = this.spy(); | ||
this.view.on('before mount', beforeMountSpy); | ||
this.view.on('mount', mountSpy); | ||
|
||
this.view.render(); | ||
assert.callOrder(beforeMountSpy, mountSpy); | ||
}, | ||
|
||
"Should only fire events if the element is new": function() { | ||
var mountSpy = this.spy(); | ||
this.view.on('mount', mountSpy); | ||
|
||
this.view.render(); | ||
this.view._getEl(); | ||
assert.calledOnce(mountSpy); | ||
}, | ||
|
||
tearDown: helpers.destroyView | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.