Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DI bug with view prototype inheritance #6

Merged
merged 1 commit into from
Sep 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions build/backbone-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ MixinBackbone = function(Backbone) {
}
},
getViewDI: function(ViewClass, options) {
var TypeView, key;
var TypeView, diview, key, remove, removeFlag, view, _ref;
if (options == null) {
options = {};
}
Expand All @@ -284,14 +284,26 @@ MixinBackbone = function(Backbone) {
this._$_p.diViews[key] = ViewClass;
} else if (typeof ViewClass === "function") {
TypeView = ViewClass;
key = TypeView.prototype._$_di || (TypeView.prototype._$_di = _.uniqueId("_$_di"));
key = TypeView._$_di || (TypeView._$_di = _.uniqueId("_$_di"));
} else {
TypeView = ViewClass.type;
TypeView.prototype._$_di || (TypeView.prototype._$_di = _.uniqueId("_$_di"));
TypeView._$_di || (TypeView._$_di = _.uniqueId("_$_di"));
key = ViewClass.key;
}
if (this._$_p.diViews[key] == null) {
this._$_p.diViews[key] = new TypeView(options);
diview = this._$_p.diViews[key];
removeFlag = !!(diview != null ? (_ref = diview._$_p) != null ? _ref.removeFlag : void 0 : void 0);
if ((diview == null) || removeFlag) {
this._$_p.diViews[key] = view = new TypeView(options);
if (view._$_p == null) {
view._$_p = {
removeFlag: false
};
remove = view.remove;
view.remove = function() {
remove.apply(view, arguments);
return view._$_p.removeFlag = true;
};
}
}
return this._$_p.diViews[key];
},
Expand Down
2 changes: 1 addition & 1 deletion build/backbone-mixin.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/backbone-mixin.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ MixinBackbone = (Backbone)->
@_$_p.diViews[key] = ViewClass
else if typeof(ViewClass) is "function"
TypeView = ViewClass
key = TypeView::_$_di or (TypeView::_$_di = _.uniqueId("_$_di"))
key = TypeView._$_di or (TypeView._$_di = _.uniqueId("_$_di"))
else
TypeView = ViewClass.type
TypeView::_$_di or (TypeView::_$_di = _.uniqueId("_$_di"))
TypeView._$_di or (TypeView._$_di = _.uniqueId("_$_di"))
key = ViewClass.key
diview = @_$_p.diViews[key]
removeFlag = !!diview?._$_p?.removeFlag
Expand Down
8 changes: 7 additions & 1 deletion test/check-DI-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe "Check MixinBackbone DI",->
keys = @view._diViewsKeys()
values = @view._diViewsValues()
expect(1).toBe _.size(keys)
expect(@TestViewDI::_$_di).toBe keys[0]
expect(@TestViewDI._$_di).toBe keys[0]
expect(1).toBe _.size(values)
expect(view1).toBe values[0]

Expand All @@ -54,4 +54,10 @@ describe "Check MixinBackbone DI",->
view2 = @view.getViewDI @TestViewDI
expect(view2?._$_p?.removeFlag).toBe false

it "correct work with prototype inheritance", ->
ParentView = Backbone.View.extend {}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testcase isn't correct. Parentview and Childview are always have different proyotypes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be You mean that they should be different? In fact you can create some view with some basic functionality and extend it in another view. And it is normal usecase. If You try to use DI while work with this views, DI will always return only the first one.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem only with testcase. If revert logic functionality test will be pass. Testcase test nothing, i think. I can mistake, because can't check anythink at this moment, but my "brain intepreter" says that testcase should fails without fixing this bug.

ChildView = ParentView.extend {}

parentView = @view.getViewDI ParentView
childView = @view.getViewDI ChildView
expect(parentView).not.toBe childView