-
Notifications
You must be signed in to change notification settings - Fork 8
Дз4 #7
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: master
Are you sure you want to change the base?
Дз4 #7
Changes from 7 commits
438a6b8
505ce51
e2c806d
c4c402c
3a93841
e611ff4
75cc9ea
c7546af
9d41173
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,3 @@ | ||
| [submodule "qunit"] | ||
| path = qunit | ||
| url = git://github.com/jquery/qunit.git |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| var Collection = function (items) { | ||
| "use strict"; | ||
|
|
||
| this.items = []; | ||
| var key; | ||
|
|
||
| for (key in items) { | ||
| if (items.hasOwnProperty(key)) { | ||
| this.items.push(items[key]); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Добавляет в коллекцию объект | ||
| * @param {object} model | ||
| * | ||
| * @return {Collection} * @example | ||
| * | ||
| */ | ||
| Collection.prototype.add = function (model) { | ||
| "use strict"; | ||
|
|
||
| var temp = new Collection(this.items); | ||
| temp.items.push(model); | ||
| return temp; | ||
| }; | ||
|
|
||
| /** | ||
| * @param {Function} selector | ||
| * | ||
| * @see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter | ||
| * | ||
| * @example | ||
| * new Collection().filter(function (item) { | ||
| * return item.get('attendee').indexOf("me") !== -1; | ||
| * }); | ||
| * @return {Collection} | ||
| */ | ||
| Collection.prototype.filter = function (selector) { | ||
| "use strict"; | ||
|
|
||
| if (typeof selector !== "function") { | ||
| throw new Error('Argument must be function'); | ||
| } | ||
|
|
||
| return new Collection(this.items.filter(selector)); | ||
| }; | ||
|
|
||
| /** | ||
| * Принимает функцию сортировки и сортирует на основе ее | ||
| * @param {function} selector | ||
| * | ||
| * @return {Collection} * @example | ||
| * | ||
| */ | ||
| Collection.prototype.sort = function (selector) { | ||
| "use strict"; | ||
|
|
||
| if (typeof selector !== "function") { | ||
| throw new Error('Argument must be function'); | ||
| } | ||
|
|
||
| return new Collection(this.items.sort(selector)); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /*global alert: true*/ | ||
| /*global Model: true*/ | ||
|
|
||
| function isDate(date) { | ||
| "use strict"; | ||
|
|
||
| if (typeof date === 'undefined') { | ||
| return false; | ||
| } | ||
| if (typeof date.getMonth !== 'function') { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| function inherits(constructor, superconstructor) { | ||
| "use strict"; | ||
|
|
||
| var Func = function () { }; | ||
|
|
||
| Func.prototype = superconstructor.prototype; | ||
| constructor.prototype = new Func(); | ||
| } | ||
|
|
||
| var Event = function (data) { | ||
| "use strict"; | ||
|
|
||
| Model.apply(this, arguments); | ||
| }; | ||
|
|
||
| inherits(Event, Model); | ||
|
|
||
| /** | ||
| * Валидирует объект event, либо undefined, если в объекте отсутвуют обязательные поля | ||
| * eventObject{ | ||
| * name - название события | ||
| * start - начало | ||
| * end - окончание | ||
| * location - место | ||
| * remindTime - за сколько минут до события напомнить | ||
| * description - описание | ||
| * } | ||
|
|
||
| * @param {object} obj Объект | ||
| * @example | ||
| * Event({ | ||
| * name: "Пара по веб-технологиям", | ||
| * start: new Date("2012-10-20 10:00:00"), | ||
| * end: new Date("2012-10-20 12:50:00"), | ||
| * location: "5 этаж", | ||
| * remindTime: 10, | ||
| * description: "Взять бумагу и ручку, не брать бук!" | ||
| * }) | ||
| * | ||
| * @return {Object} | ||
| */ | ||
| Event.prototype.validate = function () { | ||
| "use strict"; | ||
|
|
||
| var remindTime = this.remindTime || 0; | ||
| this.raiting = this.raiting || 0; | ||
|
|
||
| if (!isDate(this.get("start"))) { | ||
| throw new Error('Field "start" must be Date format'); | ||
| } | ||
|
|
||
| if (!isDate(this.end)) { | ||
| this.end = this.start; | ||
| } | ||
|
|
||
| if (this.end < this.start) { | ||
| this.end = this.start; | ||
| } | ||
|
|
||
| return { | ||
| "name": this.name || "(Нет темы)", | ||
| "start": this.start, | ||
| "end": this.end, | ||
| "location": this.location || "", | ||
| "remindTime": remindTime, | ||
| "description": this.description || "(отсутствует)", | ||
| "raiting": this.raiting | ||
| }; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| var Events = function (data) { | ||
| "use strict"; | ||
|
|
||
| Collection.apply(this, arguments); | ||
| }; | ||
|
|
||
| inherits(Events, Collection); | ||
|
|
||
| /** | ||
| * Возвращает прошедшие события, отсортированные по дате начала | ||
| * @param {events} - коллекция объектов типа event | ||
| * @return коллекция объектов типа event | ||
| */ | ||
| Events.prototype.past = function () { | ||
| "use strict"; | ||
|
|
||
| return this.filter(function (events) { | ||
| return events.start < new Date(); | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * Возвращает предстоящие события, отсортированные по дате начала | ||
| * @return коллекция объектов типа event | ||
| */ | ||
| Events.prototype.coming = function () { | ||
| "use strict"; | ||
|
|
||
| return this.filter(function (events) { | ||
| return events.start > new Date(); | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * Возвращает события, которые произойдут через опр переиод времени,отсортированные по дате начала | ||
| * @param {days} - период (в днях) времени | ||
| * @return коллекция объектов типа event | ||
| */ | ||
| Events.prototype.comeThrough = function (days) { | ||
| "use strict"; | ||
|
|
||
| var now = new Date(); | ||
| now.setDate(now.getDate() + days); | ||
|
|
||
| return this.coming() | ||
| .filter(function (events) { | ||
| return events.start < now; | ||
| }); | ||
|
Member
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. Вот так более читаемо var result = this.coming()
.filter(function (events) {
return events.start < now;
});
return result; |
||
| }; | ||
|
|
||
| Events.prototype.byEndTime = function () { | ||
| "use strict"; | ||
|
|
||
| return this.sort(function (a, b) { | ||
| return a.end - b.end; | ||
| }); | ||
| }; | ||
|
|
||
| Events.prototype.byRaiting = function () { | ||
| "use strict"; | ||
|
|
||
| return this.sort(function (a, b) { | ||
| return a.raiting - b.raiting; | ||
| }); | ||
| }; | ||
|
|
||
| Events.prototype.byStartTime = function () { | ||
| "use strict"; | ||
|
|
||
| return this.sort(function (a, b) { | ||
| return a.start - b.start; | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * Возвращает события, отсортированные по дате начала по возр/убыв | ||
| * от старых к новым / наоборот. По умолчанию сортирует в порядке возрастания | ||
| * @param {events} - коллекция объектов типа event | ||
| * @param {isAscending} - необязательный параметр - указывает порядок сортировки. | ||
| * при отсутсвии сортируется по возрастанию. | ||
| * @return коллекция объектов типа event | ||
| */ | ||
| Events.prototype.sortByTime = function (isAscending) { | ||
| "use strict"; | ||
|
|
||
| if (isAscending || typeof isAscending === "undefined") { | ||
|
Member
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. лучше вместо такой длинной проверки сделать в начале вот так |
||
| return this | ||
| .byStartTime(); | ||
| } | ||
| return this.byStartTime() | ||
| .reverse(); | ||
| }; | ||
|
|
||
| /** | ||
| * Возвращает события, отсортированные по рейтингу по убыв/возрастанию | ||
| * от с более высоким рейтингом к самому низко приоритетному / наоборот. По умолчанию сортирует в порядке убывания | ||
| * @param {events} - коллекция объектов типа event | ||
| * @param {isAscending} - необязательный параметр - указывает порядок сортировки. | ||
| * при отсутствии сортируется по убыванию. | ||
| * @return коллекция объектов типа event | ||
| */ | ||
|
Member
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. Формат JSDoc не корректный - http://code.google.com/p/jsdoc-toolkit/wiki/TagReference /**
* Создает новый экземпляр Circle по диаметру.
*
* @param {number} d Диаметр окружности.
*
* @return {Circle} Новый объект Circle.
*/
Circle.fromDiameter = function (d) {
return new Circle(d / 2);
}; |
||
| Events.prototype.sortByRaiting = function (isAscending) { | ||
| "use strict"; | ||
|
|
||
| if (isAscending || typeof isAscending === "undefined") { | ||
| return this | ||
| .byRaiting(); | ||
| } | ||
| return this | ||
| .byRaiting() | ||
| .reverse(); | ||
| }; | ||
|
Member
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. Не закрыта IEFE; Events и все подобные не экспортируются. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| var examples = [ | ||
| { name: "Пара по веб-технологиям", start: new Date("2012-10-20 10:00:00"), end: new Date("2012-10-20 12:50:00"), location: "5 этаж", remindTime: 10, description: "Взять бумагу и ручку, не брать бук!" }, | ||
| { name: "День зимы", start: new Date("2012-10-27 06:00:00"), end: new Date("2012-10-27 12:00:00"), location: "Скандинавия", description: "Кататься ^_^" }, | ||
| { name: "День инженера механика", start: new Date("2012-10-29 10:00:00"), end: new Date("2012-10-29 15:00:00"), location: "9 этаж", remindTime: 10 }, | ||
| { name: "День вегана", start: new Date("2012-11-1 10:00:00"), end: new Date("2012-10-1 23:00"), location: "Дома", description: "Be vegan =)" }, | ||
| { name: "День журналисты", start: new Date("2012-11-8 10:00:00"), location: "Китай", remindTime: 10, description: "Поздравить Олежу" }, | ||
| { name: "Всемирный день борьбы с диабетом", start: new Date("2012-11-14 12:00") }, | ||
| { name: "Международный день отказа от курения", start: new Date("2012-11-15 12:00"), description: "Поздравить Сашку)" }, | ||
| { name: "День защиты черных котов", start: new Date("2012-11-17 14:00:00"), location: "Италия", remindTime: 50, description: "Котэ" }, | ||
| { name: "Всемирный день туалетов", start: new Date("2012-11-19 15:00:00"), location: "МИр", description: "о_О" }, | ||
| { name: "День революции", start: new Date("2012-11-20 12:00:00"), location: "Мексика"}, | ||
| { name: "День сладостей", start: new Date("2012-10-20 15:00:00"), location: "США", remindTime: 10, description: "Приготовить вкусняшки" }, | ||
| { name: "Ерофеев день", start: new Date("2012-10-17 16:00:00"), location: "Россия", description: "Лисики" }, | ||
| { name: "Утиный фестиваль", start: new Date("2012-10-13 12:00:00"), location: "Италия", description: "Все в Италию!" }, | ||
| { name: "Дент ребенка", start: new Date("2012-10-12 14:00:00"), location: "Бразилия" }, | ||
| { name: "День физкультуры", start: new Date("2012-10-8 12:00:00"), location: "Япония"}, | ||
| { name: "Всемирный день животных", start: new Date("2012-10-4 12:00:00 ")}, | ||
| { name: "День сакэ в Японии", start: new Date("2012-10-1 14:00:00") }, | ||
| { name: "День моря", start: new Date("2012-09-27 15:00:00") }, | ||
| { name: "День комиксов", start: new Date("2012-09-25 15:00:00"), location: "США"}, | ||
| { name: "День почитания пожилых людей", start: new Date("2012-09-17 16:00:00")}, | ||
| { name: "Международный жень демократии", start: new Date("2012-09-15 17:00:00")} | ||
| ]; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /** | ||
| * Абстрактный конструктор, принимает объект и создает абстрактный объект для работы | ||
| * @param {Object} attributes | ||
| * | ||
| * @example | ||
| * item.set({title: "March 20", content: "In his eyes she eclipses..."}); | ||
| */ | ||
| var Model = function (data) { | ||
| "use strict"; | ||
|
|
||
| var key; | ||
|
|
||
| for (key in data) { | ||
| if (data.hasOwnProperty(key)) { | ||
| this[key] = data[key]; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| /** | ||
| * Сеттер - устанавливает аттрибуты и значения атрибутов, в соответсвии с принятым в качестве параметра объектом | ||
| * @param {Object} attributes | ||
| * | ||
| * @example | ||
| * item.set({title: "March 20", content: "In his eyes she eclipses..."}); | ||
| */ | ||
| Model.prototype.set = function (attributes) { | ||
| "use strict"; | ||
|
|
||
| var key; | ||
|
|
||
| for (key in attributes) { | ||
| if (attributes.hasOwnProperty(key)) { | ||
| this[key] = attributes[key]; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Геттер - возвращает запрашиваемое свойство у объекта | ||
| * @param {Object} attributes | ||
| */ | ||
| Model.prototype.get = function (attribute) { | ||
| "use strict"; | ||
|
|
||
| if (this.hasOwnProperty(attribute)) { | ||
| return this[attribute]; | ||
| } | ||
|
|
||
| return undefined; | ||
| }; | ||
| /** | ||
| * @param {Object} attributes | ||
| */ | ||
| Model.prototype.validate = function (attributes) { | ||
| "use strict"; | ||
|
|
||
| throw new Error('this is Abstract method'); | ||
| }; |
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.
Ты, кстати, можешь использовать
паттерн модульIEFE и избавиться от кучи"use strict";