Skip to content

Commit

Permalink
Converts container 'append' to a configurable method.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsubrian authored and molily committed Apr 9, 2012
1 parent b813fee commit 99de682
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
14 changes: 8 additions & 6 deletions coffee/views/view.coffee
Expand Up @@ -13,13 +13,14 @@ define [
# As an alternative you might pass a `render` option to the constructor.
autoRender: false

# Automatic appending to DOM
# Automatic inserting into DOM
# View container element
# Set this property in a derived class to specify to selector
# of the container element. The view is automatically appended
# to the container when it’s rendered.
# of the container element. The view is automatically inserted
# into the container when it’s rendered.
# As an alternative you might pass a `container` option to the constructor.
containerSelector: null
containerMethod: 'append'
$container: null

constructor: ->
Expand Down Expand Up @@ -112,7 +113,8 @@ define [
throw new TypeError 'View#delegate: second argument must be a string'
handler = third
else
throw new TypeError 'View#delegate: two or three arguments are allowed'
throw new TypeError 'View#delegate: only two or three arguments are
allowed'

if typeof handler isnt 'function'
throw new TypeError 'View#delegate: handler argument must be function'
Expand Down Expand Up @@ -265,9 +267,9 @@ define [
#console.debug 'View#afterRender', this

# Automatically append to DOM if the container element is set
if @$container
if @$container and @$container[@containerMethod]?
#console.debug '\tappend to DOM'
@$container.append @el
@$container[@containerMethod] @el
# Trigger an event
@trigger 'addedToDOM'

Expand Down
9 changes: 6 additions & 3 deletions js/views/view.js
Expand Up @@ -15,6 +15,8 @@ define(['lib/utils', 'lib/subscriber', 'lib/view_helper'], function(utils, Subsc

View.prototype.containerSelector = null;

View.prototype.containerMethod = 'append';

View.prototype.$container = null;

function View() {
Expand Down Expand Up @@ -76,7 +78,8 @@ define(['lib/utils', 'lib/subscriber', 'lib/view_helper'], function(utils, Subsc
}
handler = third;
} else {
throw new TypeError('View#delegate: two or three arguments are allowed');
throw new TypeError('View#delegate: only two or three arguments are\
allowed');
}
if (typeof handler !== 'function') {
throw new TypeError('View#delegate: handler argument must be function');
Expand Down Expand Up @@ -169,8 +172,8 @@ define(['lib/utils', 'lib/subscriber', 'lib/view_helper'], function(utils, Subsc
};

View.prototype.afterRender = function() {
if (this.$container) {
this.$container.append(this.el);
if (this.$container && (this.$container[this.containerMethod] != null)) {
this.$container[this.containerMethod](this.el);
this.trigger('addedToDOM');
}
return this;
Expand Down

0 comments on commit 99de682

Please sign in to comment.