-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from ilyavolodin/no-view-onof-binding
Rule no-view-onoff-binding (fixes #35)
- Loading branch information
Showing
6 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Prevent using on/off bindings inside views (no-view-onoff-binding) | ||
|
||
Instead of using `on` and `off` methods to subscribe to model/collection events, use `listenTo` and `stopListening` methods. | ||
If you remove a view that uses `on` binding to the model events, view is not completely removed from the memory, because | ||
model still has reference to event handler function that lives inside the view. This can be avoided by using `off` before | ||
removing a view. Or by using `listenTo` function. | ||
|
||
|
||
## Rule Details | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
|
||
Backbone.View.extend({ | ||
initalize: function() { | ||
this.model.on("change", this.render); | ||
} | ||
}); | ||
|
||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
|
||
Backbone.View.extend({ | ||
initialize: function() { | ||
this.listenTo(this.model, "change", this.render); | ||
} | ||
}); | ||
|
||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you destroy your models more often then you destroy your views, then you should use `on` method instead of `listenTo`. | ||
Or if you always use `off` method before destroying the view. | ||
|
||
|
||
## Further Reading | ||
|
||
[BackboneJS Documentation](http://backbonejs.org/#Events-listenTo) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* @fileoverview Prevent using on/off bindings inside views | ||
* @author Ilya Volodin | ||
* @copyright 2015 Ilya Volodin. All rights reserved. | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
var helper = require("../backbone-helper.js"); | ||
|
||
module.exports = function(context) { | ||
|
||
var backboneView = []; | ||
var settings = context.settings || /* istanbul ignore next */ {}; | ||
|
||
//-------------------------------------------------------------------------- | ||
// Public | ||
//-------------------------------------------------------------------------- | ||
|
||
return { | ||
"CallExpression": function(node) { | ||
backboneView.push(backboneView[backboneView.length - 1] || helper.isBackboneView(node, settings.backbone)); | ||
}, | ||
"CallExpression:exit": function(node) { | ||
if (helper.isBackboneView(node, settings.backbone)) { | ||
backboneView.pop(); | ||
} | ||
}, | ||
"MemberExpression": function(node) { | ||
if (backboneView[backboneView.length - 1] && | ||
node.object.type === "MemberExpression" && | ||
node.object.object.type === "ThisExpression" && | ||
(node.object.property.name === "model" || node.object.property.name === "collection")) { | ||
if (node.property.name === "on") { | ||
context.report(node, "Use listenTo instead of 'on'."); | ||
} else if (node.property.name === "off") { | ||
context.report(node, "Use stopListening instead of 'off'."); | ||
} | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* @fileoverview Prevent using on/off bindings inside views | ||
* @author Ilya Volodin | ||
* @copyright 2015 Ilya Volodin. All rights reserved. | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var eslint = require("eslint").linter, | ||
ESLintTester = require("eslint-tester"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
var eslintTester = new ESLintTester(eslint); | ||
eslintTester.addRuleTest("lib/rules/no-view-onoff-binding", { | ||
|
||
valid: [ | ||
"Backbone.View.extend({ initialize: function() { this.$el.on('click', this.render); } });", | ||
"Backbone.Model.extend({ initialize: function() { this.collection.on('change', this.changeSomething); } });", | ||
"Backbone.View.extend({ initialize: function() { this.listenTo(this.model, 'change', this.render); } });", | ||
"Backbone.View.extend({ initialize: function() { this.model.attributes[0] = 'test'; } });" | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: "Backbone.View.extend({ initialize: function() { this.model.on('change', this.render); } });", | ||
errors: [ { message: "Use listenTo instead of 'on'." } ] | ||
}, | ||
{ | ||
code: "Backbone.View.extend({ initialize: function() { this.collection.on('change', this.render); } });", | ||
errors: [ { message: "Use listenTo instead of 'on'." } ] | ||
}, | ||
{ | ||
code: "Backbone.View.extend({ initialize: function() { this.model.off('change', this.render); } });", | ||
errors: [ { message: "Use stopListening instead of 'off'." } ] | ||
}, | ||
{ | ||
code: "Backbone.View.extend({ initialize: function() { this.collection.off('change', this.render); } });", | ||
errors: [ { message: "Use stopListening instead of 'off'." } ] | ||
} | ||
] | ||
}); |