cartodb-4.42/lib/assets/javascripts/cartodb/common/view_factory.js
2024-04-06 05:25:13 +00:00

66 lines
1.9 KiB
JavaScript

var _ = require('underscore-cdb-v3');
var cdb = require('cartodb.js-v3');
var BaseDialog = require('./views/base_dialog/view');
/**
* Convenient factory to create views without having to create new files.
*/
module.exports = {
createDialogByTemplate: function(templateOrStr, templateData, dialogOptions) {
return this.createDialogByView(this.createByTemplate(templateOrStr, templateData), dialogOptions);
},
/**
* @return {Object} instance of cdb.core.View, which takes two params of template and templateData
*/
createByTemplate: function(templateOrStr, templateData, viewOpts) {
var template = _.isString(templateOrStr) ? cdb.templates.getTemplate(templateOrStr) : templateOrStr;
var view = new cdb.core.View(viewOpts);
view.render = function() {
this.$el.html(
template(templateData)
);
return this;
};
return view;
},
/**
* Creates a view that holds a list of views to be rendered.
* @param {Array} list of View object, i.e. have a render method, $el, and clean method.
* @param {Object,undefined} viewOpts view options, .e.g {className: 'Whatever'}
* @return {Object} A view
*/
createByList: function(views, viewOpts) {
var listView = new cdb.core.View(viewOpts);
listView.render = function() {
this.clearSubViews();
_.each(views, function(view) {
this.addView(view);
this.$el.append(view.render().$el);
}, this);
return this;
};
return listView;
},
createDialogByView: function(contentView, dialogOptions) {
var options = _.extend({ clean_on_hide: true, enter_to_confirm: true }, dialogOptions);
return new (BaseDialog.extend({
initialize: function() {
this.elder('initialize');
this.addView(contentView);
},
render_content: function() {
return contentView.render().el;
}
}))(options);
}
};