Browse files

allow specifying of how to attach view's el to HTML element in region…

… manager's 'show' method
  • Loading branch information...
1 parent afa0fbf commit 5fe26e60e7451ba16fcb8e8ce0a61489a0a5e477 @derickbailey committed Feb 29, 2012
Showing with 27 additions and 4 deletions.
  1. +5 −4 backbone.marionette.js
  2. +22 −0 readme.md
View
9 backbone.marionette.js
@@ -293,10 +293,10 @@ Backbone.Marionette = (function(Backbone, _, $){
// directly from the `el` attribute. Also calls an optional
// `onShow` and `close` method on your view, just after showing
// or just before closing the view, respectively.
- show: function(view){
+ show: function(view, appendMethod){
this.ensureEl();
this.close();
- this.open(view);
+ this.open(view, appendMethod);
this.currentView = view;
},
@@ -309,11 +309,12 @@ Backbone.Marionette = (function(Backbone, _, $){
// Internal method to render and display a view. Not meant
// to be called from any external code.
- open: function(view){
+ open: function(view, appendMethod){
var that = this;
+ appendMethod = appendMethod || "html";
$.when(view.render()).then(function () {
- that.$el.html(view.el);
+ that.$el[appendMethod](view.el);
view.onShow && view.onShow();
that.trigger("view:show", view);
});
View
22 readme.md
@@ -287,6 +287,27 @@ var anotherView = new AnotherView();
MyApp.mainRegion.show(anotherView);
```
+### Set How View's `el` Is Attached
+
+You can specify a second parameter to the `show` method,
+which will be used to determine how the HTML from the view's
+`el` is attached to the DOM region that is being managed.
+
+The options include any valid jQuery DOM object method, such
+as `html`, `text`, `append`, etc.
+
+```js
+MyApp.mainRegion,show(myView, "append");
+```
+
+This example will use jQuery's `$.append` function to append
+the new view to the current HTML.
+
+**WARNING**: Be careful when using this feature, as the view
+you are replacing may not be managed / closed correctly as a
+result. This can cause unexpected behavior, memory leaks or
+other problems. **Use At Your Own Risk**
+
### Attach Existing View
There are some scenarios where it's desirable to attach an existing
@@ -1203,6 +1224,7 @@ I'm using [Docco](http://jashkenas.github.com/docco/) to generate the annotated
* Added `CompositeView` for managing leaf-branch/composite model structures
* Added `CompositeRegion` for managing nested views and nested region managers
* Added `attachView` method to `RegionManager` to attach existing view without rendering / replacing
+* Specify how to attach HTML to DOM in region manager's `show` method
#### v0.4.8

0 comments on commit 5fe26e6

Please sign in to comment.