refactored code to use newer version of backbone.js and underscore.js

This commit is contained in:
Mike Ng
2018-02-05 16:14:13 +04:00
parent df85aa8827
commit 1337a6994f
11 changed files with 37 additions and 45 deletions

View File

@@ -17,6 +17,7 @@ define([
var AlertView = Backbone.View.extend({ var AlertView = Backbone.View.extend({
tagName: "div", tagName: "div",
className: "alert", className: "alert",
template: _.template($("#alert_template").html()),
initialize: function (options) { initialize: function (options) {
var self = this; var self = this;
@@ -32,11 +33,8 @@ define([
}, },
render: function () { render: function () {
var self = this; var self = this;
var template = _.template($("#alert_template").html(), {
msg: this.message
});
this.$el.addClass("alert-" + this.alert).html(template).alert(); this.$el.addClass("alert-" + this.alert).html(this.template({msg: this.message})).alert();
if (this.delay > 0) { if (this.delay > 0) {
window.setTimeout(function () { window.setTimeout(function () {

View File

@@ -24,10 +24,10 @@ define([
"click .btn-cancel-field": "toggleCreateFormAction", "click .btn-cancel-field": "toggleCreateFormAction",
"keyup input": "onKeyupInput" "keyup input": "onKeyupInput"
}, },
template: _.template($("#create_template").html()),
render: function () { render: function () {
var template = _.template($("#create_template").html());
this.$el.html(template); this.$el.html(this.template());
$("#new-source", this.$el).autocomplete({ $("#new-source", this.$el).autocomplete({
minLength: 2, minLength: 2,

View File

@@ -16,16 +16,16 @@ define([
var DcFieldsView = Backbone.View.extend({ var DcFieldsView = Backbone.View.extend({
tagName: "div", tagName: "div",
className: "input-append", className: "input-append",
template: _.template($("#dc_fields_template").html()),
initialize: function (options) { initialize: function (options) {
this.field = options.field; this.field = options.field;
}, },
render: function () { render: function () {
var template = _.template($("#dc_fields_template").html(), { this.$el.html(this.template({
dces_elements: this.collection.toJSON(), dces_elements: this.collection.toJSON(),
field: this.field.toJSON() field: this.field.toJSON()
}); })
);
this.$el.html(template);
var index = $("#dces-element", this.$el)[0].selectedIndex - 1; var index = $("#dces-element", this.$el)[0].selectedIndex - 1;
if (index > 0) { if (index > 0) {

View File

@@ -22,6 +22,7 @@ define([
var FieldEditView = Backbone.View.extend(_.extend({}, MultiViews, { var FieldEditView = Backbone.View.extend(_.extend({}, MultiViews, {
tagName: "div", tagName: "div",
className: "field-edit", className: "field-edit",
template: _.template($("#edit_template").html()),
initialize: function () { initialize: function () {
this.model.on("change", this._onModelChange, this); this.model.on("change", this._onModelChange, this);
}, },
@@ -34,15 +35,15 @@ define([
}, },
render: function () { render: function () {
var self = this; var self = this;
var template = _.template($("#edit_template").html(), {
this.$el.empty().html(this.template({
lng: AdminFieldApp.lng(), lng: AdminFieldApp.lng(),
field: this.model.toJSON(), field: this.model.toJSON(),
vocabularyTypes: AdminFieldApp.vocabularyCollection.toJSON(), vocabularyTypes: AdminFieldApp.vocabularyCollection.toJSON(),
modelErrors: AdminFieldApp.errorManager.getModelError(this.model), modelErrors: AdminFieldApp.errorManager.getModelError(this.model),
languages: AdminFieldApp.languages languages: AdminFieldApp.languages
}); }
));
this.$el.empty().html(template);
this._assignView({ this._assignView({
".dc-fields-subview": new DcFieldView({ ".dc-fields-subview": new DcFieldView({

View File

@@ -14,6 +14,7 @@ define([
"i18n" "i18n"
], function ($, _, Backbone, i18n) { ], function ($, _, Backbone, i18n) {
var FieldErrorView = Backbone.View.extend({ var FieldErrorView = Backbone.View.extend({
template: _.template($("#field_error_template").html()),
initialize: function () { initialize: function () {
AdminFieldApp.errorManager.on("add-error", this.render, this); AdminFieldApp.errorManager.on("add-error", this.render, this);
AdminFieldApp.errorManager.on("remove-error", this.render, this); AdminFieldApp.errorManager.on("remove-error", this.render, this);
@@ -33,11 +34,7 @@ define([
}); });
}); });
var template = _.template($("#field_error_template").html(), { $(".block-alert").html(this.template({messages: messages}));
messages: messages
});
$(".block-alert").html(template);
return this; return this;
} }

View File

@@ -22,6 +22,7 @@ define([
"keyup #live_search": "searchAction", "keyup #live_search": "searchAction",
"update-sort": "updateSortAction" "update-sort": "updateSortAction"
}, },
template: _.template($("#item_list_view_template").html()),
initialize: function () { initialize: function () {
var self = this; var self = this;
// store all single rendered views // store all single rendered views
@@ -62,9 +63,7 @@ define([
}); });
}, },
render: function () { render: function () {
var template = _.template($("#item_list_view_template").html()); this.$el.empty().html(this.template());
this.$el.empty().html(template);
this.$listEl = $("ul#collection-fields", this.$el); this.$listEl = $("ul#collection-fields", this.$el);

View File

@@ -16,6 +16,7 @@ define([
var FieldListRowView = Backbone.View.extend({ var FieldListRowView = Backbone.View.extend({
tagName: "li", tagName: "li",
className: "field-row", className: "field-row",
template: _.template($("#list_row_template").html()),
initialize: function () { initialize: function () {
// destroy view is model is deleted // destroy view is model is deleted
this.model.on("change", this.onChange, this); this.model.on("change", this.onChange, this);
@@ -52,14 +53,13 @@ define([
} }
}, },
render: function () { render: function () {
var template = _.template($("#list_row_template").html(), { this.$el.empty().html(this.template({
id: this.model.get("id"), id: this.model.get("id") || "",
position: this.model.get("sorter"), position: this.model.get("sorter"),
name: this.model.get("name"), name: this.model.get("name"),
tag: this.model.get("tag") tag: this.model.get("tag")
}); }
));
this.$el.empty().html(template);
// highlight view if edit view model match current view model // highlight view if edit view model match current view model
if (AdminFieldApp.fieldEditView if (AdminFieldApp.fieldEditView

View File

@@ -20,6 +20,7 @@ define([
events: { events: {
"click .confirm": "confirmAction" "click .confirm": "confirmAction"
}, },
template: _.template($("#modal_template").html()),
initialize: function (options) { initialize: function (options) {
var self = this; var self = this;
// remove view when modal is closed // remove view when modal is closed
@@ -32,11 +33,7 @@ define([
} }
}, },
render: function () { render: function () {
var template = _.template($("#modal_template").html(), { this.$el.html(this.template({msg: this.message || ""})).modal();
msg: this.message || ""
});
this.$el.html(template).modal();
return this; return this;
}, },

View File

@@ -16,6 +16,7 @@ define([
"apps/admin/fields/views/alert" "apps/admin/fields/views/alert"
], function ($, _, Backbone, i18n, bootstrap, AlertView) { ], function ($, _, Backbone, i18n, bootstrap, AlertView) {
var SaveView = Backbone.View.extend({ var SaveView = Backbone.View.extend({
template: _.template($("#save_template").html()),
initialize: function () { initialize: function () {
var self = this; var self = this;
this.previousAttributes = []; this.previousAttributes = [];
@@ -78,8 +79,7 @@ define([
return this; return this;
}, },
render: function () { render: function () {
var template = _.template($("#save_template").html()); this.$el.html(this.template());
this.$el.html(template);
this.updateStateButton(); this.updateStateButton();
return this; return this;

View File

@@ -13,8 +13,8 @@ define([
"backbone" "backbone"
], function ($, _, Backbone) { ], function ($, _, Backbone) {
var SchedulerView = Backbone.View.extend({ var SchedulerView = Backbone.View.extend({
template: _.template($('#scheduler_template').html()),
initialize: function () { initialize: function () {
this.template = _.template($('#scheduler_template').html());
// render only parts of the model // render only parts of the model
this.model.on('change:configuration', this.renderConfiguration, this); this.model.on('change:configuration', this.renderConfiguration, this);
this.model.on('change:actual', this.renderActual, this); this.model.on('change:actual', this.renderActual, this);

View File

@@ -13,8 +13,8 @@ define([
"backbone" "backbone"
], function ($, _, Backbone) { ], function ($, _, Backbone) {
var TaskView = Backbone.View.extend({ var TaskView = Backbone.View.extend({
template: _.template($('#task_template').html()),
initialize: function () { initialize: function () {
this.template = _.template($('#task_template').html());
// render only parts of the model // render only parts of the model
this.model.on('change:id', this.renderId, this); this.model.on('change:id', this.renderId, this);
this.model.on('change:configuration', this.renderConfiguration, this); this.model.on('change:configuration', this.renderConfiguration, this);