-
Notifications
You must be signed in to change notification settings - Fork 10
Рефакторинг #3
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
base: gh-pages
Are you sure you want to change the base?
Рефакторинг #3
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; |
| 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'); | ||
| this.logger.log('debug', 'user_page_visited'); | ||
| } | ||
| }); | ||
|
|
||
| module.export = PersonView; | ||
| 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>'); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Нафига $? %) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ну проще же
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Да, проще переписать уже существующий код на $. |
||
| $parser.attr('href', url); | ||
|
|
||
| return location.origin === $parser.origin; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тогда уж There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ну псевдокод ведь :) |
||
| } | ||
| }); | ||
| 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> |
| 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); | ||
| })(); |
| 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это не про SRP, конечно, но если счетчик нужно будет убрать придется тут писать.