Permalink
Browse files

Merge pull request #10 from iwz/large_dom_performance_fix

Large dom performance fix
  • Loading branch information...
robb1e committed May 13, 2013
2 parents 9da4ab3 + 50f795a commit da1f8d682611677a5dd0778a8f209b43c63c75a0
Showing with 37 additions and 6 deletions.
  1. +28 −5 spec/elemental_spec.js
  2. +9 −1 src/elemental.js
View
@@ -38,11 +38,34 @@ describe("Elemental", function(){
expect(bar).toHaveBeenCalled();
});
- it("should load a behavior nested deeply beneat the container element", function() {
- baz = jasmine.createSpy('baz');
- var container = "<div><div><div><div data-behavior='baz'> </div></div></div></div>";
- Elemental.load(container);
- expect(baz).toHaveBeenCalled();
+ describe("when the container is some specific DOM", function() {
+ it("should load a behavior nested deeply beneath the container element", function() {
+ baz = jasmine.createSpy('baz');
+ var container = "<div><div><div><div data-behavior='baz'> </div></div></div></div>";
+ Elemental.load(container);
+ expect(baz).toHaveBeenCalled();
+ });
+
+ it("should not load a behavior that is outside of the container element", function() {
+ baz = jasmine.createSpy('baz');
+ qux = jasmine.createSpy('qux');
+ var container = "<div data-behavior='baz'> </div>";
+ setFixtures("<div data-behavior='qux'> </div>");
+
+ Elemental.load(container);
+
+ expect(baz).toHaveBeenCalled();
+ expect(qux).not.toHaveBeenCalled();
+ });
+ });
+
+ describe("when the container is the document", function() {
+ it("should load a behavior nested deeply beneath the container element", function() {
+ buzz = jasmine.createSpy('buzz');
+ setFixtures("<div><div><div><div data-behavior='buzz'> </div></div></div></div>");
+ Elemental.load(document);
+ expect(buzz).toHaveBeenCalled();
+ });
});
it("should not load a behaviour that does not exist", function(){
View
@@ -29,7 +29,15 @@
};
ns.load = function(container) {
- $(container).find("*").andSelf().filter("[data-behavior]").each(function(index, element) {
+ var $selector;
+ if (container === document) {
+ $selector = $('[data-behavior]');
+ }
+ else {
+ $selector = $(container).find("*").andSelf().filter("[data-behavior]");
+ }
+
+ $selector.each(function(index, element) {
var $element = $(element);
var behaviors = $element.data('behavior');
behaviors.replace(/([^ ]+)/g, function(behavior) {

0 comments on commit da1f8d6

Please sign in to comment.