Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
var view = new Header-view();
$('.main').html(view.$el);
newMessage : function(){
require([
'js/modules/NewMessage/NewMessageModel',
'js/modules/NewMessage/NewMessageView'
], function(NewMessagesModel, NewMessagesView){
var model = new NewMessagesModel(),
view = new NewMessagesView({model : model});
$('.main').html(view.$el);
}.bind(this));
}
Плохая практика, это писать код завязанный глобальным переменных и другом магическом знании, в вашем случае глобальные селекторы.
Ну и «Толик» всё же не справился с задачей до конца и использует глобальную переменную app:
app.headerView = new HeaderView(app, document.getElementById('head'));define([
'jquery',
'underscore',
'backbone',
'require'
], function ($, _, Backbone, App, require) {
"use strict";
return Backbone.Router.extend({
routes: {
'': 'notFound',
'newMessage' : 'newMessage',
'*notFound': 'notFound'
},
notFound: function (route) {
this.navigate('newMessage', {trigger: true});
},
newMessage : function(){
require([
'js/modules/NewMessage/NewMessageModel',
'js/modules/NewMessage/NewMessageView'
], function(NewMessagesModel, NewMessagesView){
var model = new NewMessagesModel(),
view = new NewMessagesView({model : model});
$('.main').html(view.$el);
}.bind(this));
}
})
define([
'underscore',
'backbone'
], function (_, Backbone) {
"use strict";
return Backbone.Model.extend({
url: '/api/v2/mail',
defaults : {
document_number : '',
mail_subject : '',
mail_body : '',
attach : []
},
initialize: function () {
return this;
},
validate : function(attr){
var arr = _.compact(_.map(attr, function (val, name) {
var error_name = '';
if (!val) {
error_name = name;
}
return error_name;
}));
return arr.length > 0 ? arr : false;
}
});
});
define([
'jquery',
'underscore',
'backbone',
'toastr',
'js/modules/FileUploader/FileUploaderModel',
'js/modules/FileUploader/FileUploaderCollection',
'js/modules/FileUploader/FileUploaderCollectionView',
'js/modules/FileUploaderTemp/FileUploaderTempCollection',
'js/modules/FileUploaderTemp/FileUploaderTempCollectionView',
'js/modules/FileUploaderTemp/FileUploaderTempModel',
'js/app'
], function ($, _, Backbone, toastr,
FileUploaderModel,
FileUploaderCollection,
FileUploaderCollectionView,
FileUploaderTempCollection,
FileUploaderTempCollectionView,
FileUploaderTempModel,
App) {
"use strict";
return Backbone.View.extend({
tagName: 'div',
className: 'NewMessage',
events: {
'click .TooltipLoaderCloseButton' : 'remove',
'click .sendMessage' : 'sendMessage',
'keyup .limited': 'limit_characters',
'keydown .limited': 'limit_characters'
},
initialize: function () {
this.model.on('validationError', this.showError, this);
this.render();
return this;
},
render: function () {
this.template().done(function (html) {
this.$el.html(html);
var fileUploaderCollection = new FileUploaderCollection(this.model.get('attach') || [], {
model : FileUploaderModel,
modelReference : this.model
}),
fileUploaderCollectionView = new FileUploaderCollectionView({
collection : fileUploaderCollection
}),
fileUploaderTempCollection = new FileUploaderTempCollection([], {
model : FileUploaderTempModel
}),
fileUploaderTempCollectionView = new FileUploaderTempCollectionView({
collection : fileUploaderTempCollection,
collectionReference : fileUploaderCollection
});
this.$el.find('.fileUploaderTemp').replaceWith(fileUploaderTempCollectionView.$el);
this.$el.find('.fileUploader').replaceWith(fileUploaderCollectionView.$el);
}.bind(this));
return this;
},
sendMessage : function(){
var data = this.serialize(),
promise = this.model.save(data, {
data : JSON.stringify(data),
contentType : 'application/json'
});
if(promise){ //validation passed
promise.done(function(){
toastr.success('Сообщение успешно отправлено');
App.router.navigate('/journal', {trigger : true});
this.remove();
}.bind(this));
}
return promise;
},
serialize : function(){
var res = {
document_number : this.$el.find('input[name=document_number]').val(),
mail_subject : this.$el.find('input[name=mail_subject]').val(),
mail_body : this.$el.find('textarea[name=mail_body]').val()
};
if(this.model.get('attach').length > 0){
res.attach = this.model.get('attach');
}
return res;
},
showError : function(){ //TODO error handling
console.log(arguments);
},
limit_characters: function (event) {
var res = true,
target = $(event.currentTarget),
limit = +target.attr('limit'),
length = target.val().length;
if (event.type === 'keydown' && length > limit + 5) {
res = false;
} else if (length > limit) {
event.preventDefault();
target.val(target.val().substr(0, limit));
}
return res;
}
});
});
Фундамент масштабируемости javascript приложения