Skip to content

Commit

Permalink
allow templateHelpers to be passed as constructor function options. f…
Browse files Browse the repository at this point in the history
…ixes #696
  • Loading branch information
Derick Bailey committed Aug 13, 2013
1 parent 7e0cff7 commit e8165eb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
### v1.1 [view commit logs](https://github.com/marionettejs/backbone.marionette/compare/v1.0.4...v1.1)

* View
* Marionette.View / All Views
* Fix for `ui` bindings to not be removed from view prototype, if unrendered view is closed
* Template helpers can now be provided as a constructor function option

* Layout
* Will properly attach regions if the layout's `close` method was called prior to `render`
Expand Down
15 changes: 15 additions & 0 deletions docs/marionette.view.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,21 @@ view = new MyView({
view.render(); //=> "I think that Backbone.Marionette is the coolest!";
```

The `templateHelpers` can also be provided as a constructor parameter
for any Marionette view type that supports the helpers.

```js
var MyView = Marionette.ItemView.extend({
// ...
});

new MyView({
templateHelpers: {
doFoo: function(){ /* ... */ }
}
});
```

### Accessing Data Within The Helpers

In order to access data from within the helper methods, you
Expand Down
32 changes: 32 additions & 0 deletions spec/javascripts/templateHelpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,38 @@ describe("template helper methods", function(){
});
});

describe("when templateHelpers is provided to constructor options", function(){
var view, renderData;

var View = Backbone.Marionette.ItemView.extend({
template: function(data){
renderData = data;
}
});

beforeEach(function(){
var model = new Backbone.Model({bar: "baz"});

view = new View({
model: model,
templateHelpers: {
foo: function(){
}
}
});

view.render();
});

it('should include the template helpers in the data object', function(){
expect(renderData.foo).not.toBeUndefined();
});

it('should still have the data from the model', function(){
expect(renderData.bar).toBe("baz");
});
});

});

});
2 changes: 1 addition & 1 deletion src/marionette.view.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Marionette.View = Backbone.View.extend({
// are copies to the object passed in.
mixinTemplateHelpers: function(target){
target = target || {};
var templateHelpers = this.templateHelpers;
var templateHelpers = Marionette.getOption(this, "templateHelpers");
if (_.isFunction(templateHelpers)){
templateHelpers = templateHelpers.call(this);
}
Expand Down
5 changes: 4 additions & 1 deletion upgradeGuide.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
## Upgrade to v1.1.0

v1.1.0 adds a few new features, but should not break any existing API or
behavior. It should, therefore, be a drop-in replacement for v1.0.x
behavior. It should, therefore, be a drop-in replacement for v1.0.x.

Please see the [changelog](https://github.com/marionettejs/backbone.marionette/blob/master/changelog.md)
for the complete list of what was added and fixed in this release.

## Upgrade to v1.0.0

Expand Down

0 comments on commit e8165eb

Please sign in to comment.