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({
tagName: "div",
className: "alert",
template: _.template($("#alert_template").html()),
initialize: function (options) {
var self = this;
@@ -32,11 +33,8 @@ define([
},
render: function () {
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) {
window.setTimeout(function () {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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