Skip to content
This repository has been archived by the owner on Jun 8, 2019. It is now read-only.

Fix prependChildTo behaviour #12

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/assets/javascripts/backbone-support/composite_view.js
Expand Up @@ -41,7 +41,7 @@ _.extend(Support.CompositeView.prototype, Backbone.View.prototype, Support.Obser


prependChildTo: function (view, container) { prependChildTo: function (view, container) {
this.renderChild(view); this.renderChild(view);
$(container).prepend(view.el); this.$(container).prepend(view.el);
}, },


_leaveChildren: function() { _leaveChildren: function() {
Expand Down
29 changes: 28 additions & 1 deletion spec/javascripts/composite_view_spec.js
Expand Up @@ -91,6 +91,15 @@ describe("Support.CompositeView", function() {


expect($(view.el).find('.inside').text()).toEqual("Append to this!Orange!"); expect($(view.el).find('.inside').text()).toEqual("Append to this!Orange!");
}); });

it("appends the element only to elements inside the view", function(){
var view = new blankView({el: $('<div><div class="main">Append to this!</div></div>')});
var div = $("<div class='main' id='outside'></div>");
view.appendChildTo(new orangeView(), ".main");

expect($(view.el).find('.main').text()).toEqual("Append to this!Orange!");
expect($("#outside").text()).toEqual("");
});
}); });


describe("#prependChild", function() { describe("#prependChild", function() {
Expand All @@ -108,11 +117,29 @@ describe("Support.CompositeView", function() {
$("#test1").text("Prepend to this!"); $("#test1").text("Prepend to this!");


var view = new blankView({el: "#test"}); var view = new blankView({el: "#test"});
expect($("#test").text()).toEqual("");

$("#test").append($("#test1"));
view.prependChildTo(new orangeView(), "#test1"); view.prependChildTo(new orangeView(), "#test1");


expect($("#test").text()).toEqual("");
expect($("#test1").text()).toEqual("Orange!Prepend to this!"); expect($("#test1").text()).toEqual("Orange!Prepend to this!");
}); });

it("prepends child into a sub-element even if it is not added to the document", function() {
var view = new blankView({el: $('<div><div class="inside">Prepend to this!</div></div>')});
view.prependChildTo(new orangeView(), ".inside");

expect($(view.el).find('.inside').text()).toEqual("Orange!Prepend to this!");
});

it("prepends the element only to elements inside the view", function(){
var view = new blankView({el: $('<div><div class="main">Prepend to this!</div></div>')});
var div = $("<div class='main' id='outside'></div>");
view.prependChildTo(new orangeView(), ".main");

expect($(view.el).find('.main').text()).toEqual("Orange!Prepend to this!");
expect($("#outside").text()).toEqual("");
});
}); });


describe("#leave", function() { describe("#leave", function() {
Expand Down