-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Генерация событий на DOM узалх нано-блоков #105
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* @desc Класс который генерирует события на | ||
* корневом узле блока. | ||
*/ | ||
(function() { | ||
nb.define('DomEmitter', $.extend({ | ||
/** | ||
* @desc Генерирует событие на блоке и | ||
* корневом узле этого блока | ||
* @param {String} eventName The name of event | ||
* @param {Object} eventObject The object which will be pass to handlers | ||
* of event | ||
*/ | ||
trigger: function(eventName, eventObject) { | ||
var $node = $(this.node); | ||
|
||
$node.trigger.apply($node, arguments); | ||
return this.emit.apply(this, arguments); | ||
} | ||
}, new nb.Emitter())); | ||
}()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. вот это наверное в blocks/common логичнее положить There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Изначально я хотел положить сюда, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно посмотреть сюда There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vitkarpov Спасибо! Я туда смотрл, не хочется править код библиотеке nanoblocks. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* @desc Класс реализованный на jQuery, который позволяет генерировать и прослушивать | ||
* события на объекте | ||
*/ | ||
(function() { | ||
var Emitter = function() {}; | ||
|
||
/** | ||
* @desc Add event handler | ||
* @param {String} eventName The name of event | ||
* @param {Function} eventHandler The handler of event on the object | ||
* @return {Function} The handler of event | ||
*/ | ||
Emitter.prototype.on = function(eventName, eventHandler) { | ||
var $this = $(this); | ||
|
||
$this.on.apply($this, arguments); | ||
|
||
return eventHandler; | ||
}; | ||
|
||
/** | ||
* @desc Remove event handler from object | ||
* @param {String} eventName The name of event | ||
* @param {Function} eventHandler Then event handler function | ||
* @return {Function} The event handler function | ||
*/ | ||
Emitter.prototype.off = function(eventName, eventHandler) { | ||
var $this = $(this); | ||
|
||
$this.off.apply($this, arguments); | ||
|
||
return eventHandler; | ||
}; | ||
|
||
/** | ||
* @desc Trigger event on the object | ||
* @param {String} eventName The name of event | ||
* @param {Object} [eventObject] The object which pass to handlers | ||
*/ | ||
Emitter.prototype.emit = function(eventName, eventObject) { | ||
var $this = $(this); | ||
|
||
return $this.trigger.apply($this, arguments); | ||
}; | ||
|
||
nb.Emitter = Emitter; | ||
}()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. вот это наверное в blocks/common логичнее положить There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Что-то мне подсказывает что это нехило ударит по производительности на страницах с большим количеством элементов. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А, что конкретно смущает? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
afterEach(function() { | ||
// clear blocks | ||
nb.destroy(); | ||
$('.content').empty(); | ||
}); | ||
(function(globalSinon) { | ||
beforeEach(function() { | ||
sinon = globalSinon.sandbox.create(); | ||
}); | ||
|
||
afterEach(function() { | ||
sinon.restore(); | ||
// clear blocks | ||
nb.destroy(); | ||
$('.content').empty(); | ||
}); | ||
}(sinon)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/*global beforeEach, describe, it, expect, sinon*/ | ||
describe("Emitter Tests", function() { | ||
beforeEach(function() { | ||
this.emitter = new nb.Emitter(); | ||
}); | ||
|
||
describe('#on', function() { | ||
it('should implement interface #on', function() { | ||
expect(this.emitter.on).to.be.ok(); | ||
}); | ||
}); | ||
|
||
describe('#off', function() { | ||
it('should implement interface #off', function() { | ||
expect(this.emitter.off).to.be.ok(); | ||
}); | ||
|
||
it('should remove handler of event from object', function() { | ||
var eventHandler = this.emitter.on('test', sinon.stub()); | ||
|
||
this.emitter.off('test', eventHandler); | ||
this.emitter.emit('test'); | ||
|
||
expect(eventHandler.called).to.be.equal(false); | ||
}); | ||
}); | ||
|
||
describe('#emit', function() { | ||
it('should implement interface #trigger', function() { | ||
expect(this.emitter.emit).to.be.ok(); | ||
}); | ||
|
||
it('should trigger event on object', function() { | ||
var eventHandler = this.emitter.on('test', sinon.stub()); | ||
|
||
this.emitter.emit('test'); | ||
|
||
expect(eventHandler.called).to.be.equal(true); | ||
}); | ||
}); | ||
|
||
describe('How add emitter functionality to nanoblock', function() { | ||
beforeEach(function() { | ||
nb.define('test', {}, 'DomEmitter'); | ||
this.bTest = nb.block($('<div data-nb="test"/>').get(0)); | ||
}); | ||
|
||
it('should trigger event on nanoblock', function() { | ||
var eventHandler = this.bTest.on('test', sinon.stub()); | ||
|
||
this.bTest.trigger('test'); | ||
|
||
expect(eventHandler.called).to.be.equal(true); | ||
}); | ||
|
||
it('should trigger event on nanoblock node', function() { | ||
var eventHandler = sinon.stub(); | ||
$(this.bTest.node).on('test', eventHandler); | ||
|
||
this.bTest.trigger('test'); | ||
|
||
expect(eventHandler.called).to.be.equal(true); | ||
}); | ||
|
||
it('should trigger event on nanoblock node without duplicate', function() { | ||
var eventHandler = sinon.stub(); | ||
$(this.bTest.node).on('test', eventHandler); | ||
|
||
this.bTest.trigger('test'); | ||
|
||
expect(eventHandler.calledOnce).to.be.equal(true); | ||
}); | ||
|
||
it('should bubbling event triggered on nanoblock', function() { | ||
var $rootNode = $('<div id="rootNode"><b data-nb="test" /></div>'); | ||
var eventHandler = sinon.stub(); | ||
|
||
this.bTest = nb.block($rootNode.find('[data-nb="test"]').get(0)); | ||
|
||
$rootNode.on('test', eventHandler); | ||
|
||
this.bTest.trigger('test'); | ||
|
||
expect(eventHandler.called).to.be.equal(true); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/*global beforeEach, describe, it, expect, sinon*/ | ||
describe("Select Tests", function() { | ||
it('should bubbling event nb-select_changed', function() { | ||
var $rootNode = $('<div><div data-nb="select"/></div>'); | ||
var eventHandler = sinon.stub(); | ||
|
||
this.bSelect = nb.block($rootNode.find('[data-nb="select"]').get(0)); | ||
$rootNode.on('nb-select_changed', eventHandler); | ||
|
||
this.bSelect.trigger('nb-select_changed'); | ||
|
||
expect(eventHandler.called).to.be.equal(true); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Есть такой модуль sindresorhus/load-grunt-tasks, будет
require('load-grunt-tasks')(grunt);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Согласен, не знал.