Skip to content
Open
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
8 changes: 8 additions & 0 deletions tasks/srp/refactoring/PersonCollection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var Backbone = require('Backbone'),
Person = require('Person');

var PersonCollection = Backbone.Collection.extend({
model: Person
});

module.export = PersonCollection;
33 changes: 33 additions & 0 deletions tasks/srp/refactoring/PersonView.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var Backbone = require('Backbone'),
rivets = require('rivets'),
yaCounter100500 = require('yaCounter100500');

var PersonView = Backbone.View.extend({

events: {
"click .b-person": "handleClick"
},

layout: $('.header'),

initialize: function(options) {
var viewModel = {
model: options.model
};
this.logger = options.logger;

this.setElement(this.layout);
this.dataBindTo(viewModel);
},

dataBindTo: function (viewModel) {
return rivets.bind(this.el, viewModel);
},

handleClick: function () {
yaCounter100500.params('user_page_visited');
Copy link
Owner

Choose a reason for hiding this comment

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

Это не про SRP, конечно, но если счетчик нужно будет убрать придется тут писать.

this.logger.log('debug', 'user_page_visited');
}
});

module.export = PersonView;
10 changes: 10 additions & 0 deletions tasks/srp/refactoring/formatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var rivets = require('rivets');

_.extend(rivets.formatters, {
isSameOrigin: function (url) {
var $parser = $('<a>');
Copy link
Owner

Choose a reason for hiding this comment

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

Нафига $? %)

Copy link

Choose a reason for hiding this comment

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

ну проще же

Copy link
Owner

Choose a reason for hiding this comment

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

Да, проще переписать уже существующий код на $.

$parser.attr('href', url);

return location.origin === $parser.origin;
Copy link
Owner

Choose a reason for hiding this comment

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

Тогда уж $parser[0].origin

Copy link

Choose a reason for hiding this comment

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

ну псевдокод ведь :)

}
});
13 changes: 13 additions & 0 deletions tasks/srp/refactoring/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>SOLID</title>
</head>
<body>
<div class="header" data-each-person="model.persons">
<a href="{person.url}" class="b-person"
data-class-b-person_origin_same="person.url | isSameOrigin">{person.name}</a>
</div>
<script src="index.js"></script>
</body>
</html>
10 changes: 10 additions & 0 deletions tasks/srp/refactoring/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var PersonCollection = require('PersonCollection'),
PersonView = require('PersonView');

(function () {
var options = {
model: new PersonCollection(/*...*/),
logger: console
};
var view = new PersonView(options);
})();
32 changes: 4 additions & 28 deletions tasks/srp/refactoring/person.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,17 @@
function Person(options) {
options = options || {};
this.name = options.name;
this.url = options.url;

this.$el = this.render();
this.bindEvents();
this.$el.appendTo('.header');
this.setName(options.name);
this.setUrl(options.url);
}

Person.prototype = {
render: function () {
var extraClass = this.isSameOrigin(this.url) ? ' b-person_origin_same' : '';
return $('<a href="' + this.url + '" class="b-person' + extraClass + '">' + this.name + '</a>');
},

isSameOrigin: function (url) {
var parser = document.createElement('a');
parser.href = url;

return location.origin === parser.origin;
},

setName: function (name) {
this.name = name;
this.$el.html(this.name);
},

setUrl: function (url) {
this.url = url;
this.$el.attr('href', this.url);
this.$el.toggleClass('b-person_origin_same', this.isSameOrigin(this.url));
},

bindEvents: function () {
this.$el.on('click', function () {
yaCounter100500.params('user_page_visited');
console.log('debug', 'user_page_visited');
});
}
};

module.export = Person;