Skip to content

Commit

Permalink
after()/before() touch-ups + unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arturadib committed Jul 30, 2011
1 parent ddc7451 commit 08ce0c2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
16 changes: 9 additions & 7 deletions agility.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@

_container: {

// Adds child object to container, appends view, listens for child removal
_appendprepend: function(obj, selector, eventStr){
// Adds child object to container, appends/prepends/etc view, listens for child removal
_insertObject: function(obj, selector, method){
var self = this;
if (!util.isAgility(obj)) {
throw "agility.js: append argument is not an agility object";
}
this._container.children[obj._id] = obj; // children is *not* an array; this is for simpler lookups by global object id
this.trigger(eventStr, [obj, selector]);
this.trigger(method, [obj, selector]);
// ensures object is removed from container when destroyed:
obj.bind('destroy', function(event, id){
self._container.remove(id);
Expand All @@ -154,19 +154,19 @@
},

append: function(obj, selector) {
return this._container._appendprepend.call(this, obj, selector, 'append');
return this._container._insertObject.call(this, obj, selector, 'append');
},

prepend: function(obj, selector) {
return this._container._appendprepend.call(this, obj, selector, 'prepend');
return this._container._insertObject.call(this, obj, selector, 'prepend');
},

after: function(obj, selector) {
return this._container._appendprepend.call(this, obj, selector, 'after');
return this._container._insertObject.call(this, obj, selector, 'after');
},

before: function(obj, selector) {
return this._container._appendprepend.call(this, obj, selector, 'before');
return this._container._insertObject.call(this, obj, selector, 'before');
},

// Removes child object from container
Expand Down Expand Up @@ -570,11 +570,13 @@

// Triggered after child obj is inserted in the container
_before: function(event, obj, selector){
if (!selector) throw 'agility.js: _before needs a selector';
this.view.$(selector).before(obj.view.$());
},

// Triggered after child obj is inserted in the container
_after: function(event, obj, selector){
if (!selector) throw 'agility.js: _after needs a selector';
this.view.$(selector).after(obj.view.$());
},

Expand Down
10 changes: 10 additions & 0 deletions test/public/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@
obj1.prepend(obj2, 'ul');
equals(obj1.view.$('ul span').prev().html(), 'hello', 'prepend() prepends at given selector');

obj1 = $$({}, '<div><ul><li id="a"/> <li id="b"/></ul></div>');
obj2 = $$('hello'); // default format should have a <div> root
obj1.before(obj2, '#b');
equals(obj1.view.$('ul li#a').next().html(), 'hello', 'before() inserts correctly');

obj1 = $$({}, '<div><ul><li id="a"/> <li id="b"/></ul></div>');
obj2 = $$('hello'); // default format should have a <div> root
obj1.after(obj2, '#a');
equals(obj1.view.$('ul li#a').next().html(), 'hello', 'after() inserts correctly');

obj1 = $$({}, '<div><span></span></div>');
for (var i=0;i<10;i++) {
obj2 = $$('hello', '<div class="test"></div>'); // default format should have a <div> root
Expand Down

0 comments on commit 08ce0c2

Please sign in to comment.