Skip to content

Commit

Permalink
Set view options before Behaviors are initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonLaster authored and samccone committed Apr 24, 2014
1 parent 6a29e7a commit 9cfe7a2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
41 changes: 37 additions & 4 deletions spec/javascripts/behaviors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,49 @@ describe("Behaviors", function(){
});
});

describe("behavior initialize", function() {
var Behavior = Marionette.Behavior.extend({
initialize: sinon.spy()
describe('behavior initialize', function() {
var View, Behavior, Obj;
var behaviorOptions, viewOptions;

beforeEach(function() {
Behavior = Marionette.Behavior.extend({
initialize: sinon.spy()
});

Obj = {
Tooltip: Marionette.Behavior.extend({
initialize: function(options, view) {
behaviorOptions = options;
viewOptions = view.options;
}
})
};

View = Marionette.ItemView.extend({
template: _.template(''),
behaviors: {
Tooltip: {
position: 'left'
}
}
});

Marionette.Behaviors.behaviorsLookup = Obj;
});

it("should call initialize when a behavior is created", function() {
var b = new Behavior({}, {});

expect(b.initialize).toHaveBeenCalled();
});

it('should call initialize when a behavior is created', function() {
var view = new View({
words: 'big'
});

expect(viewOptions).toEqual(view.options);
expect(behaviorOptions).toEqual(View.prototype.behaviors.Tooltip);
});
});

describe("behavior events", function() {
Expand Down
9 changes: 5 additions & 4 deletions src/marionette.view.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ Marionette.View = Backbone.View.extend({
constructor: function(options){
_.bindAll(this, "render");

if (_.isObject(this.behaviors)) {
new Marionette.Behaviors(this);
}

// this exposes view options to the view initializer
// this is a backfill since backbone removed the assignment
// of this.options
Expand All @@ -19,6 +15,11 @@ Marionette.View = Backbone.View.extend({

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

if (_.isObject(this.behaviors)) {
new Marionette.Behaviors(this);
}

Backbone.View.prototype.constructor.apply(this, arguments);

Marionette.MonitorDOMRefresh(this);
Expand Down

0 comments on commit 9cfe7a2

Please sign in to comment.