Created
March 12, 2014 10:42
-
-
Save lorenzoplanas/9504525 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Listen for DOM events and trigger applicable methods | |
this.domListen = function(element) { | |
// Using jQuery's .on() for DOM events | |
(element || this.$element).on("click", ".close", null, self.unload); | |
} | |
// Tear down this presenter | |
this.unload = function(e) { | |
if (e) { e.preventDefault(); }; | |
self.unlisten.call(self); | |
self.domUnlisten.call(self); | |
self.remove.call(self); | |
}; | |
// Remove from DOM | |
this.remove = function() { | |
this.$element.remove(); | |
}; |
This file contains hidden or 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
describe("#domListen", function() { | |
it("binds to click on .close", function() { | |
var timesTriggered = 0; | |
presenter.unload = function() { timesTriggered = 1; }; | |
presenter.render(); | |
presenter.$close().trigger('click'); | |
expect(timesTriggered).toBe(1); | |
}); | |
}); |
This file contains hidden or 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
// ******** Presenter code | |
this.domUnlisten = function(element) { | |
// Using jQuery .off() | |
(element || this.$element).off("click", ".close") | |
} | |
// ******** Spec code | |
describe("#domUnlisten", function() { | |
it("unbinds from click on .close", function() { | |
var timesTriggered = 0; | |
presenter.unload = function() { timesTriggered += 1; }; | |
presenter.render(); | |
presenter.$close().trigger('click'); | |
expect(timesTriggered).toBe(1); | |
timesTriggered = 0; | |
presenter.domUnlisten(); | |
presenter.$close().trigger('click'); | |
expect(timesTriggered).toBe(0); | |
}); | |
}); |
This file contains hidden or 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
var ItemPresenter = function($element, options) { | |
var self = this; | |
this.$element = $element; | |
// bus = $.observable({}) -- $.observable comes from Riot.js | |
this.bus = options.bus; | |
this.listen = function() { | |
// This is Riot.js' .on() | |
this.bus.on("list:view:unload", self.unload); | |
}; |
This file contains hidden or 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
describe("#listen", function() { | |
it("binds to list:view:unload on the bus", function() { | |
var timesTriggered = 0; | |
presenter.unload = function() { timesTriggered += 1; } | |
bus.trigger("list:view:unload"); | |
expect(timesTriggered).toBe(0); | |
presenter.listen(); | |
bus.trigger("list:view:unload"); | |
expect(timesTriggered).toBe(1); | |
}); | |
}); |
This file contains hidden or 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
// ******** Presenter code | |
this.unlisten = function() { | |
// Using Riot.js .off() | |
this.bus.off("list:view:unload", self.unload); | |
}; | |
// ******** Spec code | |
describe("#unlisten", function() { | |
it("unbinds from list:view:unload on the bus", function() { | |
var timesTriggered = 0; | |
presenter.unload = function() { timesTriggered += 1; }; | |
presenter.listen(); | |
bus.trigger("list:view:unload"); | |
expect(timesTriggered).toBe(1); | |
timesTriggered = 0; | |
presenter.unlisten(); | |
bus.trigger("list:view:unload"); | |
expect(timesTriggered).toBe(0); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment