Skip to content

Commit

Permalink
Merge pull request #819 from seansfkelley/options-as-fn
Browse files Browse the repository at this point in the history
Don't ignore default options in the view constructor when it's a function.
  • Loading branch information
cobbweb committed Jan 10, 2014
2 parents 85fd1e7 + dc1a94d commit 581f1f9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions spec/javascripts/view.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,24 @@ describe("base view", function(){
'lila': 'zoidberg'
}
});
var presetOptionsFn = Marionette.View.extend({
options: function () {
return { 'fry': 'bender' };
}
});

it("should take and store view options", function() {
var viewInstance = new view({"Guybrush": "Island"});
expect(viewInstance.options.Guybrush).toBe("Island");
});

it("should take and store view options as a function", function() {
var viewInstance = new view(function(){
return { "Guybrush": "Island" }
});
expect(viewInstance.options.Guybrush).toBe("Island");
});

it("should have an empty hash of options by default", function() {
var viewInstance = new view;
expect(typeof(viewInstance.options.Guybrush)).toBe("undefined");
Expand All @@ -152,6 +164,11 @@ describe("base view", function(){
var viewInstance = new presetOptions;
expect(viewInstance.options.lila).toBe("zoidberg");
});

it("should retain options set on view class as a function", function() {
var viewInstance = new presetOptionsFn;
expect(viewInstance.options.fry).toBe("bender");
});
});

describe("should expose its options in the constructor", function() {
Expand Down
2 changes: 1 addition & 1 deletion src/marionette.view.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Marionette.View = Backbone.View.extend({
// this is a backfill since backbone removed the assignment
// of this.options
// at some point however this may be removed
this.options = _.extend({}, this.options, options);
this.options = _.extend({}, _.result(this, 'options'), _.isFunction(options) ? options.call(this) : options);

// parses out the @ui DSL for events
this.events = this.normalizeUIKeys(_.result(this, 'events'));
Expand Down

0 comments on commit 581f1f9

Please sign in to comment.